数字反转
给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 int main(int argc, char** argv) { 5 6 long n; 7 scanf("%ld",&n); 8 9 int i=0,j,k=0,code[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; 10 if(n<0) 11 { 12 k=1; 13 n=n*-1; 14 } 15 while(n>=10) 16 { 17 code[i]=n%10; 18 n=n/10; 19 i++; 20 } 21 code[i]=n; 22 n=0; 23 if(k) 24 printf("-"); 25 for(j=0;j<=i;j++) 26 { 27 if(code[j]>=0) 28 { 29 n=n+code[j]*pow(10,i-j); 30 } 31 } 32 printf("%d",n); 33 return 0; 34 }
posted on 2016-04-26 20:09 foggia2004 阅读(236) 评论(0) 收藏 举报
浙公网安备 33010602011771号