gpt4 book ai didi

c - 我的程序反转给定的整数有什么问题?

转载 作者:行者123 更新时间:2023-11-30 16:59:54 25 4
gpt4 key购买 nike

Codechef 不接受以下代码。谁能告诉我其中有什么问题,因为我无法指出任何错误?

//This program reverses a given integer.

#include<stdio.h>

int main(void)
{
int t,n,l;

scanf("%d",&t);

while(t--){
scanf("%d",&n);

while(n>0){
l=n%10;
n=n/10;
printf("%d",l);
}

printf("\n");
}


return 0;

}

t 是否。测试用例。
n 是输入整数。
l 是一些用于完成打印作业的随机变量。

该程序应该仅反转正整数。

Example:- Input - 1234   
Output - 4321

最佳答案

尝试这个代码(我同意 molbdnilo 的评论):

//This program reverses a given integer.

#include<stdio.h>
#include <math.h>

int main(void)
{
int i,j,t,n,l,r = 0;
char d[1024]; // stock digits

scanf("%d",&t);

while(t--){
scanf("%d",&n);
i = 0;
while(n>0){
l=n%10;
n=n/10;
d[i] = l;
i++;
}

for(j=0;j<i;j++)
r+= d[i-j-1] * pow(10,j); // r is the reversed number
printf("%d\n",r);
}


return 0;

}

关于c - 我的程序反转给定的整数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37918139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com