无法得到2个数的倍数之和 [英] Can't get the sum of multiples of 2 numbers

查看:94
本文介绍了无法得到2个数的倍数之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到用户给出的2个数字的倍数之和但结果数字不正确,这是我想要做的一个例子:

试图找到总和例如,如果我们列出10以下3或5的倍数的所有自然数,我们得到3,5,6和9的总和。这些倍数是23.

也许我的逻辑出现了问题,但我认为2 while循环在break之前再运行一次。所以我怎样才能使while循环退出之前第一个倍数是否大于用户给出的最大数量?



我尝试了什么:



I am trying to get the sum of multiples of 2 numbers given by the user but the resulted number is incorrect,here's an example of what i am trying to do:
Trying to Find the sum of the multiples of 19 or 17 under 747
For example, if we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Maybe there's something wrong with my Logic,but i think the 2 while loops are being run one more time before breaking.so how can i make the while loops exit before the sum of the first multiples are more than the max number given by the user?

What I have tried:

#include <stdio.h>
void main()
{
	int n1,n2,n3, inc1,inc2,sumt,sum1,sum2,t1,t2;
	inc1 = 1;
	inc2 = 1;
	sum1 = 0;
	sum2 = 0;
	sumt = 0;
	t1 = 0;
	t2 = 0;
	printf("type first number: ");
	scanf("%d", &n1);
	printf("\ntype second number: ");
	scanf("%d",&n2);
	printf("\ntype max number: ");
	scanf("%d", &n3);
	
	while (sum1 < n3)
	{
		t1 = n1*inc1;	
		sum1 = sum1 + t1;
			inc1++;
			
			}
	while (sum2 < n3)
	{
		t2 = n2*inc2;
		sum2 = sum2 + t2;
		inc2++;
		
	}
	sumt = sum1 + sum2;
	printf("\n the sum of the multiples %d", sumt);

	getchar();
	getchar();
}

推荐答案

此时,调试器变得非常方便! :笑:

在断行点上放置一个断点:

At this point, the debugger becomes really handy! :laugh:
Put a breakpoint on the line:
while (sum1 < n3)

并运行您的程序。输入样品编号为3,5和10,如样品中所示。

单步执行,查看t1和sum1的内容,它将执行3次:3 * 1,3 * 2和3 * 3总和在sum1中为18.这是正确的:3 + 6 + 9 = 18.

所以第一个循环看起来像是在工作。

时间对于第二个循环...

使用调试器执行相同的操作,并执行多少次?两次。一次为5 * 1,一次为5 * 2,总共给你15次。

这是错的 - 它比它应该更大,因为它包括总数中的10个。 />


那么为什么当第一个给出正确的结果时呢?答案很简单:运气。纯洁,盲目的运气!

为什么运气好?因为两个循环都是错误的,并且两个循环都会给出相同的错误数字。

为什么?简单:结束测试是错误的。当你得到一个大于限制数的倍数时,你没有停止,当总计大于限制时你停止。

将时间更改为:

and run your program. Enter your sample numbers as 3, 5 and 10 as in your sample.
Step through, looking at the content of t1 and sum1 and it will execute 3 times: 3*1, 3*2 and 3*3 leaving you with a total in sum1 of 18. Which is correct: 3 + 6 + 9 = 18.
So the first loop looks like it's working.
Time for the second loop...
Do the same thing with the debugger, and it executes how many times? Twice. Once for 5*1, and once for 5*2, giving you a total of 15 in sum2.
That's wrong - it's bigger than it should be because it included the 10 in the total.

So why did it do that when the first one gave the right result? The answer is simple: luck. Pure, blind luck!
Why luck? Because both loops are wrong, and both loops will give the same wrong number.
Why? Simple: the "end test" is wrong. You aren't stopping when you get a a multiple that is bigger than the limit number, you are stopping when the total is greater than the limit.
Change the whiles to:

while (n1 * inc1 < n3)



and

while (n2 * inc2 < n3)

并且你会得到正确的结果。

但是......有一种更好的方式,根本不涉及乘法。你能弄清楚它是什么吗?

and you will get the right result.
But...there is a "better way" that doesn't involve multiplication at all. Can you work out what it is?


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



检查问题陈述:

n3 n1 n2 的倍数的最大值或su的最大值m?

3 5 20 ,注意 15 是两者的倍数,并且必须只计算一次。

使用调试器检查您的代码正在执行所请求的内容。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Check the statement of the problem:
n3 is the maximum for the multiples of n1 and n2 or maximum of the sum ?
For 3 and 5 under 20, pay attention to the fact that 15 is multiple of both and must be count only once.
Use the debugger to check that your code is doing what is requested.


这篇关于无法得到2个数的倍数之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆