5 条题解
-
3
解题思路:
根据题目来看,我们很容易写一堆if-else if-else去判断多种情况该输出什么,事实上,我们只需要依次判断n能否被3,5,7整除,如果能,就输出这个数和一个空格,因为我们还需要处理n不能被3,5,7整除的情况,所以我们可以设一个变量,并使其初始值为0,当它能被3,5,7任何一个数字整除时,将变量更改为1,如果判断完成后,变量仍然为0,输出n
#include <iostream> using namespace std; int main() { int n,state = 0; cin >> n; if (n % 3 == 0) { cout << 3 << ' '; state = 1; } if (n % 5 == 0) { cout << 5 << ' '; state = 1; } if (n % 7 == 0) { cout << 7 << ' '; state = 1; } if (state == 0) cout << 'n'; return 0; }
-
1
#include <iostream> using namespace std; int main() { int a; cin >> a; //能同时被3,5,7整除 if(a%105==0) { cout << "3 5 7"; } //只能被其中两个数乘除 else if(a%35==0) { cout << "5 7"; } else if(a%21==0) { cout << "3 7"; } else if(a%15==0) { cout << "3 5"; } //只能被其中一个数整除 else if(a%7==0) { cout << "7"; } else if(a%5==0) { cout << "5"; } else if(a%3==0) { cout << "3"; } //不能被任何数整除 else { cout << "n"; } }
-
0
```cpp #include <iostream> using namespace std; int main() { int x; cin >> x; if (x%3==0&&x%5==0&&x%7==0)//能否被3,5,7整除 cout << "3 5 7"; else if (x%3==0&&x%5==0&&x%7!=0)//只被3,5整除 cout << "3 5"; else if(x%3==0&&x%7==0&&x%5!=0)//只被3。7整除 cout << "3 7"; else if(x%5==0&&x%7==0&&x%3!=0)//只被5,7整除 cout << "5 7"; else if(x%3==0&&x%5!=0&&x%7!=0)只被3整除 cout << "3"; else if(x%3!=0&&x%5==0&&x%7!=0)只被5整除 cout << "5"; else if(x%3!=0&&x%5!=0&&x%7==0)只被7整除 cout << "7"; else cout <<"n"; return 0; }
-
0
#include <iostream> using namespace std; int main() { int n; cin >> n; if (n % 3 == 0 && n % 5 == 0 && n % 7 == 0) { cout << "3 5 7"; } else if (n % 3 == 0 && n % 5 == 0) { cout << "3 5"; } else if (n % 3 == 0 && n % 7 == 0) { cout << "3 7"; } else if (n % 5 == 0 && n % 7 == 0) { cout << "5 7"; } else if (n % 3 == 0) { cout << "3"; } else if (n % 5 == 0) { cout << "5"; } else if (n % 7 == 0) { cout << "7"; } else { cout << "n"; } return 0; }
- 1
信息
- ID
- 166
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 3
- 标签
- 递交数
- 220
- 已通过
- 117
- 上传者