实验4-2-8 输出整数各位数字 (15分)

题要求编写程序,对输入的一个整数,从高位开始逐位分割并输出它的各位数字。

输入格式:

输入在一行中给出一个长整型范围内的非负整数。

输出格式:

从高位开始逐位输出该整数的各位数字,每个数字后面有一个空格。

输入样例:

123456

输出样例:

1 2 3 4 5 6 

 //很死板的写法

#include<stdio.h>
#include<math.h>
int main()
{
int fact(int x);
int n,i,t;
int shu=0,count=0,num=0;
scanf("%d",&n);
if(n==0)
{
printf("0 ");
}
else if(n>0)
{

count=fact(n);
// printf("count=%d\n",count);
shu=count;
int str[shu];
while(n>0)
{

// printf("%d\n",t);
for(i=0;i<shu;i++)
{
t=n%10;
// printf("t=%d\n",t);
str[i]=t;
// printf("str=%d\n",str[i]);
n=n/10;
// printf("n=%d\n",n);
}
}
for(i=shu-1;i>=0;i--)
{
printf("%d ",str[i]);
}

}

return 0;
}
int fact(int x)
{
int count=0;
while(x>0)
{
count++;
x=x/10;
}
return count;
}

 

 

//新学一位大佬的,卧槽,牛逼,呜呜呜呜,我好笨

#include<stdio.h>
int main()
{
char a[100];
int i;
gets(a);
while(a[i])
{
printf("%c ",a[i]);
i++;
}

return 0;
}

//

#include<stdio.h>
#include<math.h>
int main(){
int n,m;
int count=0,i;
scanf("%d",&n);
m=shu;
if(n==0) printf("0 ");
else{
while(m){
m/=10;
count++;
}
for(i=count;i>0;i--){
printf("%d ",shu/(int)pow(10,i-1));
shu%=(int)pow(10,i-1);
}
}
return 0;
}

 

posted @ 2020-05-22 23:04  attendere  阅读(874)  评论(0编辑  收藏  举报