以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  荤菜求助:给一个不多于5位的正整数  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=43378)


--  作者:荤菜
--  发布时间:2/21/2007 9:27:00 PM

--  荤菜求助:给一个不多于5位的正整数
给一个不多于5位的正整数,1.求出他是几位数2.打印每一位数.3.逆序打印.
main()
{
long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
printf("please input a number(0--99999):");
scanf("%ld",&num);
if(num>9999)
place=5;
else if(num>999)
  place=4;
else if(num>99)
place=3;
else if(num>9)
place=2;
else place=1;
printf("place=%d\n",place);
printf("every number is:");
ten_thousand=num/10000;
thousand=(num-ten_thousand*10000)/1000;
hundred=(num-ten_thousand*10000-thousand*1000)/100;
ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=num-ten_thousand*10000-thousand*10000-hundred*100-ten*10;
switch(place)
{case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv);
printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);
break;
case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d",hundred,ten,indiv);
printf("%d%d%d\n",indiv,ten,hundred);
break;
case 2:printf("%d,%d",ten,indiv);
printf("%d%d\n",indiv,ten);
break;
case 1:printf("%d",indiv);
printf("%d\n",indiv);
break;
}
getch();
}
当我输入123时答案正确,当我随意输入小于5个数时答案屡屡出错,请各位高手解答一下,谢谢.
--  作者:Limit
--  发布时间:2/26/2007 4:49:00 PM

--  
an erreur in :
indiv=num-ten_thousand*10000-thousand*10000-hundred*100-ten*10;
change it to :
indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;
--  作者:荤菜
--  发布时间:3/2/2007 11:02:00 AM

--  
谢谢了
--  作者:michaelscofield
--  发布时间:3/25/2007 9:32:00 AM

--  
main( )
{
long a,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;/*分解出万位*/
b=x%10000/1000;/*分解出千位*/
c=x%1000/100;/*分解出百位*/
d=x%100/10;/*分解出十位*/
e=x%10;/*分解出个位*/
if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
  else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
    else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
      else if (e!=0) printf(" there are 1,%ld\n",e);
}
这个绝对是正确的,我试过了
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
61.035ms