係統粉 > 電腦問答 > 其他問答

已解決哪位好心幫我用C++編寫一個小程序,謝謝幫忙先

提問者:福清哥走天下  |  瀏覽次  |  提問時間:2017-01-11  |  回答數量:3

哪位好心幫我用C++編寫一個小程序,謝謝幫忙先 如題:給出一個不多於5位的正整數,要求:1,求出它是幾位數;2,分別打印出每一位數字;3,按逆序打印出各位數字,例如原數為321,應輸出123.

已有3條答案
_沈柒墨

_沈柒墨

回答數:45  |  被采納數:45

char str[10];
scanf("%s",str);
printf("%d位數",strlen(str));
printf(str);
int i=strlen(str)-1;
while(i>=0)printf("%c",str[i--]);
2017-01-11 01:21:58
讚 4
惟愛薑寶

惟愛薑寶

回答數:0  |  被采納數:0

謝謝啦,不過我現在學C++的,C不太懂,有沒有C++的?
2017-01-11 03:21:03
讚 0
weiyun510

weiyun510

回答數:34  |  被采納數:118

#include<iostream>
using namespace std;
int main()
{
int num,place,i,j,k,m,n;
cout<<"請輸入\n";
cin>>num;
if(num>99999)
cout<<"The data error! ";
else
if(num>9999)
{i=num/10000;
j=(num-i*10000)/1000;
k=(num-i*10000-j*1000)/100;
m=(num-i*10000-j*1000-k*100)/10;
n=num-i*10000-j*1000-k*100-m*10;
place=5;
cout<<"你輸入數位數為:"<<place<<"\n";
cout<<"你輸入數的各位為:\n";
cout<<i<<" "<<j<<" "<<k<<" "<<m<<" "<<n;
cout<<"\n";
cout<<"逆序排列為:\n";
cout<<n<<m<<k<<j<<i<<"\n";}
else
if(num>999)
{i=num/1000;
j=(num-i*1000)/100;
k=(num-i*1000-j*100)/10;
m=num-i*1000-j*100-k*10;
place=4;
cout<<"你輸入數位數為:"<<place<<"\n";
cout<<"你輸入數的各位為:\n";
cout<<i<<" "<<j<<" "<<k<<" "<<m;
cout<<"\n";
cout<<"逆序排列為:\n";
cout<<m<<k<<j<<i<<"\n";}
else
if(num>99)
{i=num/100;
j=(num-i*100)/10;
k=(num-i*100-j*10);
place=3;
cout<<"你輸入數位數為:"<<place<<"\n";
cout<<"你輸入數的各位為:\n";
cout<<i<<" "<<j<<" "<<k;
cout<<"\n";
cout<<"逆序排列為:\n";
cout<<k<<j<<i<<"\n";}
else
if(num>9)
{i=num/10;
j=num-i*10;
place=2;
cout<<"你輸入數位數為:"<<place<<"\n";
cout<<"你輸入數的各位為:\n";
cout<<i<<" "<<j;
cout<<"\n";
cout<<"逆序排列為:\n";
cout<<j<<i<<"\n";}
else
{i=num;
place=1;
cout<<"你輸入數位數為:"<<place<<"\n";
cout<<"你輸入數的各位為:\n";
cout<<i;
cout<<"\n";
cout<<"逆序排列為:\n";
cout<<i<<"\n";}

return 0;

}
2017-01-11 06:48:51
讚 3
相關問答
最新其他問答
解決方法