我想请求有关我的硬件的一些帮助。我想我已经很接近弄清楚这个问题了。我们的 CompSci 课程目前正在从学习 Python 转向(入门)C++。由于这两者模糊相似,作为初学者,我们被建议用 Python(我们非常熟悉)编写问题代码,并使用基础知识将其转换为 C++我们刚刚了解到。要解决的问题是一个简单的“给定正整数输入,将 1 到该数字的连续整数相加”。所以一个例子是:
>>Enter a positive integer: 10
>>1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
我试图翻译成 C++ 的 Python 代码(这是成功的)是:
num = int(raw_input("Enter a positive integer: "))
sum = 0
for i in range(1, num):
sum += i
print i, "+",
print num, "=", sum+num
而我不成功的 C++ 代码:
#include <iostream>
using namespace std;
int main()
{
int num;
int sum;
int i;
sum = 0;
cout << "Please enter a positive integer: " << endl;
cin >> num;
for (i=0; 1 <= num; i++)
{
sum = sum + i;
cout << i << "+" << endl;
}
cout << num << "=" << sum + num << endl;
return 0;
}
但输出只是一个从 0 到无穷大的无限、非结束的加法序列,从上到下。更糟糕的是它没有像我想要的那样以直线打印。正如你所看到的,我确实尝试逐字翻译;我以为那是万无一失的。我的 for 循环一定有问题。由于 C++ 没有像 Python 那样有自己的“范围”类,因此我认为中间条件语句(“1 <= num;”)将充当范围。为什么我的“=”符号没有打印出来?我不明白为什么它在达到“num”时不会终止。你认为你能帮忙吗?我预先感谢您的答复。
I'd like to request some help on my HW. I think I'm really close to figuring this out. Our CompSci class is currently shifting from learning Python to (introductory) C++. Since the two are vaguely similar, we've been advised, since we're beginners, to code the problem in Python (which we're very familiar with) and to translate it into C++ using the basics we just learned. The problem to solve is a simple "add the consecutive integers from 1 to that number, given a positive integer input." So an example would be:
>>Enter a positive integer: 10
>>1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
The Python code (this was successful) that I'm attempting to translate into C++ is:
num = int(raw_input("Enter a positive integer: "))
sum = 0
for i in range(1, num):
sum += i
print i, "+",
print num, "=", sum+num
And my unsuccessful C++ code:
#include <iostream>
using namespace std;
int main()
{
int num;
int sum;
int i;
sum = 0;
cout << "Please enter a positive integer: " << endl;
cin >> num;
for (i=0; 1 <= num; i++)
{
sum = sum + i;
cout << i << "+" << endl;
}
cout << num << "=" << sum + num << endl;
return 0;
}
But the output is simply an infinite, non-ending addition sequence from 0 to infinity, going top to bottom. Even worse is that it did not print in a straight line like I want it. As you can see, I quite literally tried to translate it word-for-word; I thought that'd be foolproof. Something must be wrong with my for loop. Since C++ doesn't have a class of its own for "range" like Python does, I thought the middle condition statement ("1 <= num;") would act as the range. Why didn't my "=" sign print out? And I don't understand why it won't terminate when it reaches "num." Think you can help? I thank you in advance for the replies.
发布评论
评论(5)
固定代码:
Fixed code:
这:
应该是:
This:
should be:
试试这个。
try this.
我不太了解 Python,但代码
看起来非常相似
,或者可能
看起来更像 C++?
I don't really know Python, but the code
looks really similar to
or is it possibly
which looks more like C++?
C++中的循环比Python最基本,for循环更简单,它基于三个表达式:初始化表达式、循环测试表达式和计数表达式。特别是您的代码中的错误是测试表达式。请记住,如果测试表达式为 true,则执行循环。如果条件
i 为 true,则需要循环。你的循环永远不会结束,因为
num
总是>= 1
,或者像你写的那样1 <= num
总是。要打印一行上的所有内容,请不要使用
endl
loop in c++ are most basic than python, the for loop is more simpler, it is based on the three expression: initializer expression, the loop test expression, and the counting expression. In particular what is wrong in your code is the test expression. Remember that the loop is executed if the test expression is true. You need to loop if the condition
i<num
is true. Your loop is never ending becausenum
is always>= 1
, or as you wrote1 <= num
always.To print everythig on a line don't use
endl