Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

用C++语言帮我写一个计算从1加到1000的程序

  1. User

    用C++语言帮我写一个计算从1加到1000的程序

  2. Assistant

    您好,下面是用C++语言写的计算从1加到1000的程序:

    #include <iostream>
    
    int main() {
        int sum = 0;  // 定义变量存储和
        for (int i = 1; i <= 1000; i++) {
            sum += i;  // 累加每一个数
        }
        std::cout << "从1加到1000的和为:" << sum << std::endl;  // 输出结果
        return 0;
    }
    

    以上代码中,使用了for循环来累加从1到1000的每一个数,并将结果存储在sum变量中,最后输出结果。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.