gpt4 book ai didi

c - c语言中如何除999?

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

我有一个任务,要求我将 1 到 998 之间的数字除以 999。但是,它要求我提供 3 位小数的输出。如果重复,则用 "..." 输出重复的值 例如:

Input :
998
Output :
0.998... // because 998/999 = 0.998998998998998....

Input :
1
Output :
0.001... // because 001 is repeated just output "0.001..."

我的问题是,“也许”我的代码无法检测哪个是重复的,哪个不是。所以这段代码中有很多测试用例是错误的。

这是我的代码:

int main()
{
int x1,x2,x3;
double a,b,c = 0;
scanf("%d",&x1); getchar();
scanf("%d",&x2); getchar();
scanf("%d",&x3); getchar();
a = (double)x1/999;
a = (int)(a*1000) / 1000.0;

b = (double)x2/999;
b = (int)(b*1000) / 1000.0;

c = (double)x3/999;
c = (int)(c*1000) / 1000.0;

printf("%.3lf...\n", a);
printf("%.3lf...\n", b);
printf("%.3lf...\n", c);
getchar();
return 0;
}

在线评委只给我50分。哪些测试用例是错误的。

最佳答案

#include <stdio.h>

int main(void)
{
int x;
scanf("%d",&x);

return (x%111)? !printf("0.%03d...\n", x) : !printf("0.%d...\n", x/111);
}

输入:

57
222

输出:

0.057...
0.2...

关于c - c语言中如何除999?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58713349/

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