给出一个不多于5位的整数,要求 1、求出它是几位数 2、分别输出每一位数字 3、按逆序输出各位数字,例如原数为321,应输出123
怎么写
#include <stdio.h> #include <stdlib.h> int main() { int x, a, b, c, d, e; //输入一个整数 printf("Please enter number: "); scanf("%d", &x); //以下是判断整数是否多于5位,多于判断出错,重新输入 while (x < 0 || x > 99999){ printf("Error! Retry!\nPlease enter number: "); scanf("%d", &x); } //以下判断出该数是几位数 a = x / 10000; b = x / 1000; c = x / 100; d = x / 10; e = x - d * 10; if (a >= 1 && a <= 9){ printf("%d is 5 bits!\n", x); printf("%d %d %d %d %d\n", a, b-a*10, c-b*10, d-c*10, e); printf("%d %d %d %d %d\n", e, d-c*10, c-b*10, b-a*10, a); } else if (b >= 1 && b <= 9){ printf("%d is 4 bits!\n", x); printf("%d %d %d %d\n", b, c-b*10, d-c*10, e); printf("%d %d %d %d\n", e, d-c*10, c-b*10, b); } else if (c >= 1 && c <= 9){ printf("%d is 3 bits!\n", x); printf("%d %d %d\n", c, d-c*10, e); printf("%d %d %d\n", e, d-c*10, c); } else if (d >= 1 && d <= 9){ printf("%d is 2 bits!\n", x); printf("%d %d\n", d, e); printf("%d %d\n", e, d); } else{ printf("%d is 1 bits!\n", x); printf("%d\n", x); } system("pause"); return 0; }
#include <stdio.h> int main( void ) { int n; scanf( "%d", &n ); unsigned count = 0; char s[40] = { '-', '0', 0 }; char* p = s; for( unsigned m=n<0?-n:+n; m; m/=10 ) { ++count; if( p!=s || m%10!=0 ) *++p = m%10+'0'; } printf( "%u, %s\n", count==0?1:count, s+(n>=0) ); }