09:判断能否被3,5,7整除
描述
给定一个整数,判断它能否被3,5,7整除,并输出以下信息:
1、能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格);
2、只能被其中两个数整除(输出两个数,小的在前,大的在后。 例如:3 5或者 3 7或者5 7,中间用空格分隔);
3、只能被其中一个数整除(输出这个除数);
4、不能被任何数整除,输出小写字符‘n’,不包括单引号。
输入
输入一行,包括一个整数。
输出
输出一行,按照描述要求给出整数被3,5,7整除的情况。
示例输入
105
示例输出
3 5 7
分析
1.可以使用if语句嵌套,也可以不嵌套,但是一定要把条件写完全。
2.记得全部不能整除需要输出'n'
代码
#include <iostream>
using namespace std;
int main()
{
int n; //声明一个整数n
cin >> n;
if (n % 3 == 0) //3
{
cout << '3';
if (n % 5 == 0) //3,5
{
cout << ' ' << '5';
if (n % 7 == 0) //3,5,7
{
cout << ' ' << '7' << endl;
}
}
else //3,7
{
if (n % 7 == 0)
{
cout << ' ' << '7' << endl;
}
}
}
else
{
if (n % 5 == 0) //5
{
cout << '5';
if (n % 7 == 0) //5,7
{
cout << ' ' << '7' << endl;
}
}
else
{
if (n % 7 == 0) //7
{
cout << '7' << endl;
}
else //n
{
cout << 'n' << endl;
}
}
}
return 0;
}
或
#include <iostream>
using namespace std;
int main()
{
int n; //声明一个整数n
cin >> n;
if ((n % 3 == 0) && (n % 5 == 0) && (n % 7 == 0)) //3,5,7
{
cout << '3' << ' ' << '5' << ' ' << '7' << endl;
return 0;
}
if ((n % 3 == 0) && (n % 5 == 0) && (n % 7 != 0)) //3,5
{
cout << '3' << ' ' << '5' << endl;
return 0;
}
if ((n % 3 == 0) && (n % 5 != 0) && (n % 7 == 0)) //3,7
{
cout << '3' << ' ' << '7' << endl;
return 0;
}
if ((n % 3 != 0) && (n % 5 == 0) && (n % 7 == 0)) //5,7
{
cout << '5' << ' ' << '7' << endl;
return 0;
}
if ((n % 3 == 0) && (n % 5 != 0) && (n % 7 != 0)) //3
{
cout << '3' << endl;
return 0;
}
if ((n % 3 != 0) && (n % 5 == 0) && (n % 7 != 0)) //5
{
cout << '5' << endl;
return 0;
}
if ((n % 3 != 0) && (n % 5 != 0) && (n % 7 == 0)) //7
{
cout << '7' << endl;
return 0;
}
cout << 'n' << endl; //n
return 0;
}