0
点赞
收藏
分享

微信扫一扫

codeforces - 1455B - 思维题

原题链接:https://codeforces.com/problemset/problem/1455/B

翻译:

思路:这题其实就是找规律 ,你可以自己尝试着列一下1 ~ 15的距离要跳的次数,你就会发现规律。

代码实现:

#include <bits/stdc++.h>

using namespace std;

const int x = 1e6;
int jl[200000] = {0};
int main()
{
    int T,i = 0;
    cin >> T;
    for(i = 1; jl[i] < 2*x; i++)
    {
        jl[i] = jl[i - 1] + i;
    }

    while(T--)
    {
        int n,ans = 0;
        scanf("%d",&n);
        if(n == 1) ans = 1;
        if(n == 2) ans = 3;
        if(n == 3) ans = 2;
        if(n > 3){
            for(int j = 3; j < i; j++){
                if(n == jl[j])
                {
                    ans = j;break;
                }
                else if(n > jl[j - 1] && n < jl[j]){
                    if(n == jl[j] - 1){
                            ans = j + 1;
                            break;
                            }
                    else{
                        ans = j;
                        break;
                        }
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}
举报

相关推荐

0 条评论