小程序如下:
---------
# include <stdio.h>
int main ()
{
int i/*(number)*/,place,ten_thousand,thousand,hundred,ten,individual;
printf ("please enter an integer:");
scanf ("%d",i);
while (i>99999)
{
printf ("wrong!\nenter again:");
scanf ("%d",&i);
}
//if (i>10000) //wrong!
if (i>9999)
place=5;
if (i>999)
place=4;
if (i>99)
place=3;
if (i>9)
place=2;
else
place=1;
printf("位数:%d",place);
printf("每位数字为:");
ten_thousand=i/10000;
thousand=(i-ten_thousand*10000)/1000;
hundred=(i-ten_thousand*10000-thousand*1000)/100;
ten=(i-ten_thousand*10000-thousand*1000-hundred*100)/10;
individual=i-ten_thousand*10000-thousand*1000-hundred*100-ten*10;
switch(place)
{
case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,individual);
printf("逆序数字为:");
printf("%d,%d,%d,%d,%d\n",individual,ten,hundred,thousand,ten_thousand);
break;
case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,individual);
printf("逆序数字为:");
printf("%d,%d,%d,%d,\n",individual,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d\n",hundred,ten,individual);
printf("逆序数字为:");
printf("%d,%d,%d\n",individual,ten,hundred);
break;
case 2:printf("%d,%d\n",ten,individual);
printf("逆序数字为:");
printf("%d,%d,\n",individual,ten);
break;
case 1:printf("%d\n",individual);
printf("逆序数字为:");
printf("%d\n",individual);
break;
}
printf("好累啊!");
return 0;
}
---------
这是怎么回事啊?
前面有一次也越到这种情况的!
------最佳解决方案--------------------
int i/*(number)*/,place,ten_thousand,thousand,hundred,ten,individual;
printf ("please enter an integer:");
scanf ("%d",i);
改成scanf ("%d",i&);
------其他解决方案--------------------
#include<stdio.h>
#include<math.h>
void main(){
int a[10];//数组存放高位至低位
int num,temp,j,i=1,data_length;//data_length表示该数字是几位数
printf("请输入一个数\n");
scanf("%d",&num);
temp=num;
while(temp>10){
i++;
temp=temp/10;
}//这是求数的位数的
data_length=i;
printf("该数字是%d位数\n",data_length);
printf("逆序为\n");
for(j=0;j<data_length;j++){
a[j]=num/(int)pow(10,i-1);
num=num-a[j]*pow(10,i-1);
i--;
}//这个是用来求各个位数的,如12345 先12345/10000=1 ,num=12345-1*10000=2345 ,再以此类推
i=data_length;
while(i>0){
printf("%d ",a[i-1]);
i--;
}
printf("\n");
}
------其他解决方案--------------------
這一段
for(j=0;j<data_length;j++){
a[j]=num/(int)pow(10,i-1);
num=num-a[j]*pow(10,i-1);
i--;
}//这个是用来求各个位数的,如12345 先12345/10000=1 ,num=12345-1*10000=2345 ,再以此类推