当前位置: 代码迷 >> C语言 >> 新手求助(switch)
  详细解决方案

新手求助(switch)

热度:549   发布时间:2006-07-22 19:19:12.0
新手求助(switch)
怎么用switch求出一个数字的个位,十位,百位。。。。。?




Thanks!
搜索更多相关的解决方案: switch  

----------------解决方案--------------------------------------------------------

(初学)书上一道题说把一个输入的数字正反2种输出 列如:
1432
2341;
我只能想到求出这个数字的个位,十位,百位。。。然后排列.
帮我看下~谢谢!


----------------解决方案--------------------------------------------------------

你要输入的是几位数?


----------------解决方案--------------------------------------------------------

/*把一个输入的数字正反2种输出 列如:
1432
2341*/

#include <string.h>
main()
{int i,a;
char n[20];
scanf("%s",n);
a=strlen(n);
clrscr();
for(i=0;i<a;i++)
printf("%c",n[i]);
printf("\n");
for(i=a-1;i>=0;i--)
printf("%c",n[i]);
getch();
}

我用TC写的


----------------解决方案--------------------------------------------------------

几位数不重要吧?
谢谢,我想问问不用到For() 能实现吗?


----------------解决方案--------------------------------------------------------

最好不要用for(),你可以用'\'或者‘%’把个、十百位求出来!我只是给你个建议


----------------解决方案--------------------------------------------------------
以下是引用神经塔在2006-7-22 23:36:22的发言:

最好不要用for(),你可以用'\'或者‘%’把个、十百位求出来!我只是给你个建议

你做做!
while(num)
{
printf("%d",num%10);
num=num/10;
}


----------------解决方案--------------------------------------------------------

#include <stdio.h>main()
main()
{
int s,j,gw,bw,sw,qw;
char *p;scanf("%d",&s);
printf("%d",s);
*p=sswitch(j=strlen(s))
{
case 2:gw=s%10;sw=s/10;
printf("%d%d",sw,gw);break;
case 3:gw=s%10;sw=s/10;sw=sw%10;bw=s/100;
printf("%d%d%d",bw,sw,gw);break;
case 4:gw=s%10;sw=s%100;sw=sw/10;bw=s/10;bw=bw%100,bw=bw/10;qw=s/1000;
printf("%d%d%d%d",qw,bw,sw,gw);break;
default:gw=s;

printf("%d",gw); break;


}


----------------解决方案--------------------------------------------------------

错啦,不是这样子,
#include <string.h>
#include <stdio.h >
#include <conio.h >
#include <alloc.h>

void main ()
{
int i , n ;
char * p = ( char * ) malloc(sizeof(char));
scanf("%s" , p) ;
printf( " %s \n " , p ) ;
n = strlen (p) ;
for ( i = n - 1 ; i >= 0 ; i -- )
printf ( "%c" , p[i] ) ;
getch () ;
free(p) ;
}


----------------解决方案--------------------------------------------------------

第 9 楼 错啦,不是这样子,
#include <string.h>
#include <stdio.h >
#include <conio.h >
#include <alloc.h>

void main ()
{
int i , n ;
char * p = ( char * ) malloc(sizeof(char));
scanf("%s" , p) ;
printf( " %s \n " , p ) ;
n = strlen (p) ;
for ( i = n - 1 ; i >= 0 ; i -- )
printf ( "%c" , p[i] ) ;
getch () ;
free(p) ;
}
这个才是正确的


----------------解决方案--------------------------------------------------------
  相关解决方案