HDU1597 find the nth digit【模拟】

问题链接HDU1597 find the nth digit

问题描述参见上文。

问题分析

计算还是模拟是一个值得思考的问题。

怎么算?找不到规律,找不到一个好的算法时,还是模拟吧。不过需要不是太花时间。

程序说明

(略)

参考链接:(略)



AC的C++语言程序:

#include <iostream>

using namespace std;

int solve(int n)
{
    // 模拟
    int i = 1;
    while(n > i) {
        // 每次减去的i之和=前i项之和,当n>i则n在第i项中并且n已经被减去了之前i-1项的和
        n -= i;     
        i++;
    }
    n -= (n / 9) * 9;   // 其中1-9是循环的,所以要重复减去9(不能为0,0的话改为9)
    n = (n ? n : 9);
    return n;
}

int main()
{
    int k, n;

    cin >> k;
    while(k--) {
        cin >> n;

        cout << solve(n) << endl;
    }
    return 0;
}



原文地址:https://www.cnblogs.com/tigerisland/p/7563775.html