INT_cool_boy 2021-08-09 12:57 采纳率: 100%
浏览 58
已结题

杭电oj 1001题 不知道自己哪里出错了

杭电oj 1001题

img
#include
using namespace std ;

#include <iostream>
using namespace std ;

int main (){
    int num2 ;
    int sum = 0 ;
    int num = 0 ;
    while ( cin >> num2 ){
        while( num <= num2 ){
            sum += num ;
            num++ ;
        }
        cout << sum << endl << endl ;
    }
    system ("pause") ;
    return 0 ;
}

我这提交的时候是错的
我不知道我这哪里有问题

  • 写回答

1条回答 默认 最新

  • codeaftercode 2021-08-09 13:03
    关注

    system ("pause") ;这行删掉
    第一层while循环里,sum、num没有归零

    #include <iostream>
    using namespace std ;
    int main (){
        int num2 ;
        while ( cin >> num2 ){
            int sum=0;
            int num=0;
            while( num <= num2 ){
                sum += num ;
                num++ ;
            }
            cout << sum << endl << endl ;
        }
    
        return 0 ;
    }
     
    
    

    另外,acm不仅要求编程技巧,同时也要求数学知识。这题可以用等差数列求和公式:s=n(n+1)/2,这样执行时间更短。

    #include <iostream>
    using namespace std ;
    int main (){
        int n ;
        while ( cin >> n ){
            cout << n*(n+1)/2 << endl << endl ;
        }
    
        return 0 ;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月17日
  • 已采纳回答 8月9日
  • 创建了问题 8月9日

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示