【算法通关指南:C++编程篇(四) 】---- 条件判断与循环练习合集(中)
·

🔥小龙报:个人主页
🎬作者简介:C++研发,嵌入式,机器人方向学习者
❄️个人专栏:《算法通关指南》
✨ 永远相信美好的事情即将发生

前言
本专栏聚焦算法题实战,系统讲解算法模块:以《c++编程》,《数据结构和算法》《基础算法》《算法实战》 等几个板块以题带点,讲解思路与代码实现,帮助大家快速提升代码能力
ps:本章节题目分两部分,比较基础笔者只附上代码供大家参考,其他的笔者会附上自己的思考和讲解,希望和大家一起努力见证自己的算法成长

一、晶晶赴约会
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;
else
cout << "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;
else
cout << 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;
else
cout << 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;
}
总结–每日励志时刻

更多推荐
所有评论(0)