一、题目链接
http://noi.openjudge.cn/ch0104/09/
二、解题思路
◎ 一个指定的整数存在四种可能性:
→ 对3求余数为0,则能被3整除;
→ 对5求余数为0,则能被5整除;
→ 对7求余数为0,则能被7整除;
→ 对3、5、7求余数均不为0,则不能被3、5、7中的任意一个数整除;
以上四种可能性不是互斥的。
三、实施步骤
◎ 首先,定义并输入int类型的整数n,代表待判断的数;
◎ 其次:
→ 如果n%3==0,输出3和一个空格;
→ 如果n%5==0,输出5和一个空格;
→ 如果n%7==0,输出7;
→ 如果n%3!=0&&n%5!=0&&n%7!=0,输出字符n。
四、Java程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if (n % 3 == 0) {
System.out.print(3 + " ");
}
if (n % 5 == 0) {
System.out.print(5 + " ");
}
if (n % 7 == 0) {
System.out.print(7);
}
if (n % 3 != 0 && n % 5 != 0 && n % 7 != 0) {
System.out.print('n');
}
}
}
五、C++程序
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n % 3 == 0)
{
cout << 3 << " ";
}
if (n % 5 == 0)
{
cout << 5 << " ";
}
if (n % 7 == 0)
{
cout << 7;
}
if (n % 3 != 0 && n % 5 != 0 && n % 7 != 0)
{
cout << 'n';
}
return 0;
}