HDU1597 find the nth digit【模拟】

来源:互联网 发布:mac 压缩文件夹 编辑:程序博客网 时间:2024/05/16 18:45

问题链接: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;}



原创粉丝点击