【发布时间】:2020-01-16 20:40:34
【问题描述】:
我对这样的任务有疑问:
编写一个程序,找出这样一对数字 x 和 y,它们的和等于 n。此外,这对数字还应满足以下条件:
- 数字 x 至少有 2 位,
- 数字 y 比数字 x 少一位。
N 来自
对于数字 80,输出应如下所示:
79 + 1 = 80
78 + 2 = 80
77 + 3 = 80
76 + 4 = 80
75 + 5 = 80
74 + 6 = 80
73 + 7 = 80
72 + 8 = 80
71 + 9 = 80
我编写了一个在大多数情况下都有效的代码,但是在测试数字 100000 时系统拒绝了解决方案,因为程序没有找到这样的对 - 测试需要 9000 个这样的对。我不明白出了什么问题,因为我认为该程序还可以。我想请教一些建议。
我的代码:
#include <stdio.h>
#include <math.h>
int digit_count(int n)
{
int i = 0;
while (n > 0)
{
n /= 10;
i++;
}
return i;
}
int breakdown(int n)
{
long n_digits = digit_count(n), check = 0, count = 1;
long double y_max = pow(10, (n_digits - 1)) - 1, y_min = (pow(10, (n_digits - 2)));
for (int i = (int)y_min; i <= (int)y_max; i++)
{
if (digit_count(n - i) >= 2 && digit_count(i)+1 == digit_count(n - 1))
{
printf("%d + %d = %d %d\n", n - i, i, n, count);
check = 1;
count++;
}
}
if (check == 0)
{
printf("Nothing to show.");
}
return 0;
}
int main(void)
{
unsigned int n = 0;
printf("Podaj N: ");
if (1 != scanf("%u", &n))
{
printf("Incorrect input");
return 1;
}
if (n > 1000000 || n < 10)
{
printf("Incorrect input");
return 1;
}
breakdown(n);
return 0;
}
PS:我忘了说 count 变量在这里只是为了调试
【问题讨论】:
-
digit_count(n-1)应该是digit_count(n-i)? -
我建议阅读this article 了解调试代码的技术。您应该手动找到一对符合所有条件且总和为 100000 的数字
x和y。然后您可以调试您的代码以找出它无法找到该对的原因。 -
long double y_max = pow(10, (n_digits - 1)) - 1,你的代码不需要双打。我认为在这种情况下,它们会使您的代码变慢。只需编写您自己的小int pow10_int(int)函数。for (int i = (int)y_min; i <= (int)y_max; i++)将运行 很多 次。y_max和y_min的值应该是多少?他们是否以某种方式被选中?为什么你从n_digits - 2而不是n_digits / 2开始?the system rejects the solution- “系统”拒绝解决方案,因为没有返回足够的对?digit_count(n - i) >= 2你就不能n - i >= 10吗? -
我认为这个程序是这样工作的:
digit_count(First number)==digit_count(number)==digit_count (second_number)+1 -
不,不要将更正后的代码放在问题中!编写一个答案,说明代码中的问题以及您如何解决它。当您解决自己的问题时,我们鼓励您自行回答(您也可以获得对答案的投票)。
标签: c