1、编写一个函数fun(n),求任意整数的逆序数, 例如:当n=1234时,函数值为4321。
答案:7 悬赏:70 手机版
解决时间 2021-01-29 01:29
- 提问者网友:王者佥
- 2021-01-28 04:26
1、编写一个函数fun(n),求任意整数的逆序数, 例如:当n=1234时,函数值为4321。
最佳答案
- 五星知识达人网友:举杯邀酒敬孤独
- 2021-01-28 05:46
#include
int main()
{
int num;
printf("please enter a number:");
scanf("%d", &num);
int new_num = num;
int k = 0;
while(new_num>0)
{
k++;
new_num = new_num/10;
}
int a[k];
int old_num = num;
int j = 0;
while(old_num>0)
{
a[j++] = old_num%10;
old_num = old_num/10;
}
int new_a = a[0];
int i;
for(i=1; i {
new_a = new_a*10 + a[i];
}
printf("%d", new_a);
return 0;
}
int main()
{
int num;
printf("please enter a number:");
scanf("%d", &num);
int new_num = num;
int k = 0;
while(new_num>0)
{
k++;
new_num = new_num/10;
}
int a[k];
int old_num = num;
int j = 0;
while(old_num>0)
{
a[j++] = old_num%10;
old_num = old_num/10;
}
int new_a = a[0];
int i;
for(i=1; i
new_a = new_a*10 + a[i];
}
printf("%d", new_a);
return 0;
}
全部回答
- 1楼网友:行雁书
- 2021-01-28 09:14
function m=fun(n)
m=num2str(n);
m(1:end)=m(end:-1:1);
m=num2str(n);
m(1:end)=m(end:-1:1);
- 2楼网友:蓝房子
- 2021-01-28 08:50
#include
int calc(int n)
{
int i=0;
int rem = n;
int re=0;
while( n != 0)
{
i++;
n = n/10;
}
int j;
for(j=1;j<=i;j++)
{
re = 10*re + rem%10;
rem = rem/10;
}
return re;
}
int main()
{
int n;
int re;
scanf("%d",&n);
re = calc(n);
printf(" %d",re);
getchar();
return 0;
}
int calc(int n)
{
int i=0;
int rem = n;
int re=0;
while( n != 0)
{
i++;
n = n/10;
}
int j;
for(j=1;j<=i;j++)
{
re = 10*re + rem%10;
rem = rem/10;
}
return re;
}
int main()
{
int n;
int re;
scanf("%d",&n);
re = calc(n);
printf(" %d",re);
getchar();
return 0;
}
- 3楼网友:风格不统一
- 2021-01-28 08:38
#include
#include
int
fun(int in)
{
int result = 0, tmp;
while(1)
{
result *= 10;
tmp = in%10;
if(in > 0)
{
result += tmp;
in /= 10;
if(in == 0)
{
break;
}
}
}
return result;
}
void main()
{
int i = 0;
printf("please input a number:");
scanf("%d", &i);
i = fun(i);
printf("\nafter convert i = %u\n", i);
}
#include
int
fun(int in)
{
int result = 0, tmp;
while(1)
{
result *= 10;
tmp = in%10;
if(in > 0)
{
result += tmp;
in /= 10;
if(in == 0)
{
break;
}
}
}
return result;
}
void main()
{
int i = 0;
printf("please input a number:");
scanf("%d", &i);
i = fun(i);
printf("\nafter convert i = %u\n", i);
}
- 4楼网友:大漠
- 2021-01-28 07:50
#include
int fun(int);
void main()
{
int n;
int i;
for(i=0;i<3;i++) {
scanf("%d",&n);
printf("%d ",fun(n));
}
while(1);
}
int fun(int n)
{
int result = 0;
int sig = 1;//代表正负
if(n<0) {
n = n * (-1);
sig = -1;
}
while(n>0){
result = result*10 + n%10;
n = n/10;
}
return result*sig;
}
- 5楼网友:渊鱼
- 2021-01-28 07:19
#include
#include
int fun(int a){
char arr[32];
char rarr[32];
int it;
memset(arr, 0x00, sizeof arr);
memset(rarr, 0x00, sizeof rarr);
sprintf(arr,"%d\0",a);
char *p =arr+strlen(arr)-1;
char *pp =rarr;
while( *pp++ = *p-- );
it = atoi(rarr);
return it;
}
int main(){
int tst=12345;
int ts = fun(tst);
printf("The input Nbr:%d , The out put Nbr:%d\n",tst,ts);
}
#include
int fun(int a){
char arr[32];
char rarr[32];
int it;
memset(arr, 0x00, sizeof arr);
memset(rarr, 0x00, sizeof rarr);
sprintf(arr,"%d\0",a);
char *p =arr+strlen(arr)-1;
char *pp =rarr;
while( *pp++ = *p-- );
it = atoi(rarr);
return it;
}
int main(){
int tst=12345;
int ts = fun(tst);
printf("The input Nbr:%d , The out put Nbr:%d\n",tst,ts);
}
- 6楼网友:第幾種人
- 2021-01-28 06:52
输入数字:34555678
87655543
Press any key to continue
#include
#include
void Res(int n)
{
int i;
char str1[20] ;
sprintf(str1, "%d ",n);
for(i=strlen(str1);i>0;i--)
printf("%c",str1[i-2]);
printf("\n");
}
void main()
{
int n;
printf("输入数字:");
scanf("%d",&n);
Res(n);
}
87655543
Press any key to continue
#include
#include
void Res(int n)
{
int i;
char str1[20] ;
sprintf(str1, "%d ",n);
for(i=strlen(str1);i>0;i--)
printf("%c",str1[i-2]);
printf("\n");
}
void main()
{
int n;
printf("输入数字:");
scanf("%d",&n);
Res(n);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯