《算法通关指南---C++编程篇(4)》
《151道题带你快速梳理C++知识(3)-- 条件判断与循环(中)》
前言
🔥小龙报:个人主页
🎬作者简介:C++研发,嵌入式,机器人方向学习者
❄️个人专栏:《C语言》《算法》KelpBar海带Linux智慧屏项目
✨永远相信美好的事情即将发生
一、晶晶赴约会
1.1题目链接:晶晶赴约会
1.2题目解析
代码:
#include <iostream>
using namespace std;int main()
{int n;cin >> n;if (n == 1 || n == 3 || n == 5)cout << "NO" << endl;elsecout << "YES" << endl;return 0;
}
二、三角形判断
2.1题目链接:三角形判断
2.2题目解析
代码:
#include <iostream>
using namespace std;int main()
{int a, b, c;cin >> a >> b >> c;if ((a + b > c) && (a + c > b) && (b + c > a))cout << 1 << endl;elsecout << 0 << endl;return 0;
}
三、判断能否被 3,5,7 整除
3.1题目链接:判断能否被 3,5,7 整除
3.2题目解析
代码:
#include <iostream>
using namespace std;int main()
{int x;cin >> x;if ((x % 3 == 0) && (x % 5 == 0) && (x % 7 == 0))cout << 3 << " " << 5 << " " << 7 << endl;else if ((x % 3 != 0) && (x % 5 != 0) && (x % 7 != 0))cout << "n" << endl;else if ((x % 3 == 0) && (x % 5 == 0))cout << 3 << " " << 5 << endl;else if ((x % 3 == 0) && (x % 7 == 0))cout << 3 << " " << 7 << endl;else if ((x % 5 == 0) && (x % 7 == 0))cout << 5 << " " << 7 <<endl;return 0;
}
四、数的性质
4.1题目链接:数的性质
4.2题目解析
代码:
#include <iostream>
using namespace std;int main()
{int x;cin >> x;int a = 0, b = 0, c = 0, d = 0;if ((x % 2 == 0) && (x > 4 && x <= 12))a = 1;if (x % 2 == 0 || x > 4 && x <= 12) //至少符合一个b = 1;if (x % 2 == 0 && x < 4 || x % 2 != 0 && x > 4 && x <= 12) //刚好一个c = 1;if (x % 2 != 0 && x < 4)d = 1;cout << a << " " << b << " " << c << " " << d << " ";return 0;
}
五、四季
5.1题目链接:四季
5.2题目解析
代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{int year = 0, month = 0;scanf("%4d%2d", &year, &month);if (month >= 3 && month <= 5)cout << "spring" << endl;else if (month >= 6 && month <= 8)cout << "summer" << endl;else if (month >= 9 && month <= 11)cout << "autumn" << endl;else if (month == 12 || month == 1 || month == 2)cout << "winter" << endl;return 0;
}
六、简单计算器
6.1题目链接:
6.2题目解析
七、反向输出一个四位数
7.1题目链接:反向输出一个四位数
7.2题目解析
代码:
#include <iostream>
using namespace std;
int main()
{int n;cin >> n;while(n){cout << n % 10;n /= 10;}return 0;
}
八、简单计算器
8.1题目链接:简单计算器
8.2题目解析
代码:
#include <iostream>
using namespace std;int main()
{int a, b;char c;cin >> a >> b >> c;switch (c){case '+':cout << a + b << endl;break;case '-':cout << a - b << endl;break;case '*':cout << a * b << endl;break;case '/':if (b == 0)cout << "Divided by zero!" << endl;elsecout << a / b << endl;break;default:cout << "Invalid operator!" << endl;break;}
}
九、数位之和
9.1题目链接:数位之和
9.2题目解析
代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{int n;int sum = 0;cin >> n;if(n < 0)n = fabs(n);while(n){sum += n % 10;n /= 10;}cout << sum << endl;return 0;
}
十、小乐乐求和
10.1题目链接:小乐乐求和
10.2题目解析
代码:
#include <iostream>using namespace std;typedef long long LL;int main(){int n;cin >> n;LL sum = 0;for(LL i = 1;i <= n;i++){sum += i;}cout << sum << endl;return 0;}