Loading

B.Jumps

 

 首先把x一直减去一个cnt++(cnt = 0)。

如果x正好等于0,则答案就是减去的次数很好理解;如果x小于0,不等于-1的话也是减去的次数,可以列出等式,如下图,右边是等于-1则答案需要加一。

 

 

 

#include <bits/stdc++.h>

using namespace std;
#define gogo ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const string YES = "YES";
const string NO = "NO";

void run() {
    int x;
    cin >> x;
    int ans = 0;
    while (true) {
        x -= ++ ans;
        if (x <= 0)
            break;
    }   
    if (x == -1)
        ans ++;
    cout << ans << '\n';
}

int32_t main() {
    gogo;
    
    int tt;
    cin >> tt;
    while (tt --)
        run();

    return 0;
}
View Code

 

posted @ 2023-02-28 13:34  KakaDBL  阅读(23)  评论(0)    收藏  举报