41.

#include <iostream> using namespace std; int main() { int N; cin >> N; if (N > 0) cout << "positive"; //如果大于0 输出 positive else if (N == 0) cout << "zero"; //else if 相当于Python的elif else cout << "negative"; //否则 输出 negative return 0; }
结果:
42.
#include <iostream> #include <iomanip> using namespace std; int main() { float x; cin >> x; if (x < 0) x *= -1; // 如果x小于(负数),则乘以-1变为正数 cout << fixed << setprecision(2) << x; //保留小数点2位 return 0; }
结果:
43.
#include <iostream> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) cout << "even"; //如果除以2余数为0,输出 even 偶数 else cout << "odd"; //否则输出 odd 奇数 return 0; }
结果:
44.
#include <iostream> using namespace std; int main() { char c; //声明字符变量c cin >> c; if (c % 2 != 0) cout << "YES"; //如果c的值除以2余数不为0 输出 YES else cout << "NO"; //否则输出 NO return 0; }
结果:
45.
#include <iostream> using namespace std; int main() { long long x, y; //声明 long long型变量x,y cin >> x >> y; if (x > y) cout << '>'; //如果x>y 输出 > else if (x == y) cout << '='; //如果 x==y 输出 = else cout << '<'; //否则输出 < return 0; }
结果:
46.
#include <iostream> using namespace std; int main() { int n; cin >> n; if (n >= 10 && n <= 99) cout << 1; //如果大于等于10,小于等于99 输出 1 else cout << 0; //否则输出 0 return 0; }
结果:
47.
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; if (a >= 10 || b >= 20) cout << 1; // || 或 && 与 ! 非 如果a>=10或b>=20 输出1 else cout << 0; //否则输出0 return 0; }
结果:
48.
#include <iostream> using namespace std; int main() { int n; cin >> n; if (n % 3 == 0 && n % 5 == 0) cout << "YES"; //如果除以3余数为0或除以5余数为0 输出 YES else cout << "NO"; //否则输出 NO return 0; }
结果:
49.
#include <iostream> using namespace std; int main() { int n; cin >> n; bool b3 = n % 3 == 0; //除以3余数为0 是 1 否 0 bool b5 = n % 5 == 0; //除以5余数为0 是 1 否 0 bool b7 = n % 7 == 0; //除以7余数为0 是 1 否 0 if (b3) cout << "3 "; //如果为1 输出3 if (b5) cout << "5 "; //如果为1 输出5 if (b7) cout << "7 "; //如果为1 输出7 if (!b3 && !b5 && !b7) cout << 'n'; return 0; }
结果:
50.
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; if (a >= 60 && b < 60 || a < 60 && b >= 60) //如果 a>=60 并且 b<60 或 a<60 并且 b>=60 cout << 1; else cout << 0; return 0; }
结果:


还没有评论,来说两句吧...