优化代码:能否被3,5,7整除

This commit is contained in:
xtaodada 2021-10-25 13:54:26 +08:00
parent 48e7778b82
commit 717cf8dccf
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736

View File

@ -2,44 +2,36 @@
int number(int num){ int number(int num){
int a = num % 3,b = num % 5,c = num % 7; int a = num % 3,b = num % 5,c = num % 7,first = 1;
if (a == 0) { if (a == 0 || b == 0 || c == 0) {
if (b == 0) { printf("Can be divisible by ");
if (c == 0) { if (a == 0)
printf("Can be divisible by 3,5,7."); first = 0;
printf("3");
if (b == 0)
if (first) {
printf("5");
first = 0;
}
else
printf(",5");
// 防止编译器编译时输出警告
else a=a;
if (c == 0)
if (first) {
printf("7");
first = 0;
} }
else { else
printf("Can be divisible by 3,5."); printf(",7");
} // 防止编译器编译时输出警告
} else a=a;
else {
if (c == 0) {
printf("Can be divisible by 3,7.");
}
else {
printf("Can be divisible by 3.");
}
}
} }
else { else {
if (b == 0) { printf("Can not be divisible by 3,5,7");
if (c == 0) {
printf("Can be divisible by 5,7.");
}
else {
printf("Can be divisible by 5.");
}
}
else {
if (c == 0) {
printf("Can be divisible by 7.");
}
else {
printf("Can not be divisible by 3,5,7.");
}
}
} }
printf(".");
return 0; return 0;
} }