添加输入中的连续整数(从 Python 翻译为 C++)

发布于 2024-11-06 12:40:50 字数 1213 浏览 0 评论 0 原文

我想请求有关我的硬件的一些帮助。我想我已经很接近弄清楚这个问题了。我们的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦忆晨望 2024-11-13 12:40:50

固定代码:

#include <iostream>
using namespace std;

int main()
{
    int num;
    int sum;
    int i;
    sum = 0;
    cout << "Please enter a positive integer: " << endl;
    cin >> num;
    // Here you had 1 <= num which was always true for positive num
    // and it did not depend on value of i.
    for (i = 1; i < num; ++i)
        {
        sum = sum + i;
        cout << i << "+";  // Here you had endl which produced newline characters.
        }
    cout << num << "=" << sum + num << endl;-
    return 0;
}

Fixed 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;
    // Here you had 1 <= num which was always true for positive num
    // and it did not depend on value of i.
    for (i = 1; i < num; ++i)
        {
        sum = sum + i;
        cout << i << "+";  // Here you had endl which produced newline characters.
        }
    cout << num << "=" << sum + num << endl;-
    return 0;
}
半衾梦 2024-11-13 12:40:50

这:

for (i=0; 1 <= num; i++)

应该是:

for (i=0; i <= num; i++)

This:

for (i=0; 1 <= num; i++)

should be:

for (i=0; i <= num; i++)
彩扇题诗 2024-11-13 12:40:50

试试这个。

#include <iostream>
using namespace std;

int main()
{
    int num;
    int sum;
    int i;
    sum = 0;
    cout << "Please enter a positive integer: ";
    cin >> num;
    for (i=0; i < num; i++)
        {
        sum = sum + i;
        cout << i << " + ";
        }
    cout <<num << " = " << sum+num << endl; 
    return 0;
}

try this.

#include <iostream>
using namespace std;

int main()
{
    int num;
    int sum;
    int i;
    sum = 0;
    cout << "Please enter a positive integer: ";
    cin >> num;
    for (i=0; i < num; i++)
        {
        sum = sum + i;
        cout << i << " + ";
        }
    cout <<num << " = " << sum+num << endl; 
    return 0;
}
老街孤人 2024-11-13 12:40:50

我不太了解 Python,但代码

for i in range(1, num): 

看起来非常相似

for (int i=1; i <= num; ++i) 

,或者可能

for (int i=1; i != num; ++i) 

看起来更像 C++?

I don't really know Python, but the code

for i in range(1, num): 

looks really similar to

for (int i=1; i <= num; ++i) 

or is it possibly

for (int i=1; i != num; ++i) 

which looks more like C++?

战皆罪 2024-11-13 12:40:50

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 because num is always >= 1, or as you wrote 1 <= num always.

To print everythig on a line don't use endl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文