DALAO们求助!AC后包关的(求回关)

P2669 [NOIP2015 普及组] 金币

zyx13765814016 @ 2024-10-07 18:30:42

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,a=0,b=0;;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            a+=i;
            b+=1;
            if(b==n){
                break;
            }
        }
    }

    cout<<a;
    return 0;
}

by imfbust @ 2024-10-07 18:36:14

那个break后只是跳出内层循环,但最外层循环没跳出,b会继续增加


by imfbust @ 2024-10-07 18:37:39

要么你在内层循环结束时在判断一次b==n,要么你判断完后直接输出a然后return 0;


by imfbust @ 2024-10-07 18:38:05

@zyx13765814016


by THE_SUPER_WING @ 2024-10-07 18:42:26

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,a=0,b=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            a+=i;
            b+=1;
            if(b==n){
                cout<<a;
                return 0;
            }
        }
    }
    return 0;
}

改好了


by easy42 @ 2024-10-07 18:53:20

@zyx13765814016 原因:break只会跳出一层循环


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,a=0,b=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            a+=i;
            b+=1;
            if(b==n){
                cout<<a;
                return 0;
            }
        }
    }
    return 0;
}

by zyx13765814016 @ 2024-10-07 19:01:50

谢谢DALAO,已关


by zyx13765814016 @ 2024-10-07 19:04:47

求回关


by abssortpow1145145 @ 2024-10-11 20:42:54


#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int k,sum=0,day=0;
    cin >> k;
    for(int i=1;1>0;i++)
    {
        for(int j=1;j<=i;j++)
        {
            sum+=i;
            day++;
            if(day==k)
            {
                cout << sum;
                return 0;
            }
        }
    }
}

|