请大神帮忙看下数字倒置的问题。。
自己用do while做的数字倒置的练习,但是打印结果是错的,请大神赐教,源码如下:#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int getwei(int num)
{
int inum = num;
int wei = 0;
do{
wei++;
inum /= 10;
} while (inum);
return wei;
}
int res(int i)
{
int res = 1;
int j = 0;
do{
res *= 10;
j++;
} while (j < i);
return res;
}
void main()
{
int num;
printf("输入一个数字:");
scanf("%d", &num);
int inum = num;
int wei = getwei(num);
printf("位数是:%d", wei);
int shu;
int i=0;
int last = 0;
do{
shu = inum % 10;
inum /= 10;
printf("\nshu=%d", shu);
printf("\ni=%d", i);
last += shu*res(wei - 1 - i);
i++;
} while (i < wei);
printf("\n结果是:%d",last);
system("pause");
} 请问能改成代码模式吗 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int getwei(int num)
{
int inum = num;
int wei = 0;
do{
wei++;
inum /= 10;
} while (inum);
return wei;
}
int res(int i)
{
int res = 1;
int j = 0;
do{
res *= 10;
j++;
} while (j < i);
return res;
}
void main()
{
int num;
printf("输入一个数字:");
scanf("%d", &num);
int inum = num;
int wei = getwei(num);
printf("位数是:%d", wei);
int shu;
int i=0;
int last = 0;
do{
shu = inum % 10;
inum /= 10;
printf("\nshu=%d", shu);
printf("\ni=%d", i);
last += shu*res(wei - 1 - i);
i++;
} while (i < wei);
printf("\n结果是:%d",last);
system("pause");
}
页:
[1]