总和数字总线循环 [英] sum numbers busing while loop
本文介绍了总和数字总线循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我想把所有3的倍数加在1到100之间
这就是我试过的东西
hi guys i want to sum all the multiples of 3 between 1 and 100
this is what i tried
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
for (int i=1 ;i<=100; i++)
{
cout << i+3<< " ";
}
return 0;
}
推荐答案
我会做一些更简单的事情 - 但因为这是你的功课,我赢了;不给你代码!
而不是那样,使用一个在1和100/3之间运行的for循环。在循环外声明一个名为sum的变量,并将其设置为零。
然后在循环内,每次将值i * 3添加到sum。
循环后,打印sum的值。
I'd do something simpler - but since this is your homework, I won;t give you the code!
Instead of that, use a for loop that runs with i between 1 and 100/3. Declare a variable called "sum" outside the loop, and set it to zero.
Then inside the loop, add the value "i * 3" to "sum" each time.
After the loop, print the value of "sum".
有很多种方法。以下是一些:
1.将数字3,6,9,...添加到99.
2.添加数字1到33并乘以3.
3. 3 *(33 * 33 + 33)/ 2
4. 1683
我已经添加了这个解决方案作为对Griff解决方案的赞美。如果你选择#3或#4,请确保你理解所涉及的数学,并准备解释你对教授的答案。
There are a number of ways. Here are a few:
1. add the numbers 3, 6, 9, ... to 99.
2. add the numbers 1 to 33 and multiply by 3.
3. 3 * (33*33 + 33)/2
4. 1683
I've added this solution as a compliment to Griff's solution. If you pick #3 or #4, make sure you understand the math involved and are prepared to explain your answer to the professor.
这篇关于总和数字总线循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文