[刷题] PTA 7-37 输出整数各位数字

程序:

方法1:

 1 #include <stdio.h>
 2 #define N 10000
 3 int main(){
 4     long n, temp;
 5     long mask=1;
 6     scanf("%ld", &n);
 7     temp = n;
 8     while(temp/10 != 0){
 9         // 获取最高位权值
10         mask *= 10;
11         temp /= 10;
12     }
13     while(mask!=0){
14         // 按权逐位输出,权为0表示各位已经输出完毕
15         printf("%d ", n/mask);
16         n %= mask;
17         mask /= 10;
18     }
19 
20     return 0;
21 }

方法2:

 1 #include <stdio.h>
 2 
 3 int main(void) {
 4     char temp;
 5     temp = getchar();
 6     while (temp >= '0'&&temp <= '9') {
 7         printf("%c ", temp);
 8         temp = getchar();
 9     }
10     printf("
");
11     return 0;
12 }

方法3:

 1 #include<stdio.h>\字符串数组的做法
 2 #include<string.h>
 3 int main()
 4 {
 5     char a[100]={''};
 6     gets(a);
 7     for(int i = 0;i < strlen(a);i ++){
 8         printf("%c ",a[i]);
 9     }
10 return 0;
11 }

我的代码,有一个测试用例通不过:

 1 #include<stdio.h>
 2 #include<math.h>
 3 
 4 int main() {
 5     long i,n,c;
 6     scanf("%ld",&n);
 7     c = log10(n) + 1;
 8     int a[c];
 9     for(i=0; i<c; i++) {
10         a[i] = n%10;
11         n = n/10;
12     }
13     for(i=c-1; i>=0; i--) {
14         printf("%d ",a[i]);
15     }
16 }
原文地址:https://www.cnblogs.com/cxc1357/p/10720680.html