2887 能被3,5,7整除的数

2887:能被3,5,7整除的数\

总时间限制: 
1000ms
内存限制: 
65536kB
描述
输入一个整数,判断它能否被3,5,7整除,并输出以下信息:
1、能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格);
2、能被其中两个数整除(输出两个数,小的在前,大的在后。例如:3 5或者 3 7或者5 7,中间用空格分隔)
3、能被其中一个数整除(输出这个除数)
4、不能被任何数整除;(输出小写字符'n',不包括单引号)
输入
一个数字
输出
一行数字,从小到大排列,包含3,5,7中为该输入的除数的数字,数字中间用空格隔开
样例输入
0
5
15
105
样例输出
3 5 7
5
3 5
3 5 7
提示
因为有多组测试数据,程序通过下面方式读入n

int n;
while(cin>>n)
{
//你的代码


good luck:)
【思路】设置一个数count用于记录,初始值为0。分别判断是否能被3,5,7整除,若能则加上相对应的数,通过最后该数的值进行判断。
【代码】AC的C++代码如下:
#include <iostream>
using namespace std;
int main()
{
    int x;
    int count;
    while (cin >> x)
    {
        count = 0;
        if (x % 3 == 0)
            count += 3;
        if (x % 5 == 0)
            count += 5;
        if (x % 7 == 0)
            count += 7;
        if (count == 0) //不能被任何数整除
            cout << "n" << endl;
        else if (count == 3 || count == 5 || count == 7)
        //能被其中一个数整除
            cout << count << endl;
        else if (count == 8 || count == 10 || count == 12)
        {//能被其中两个数整除
            if (count == 8)
                cout << "3 5" << endl;
            else if (count == 10)
                cout << "3 7" << endl;
            else
                cout << "5 7" << endl;
        }
        else
            cout << "3 5 7" << endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/m0_38056893/article/details/80216447