分类讨论,不重不漏

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int x;
  6. cin >> x;
  7. if (x > 0) cout << '+' << '\n';
  8. if (x > 0) cout << '+' << '\n';
  9. else cout << "-" << '\n';
  10. if (x > 0) cout << '+' << '\n';
  11. else if (x == 0) cout << '0' << '\n';
  12. else cout << '-' << '\n';
  13. if (x > 0) cout << '+';
  14. else{
  15. if (x == 0) cout << '0';
  16. else cout << '-';
  17. }
  18. //if嵌套,套一层而已
  19. if (x >= 0){
  20. if (x == 0) cout << '0' << '\n';
  21. else cout << '+' << '\n';
  22. }
  23. return 0;
  24. }

例题,收集瓶盖赢大奖

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int a, b;
  6. cin >> a >> b;
  7. if (a >= 10 || b >= 20) cout << 1 << endl;
  8. else cout << 0 << endl;
  9. return 0;
  10. }

例题,判断一个数能否同时被3和5整除

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n;
  6. cin >> n;
  7. if (n % 3 == 0 && n % 5 == 0) puts("YES");
  8. else puts("NO");
  9. return 0;
  10. }

例题,判断能否被3,5,7整除

  1. //

例题,判断闰年

  1. // 百度百科,闰年
  2. // 普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。
  3. // 世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)。
  4. // 能被4整除,不能被100整除的,是闰年
  5. // 能被400整除的,是闰年
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. int main()
  9. {
  10. int n;
  11. cin >> n;
  12. if ((n % 4 == 0 && n % 100) || (n % 400 == 0)) puts("Y");
  13. else puts("N");
  14. return 0;
  15. }

例题,求一元二次方程

  1. // 本题对小学阶段有点麻烦,需要学习几下基本知识
  2. // 重点是阅读一下,我是如何用代码表达意思的
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. int main(){
  6. double a, b, c;
  7. cin >> a >> b >> c;
  8. double delta = b * b - 4 * a * c;
  9. if (delta < 0) cout << "No answer!" << '\n';
  10. else if (delta == 0){
  11. printf("x1=x2=%.5lf\n", -b / (2 * a));
  12. }
  13. else{
  14. double x1 = (-b - sqrt(delta)) / (2 * a);
  15. double x2 = (-b + sqrt(delta)) / (2 * a);
  16. printf("x1=%.5lf;x2=%.5lf\n", min(x1, x2), max(x1, x2));
  17. }
  18. return 0;
  19. }