實驗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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM