zoukankan      html  css  js  c++  java
  • 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;
    }



  • 相关阅读:
    5 November
    31 October
    K-th Path
    P1525 关押罪犯
    dp-棋盘形dp
    P1462 通往奥格瑞玛的道路
    noip2017部分题目
    洛谷orz--尺取法
    树形dp
    最短路练习
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563775.html
Copyright © 2011-2022 走看看