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;
}
 
                    
                 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号