c语言问题 一道题 我是初学者

来源:百度知道 编辑:UC知道 时间:2024/09/28 08:15:28
给一个不多与5位的正整数, 1 求出它是几位数 2 分别打印出每位数字 3 按逆序打印出各位数字 例如 123 应输入321 谢谢

我的是 不过出不来

main()
{long x;
int a,b,c,d,e;
a=(int)(a/10000);
b=(int)(a-b*10000/1000);
c=(int)(a-b*10000/1000-c*1000/100);
d=(int)(a-b*10000/1000-c*1000/100-d*100/10);
e=(int)(a-b*10000/1000-c*1000/100-d*100/10-e*10/1);
printf("%d,%d,%d,%d,%d",a,b,c,d,e);
}

这只是第一问的!!!
高手多包含,错的指出下 谢谢 菜鸟无罪啊!!!!!@
能不能 按我提出的问题 写出下 谢谢!!!!

给你说个简单方法:
int pow;
pow=1+(x>10)+(x>100)+(x>1000)+(x>10000);
这个pow就是x的位数。
打印每位就是:
long n=x;
for(int i=0;i<pow;i++)
{
printf("%d",n%10);
n=n/10;
}

逆序:
n=1;
for(i=0;i<pow-1;i++) n*=10;
for(i=0;i<pow;i++)
{
printf("%d",x/n);
x=x%n;
}

sprintf函数,把int存到一个char数组,然后你想怎么处理就怎么处理.

#include "stdio.h"
#include "string.h"

void main()
{
int a = 12345;
int len = 0, i = 0;
char output[32] = {0};

sprintf(output, "%d", a);
len = strlen(output);//求int数据长度
printf("len = %d\n", len);
//正序打印
for(i=0; i<len; i++)
{
printf("%c ", output[i]);
}
printf("\n");

//逆序打印
for(i=len; i>=0; i--)
{
printf("%c ", output[i]);
}
printf("\n