我写了一个代码,用来计算用户提供的数字中有多少个零,几率和小数。
我如何更改此代码,以便结果我有:
谢谢。
int sk,a=0,b=0,c=0;
printf ("Write number: ");
scanf("%d", &sk);
while (sk!=0){
if (sk%10==0){
a++;
}
else if (sk%2==0){
b++;
}
else {
c++;
}
sk=sk/10;
}
printf(" Zeros %d \n Evens %d \n Odss %d ",a , b, c);
答案 0 :(得分:1)
这可能会有所帮助:)
int sk,a=0,b=0,c=0;
int cnt_a=1,cnt_b=1,cnt_c=1;
printf ("Write number : ");
scanf("%d", &sk);
while (sk != 0){
if (sk%10 != 0){
a = a + (sk%10 * cnt_a);
cnt_a *= 10;
}
if (sk%2==0){
b = b + (sk%10 * cnt_b);
cnt_b *= 10;
}
else {
c = c + (sk%10 * cnt_c);
cnt_c *= 10;
}
sk = sk/10;
}
printf(" Without Zeros : %d \n Only Evens : %d \n Only Odds : %d ", a , b, c);
答案 1 :(得分:0)
更改'printf'功能,如上所述,您将收到缺失的变量。
int sk,a=0,b=0,c=0;
printf ("Write number: ");
scanf("%d", &sk);
while (sk != 0) {
if (sk%10 == 0){
a++;
}
else if (sk%2 == 0){
b++;
}
else {
c++;
}
sk = sk/10;
}
printf(" Zeros %d \n Evens %d \n Odss %d \n Without Zeros: %d \n Without Odds: %d \n",a , b, c, b+c, a+b);