逆序数字 新手题
有多组数据,每组数据只有一个不多于5位的正整数,如321output
几位数
输出每位数,以逗号相隔
按逆序打印各位数,如原数321,应输出123
如输入321
输出
3
3,2,1
[此贴子已经被作者于2019-10-16 19:34编辑过]
#include<stdio.h> int main() { int long x; scanf("%d",&x); if(x<=9) { printf("%d\n",1); printf("%d\n",x); int a; printf("%d",a=x); } if(x<99&&x>9) { printf("%d\n",2); printf("%d,%d\n",x/10,x%10); int a; printf("%d",a=x%10*10+x/10); } if(x<999&&x>99) { printf("%d\n",3); printf("%d,%d,%d\n",x/100,(x%100)/10,x%10); int a; printf("%d",a=x%10*100+(x%100)/10*10+x/100); if(x<9999&&x>999) { printf("%d\n",4); printf("%d,%d,%d,%d\n",x/1000,(x%1000)/100,(x%100)/10,x%10); int a; printf("%d",a=x%10*1000+(x%100)/10*100+(x%1000)/100*10+x/1000); } if(x<99999&&x>9999) { printf("%d\n",5); printf("%d,%d,%d,%d,%d\n",x/10000,(x%10000)/1000,(x%1000)/100,(x%100)/10,x%10); int a; printf("%d",a=x/10000+(x%10000)/1000*10+(x%1000)/100*100+(x%100)/10*1000+x%10*10000); } } }
[此贴子已经被作者于2019-10-17 21:14编辑过]
#include <stdio.h> int main( void ) { unsigned x; if( scanf("%u",&x)!=1 || x==0 || x>=100000 ) // 不多于5位的正整数 return 1; unsigned n = 0; unsigned r = 0; for( unsigned t=x; t!=0; t/=10 ) { ++n; r = r*10 + t%10; } printf( "%u\n", n ); for( unsigned i=0,t=r; i!=n; ++i,t/=10 ) printf( "%u%c", t%10, ",\n"[i+1==n] ); printf( "%u\n", r ); }
#include<iostream> #include<cstdio> #define ll long long using namespace std; void doit(long long n) { if(n==0) return; cout<<n%10; doit(n/10); } int main() { ll n; cin>>n; doit(n); return 0; }
#include<iostream> #include<cstdio> #define ll long long using namespace std; ll ans=0; void doit(long long n) { if(n==0) return; ans=ans*10+n%10; doit(n/10); } int main() { ll n; cin>>n; doit(n); cout<<ans; return 0; }
#include<iostream> #include <algorithm> using namespace std; int main() { int iNum = 12345; char s[6];//因为最大位5位数,字符串最后以'/0'结尾,所以设置位6. string sNum = itoa(iNum, s,10); reverse(sNum.begin(), sNum.end()); int iResNum = atoi(sNum.c_str()); //cout << iResNum << endl; for (int i = 0;i<sNum.size();i++) { if (i < sNum.size()-1) cout << sNum[i] << ","; else cout << sNum[i]; } }
[此贴子已经被作者于2019-10-20 11:38编辑过]