• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            QuXiao

            每天進步一點點!

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              50 隨筆 :: 0 文章 :: 27 評論 :: 0 Trackbacks
            題意:
                就是有這樣一個序列:1 12 123 1234 12345 .....,輸入序列的下標,讓你算出該序列所在下標的字符

            思路:
                一開始想用字符串模擬來做,結果TLE。后來看了discuss,又想了一下,發現可以按規律將序列分成幾段,然后再在每段中查找。具體做法是:先按序列 中數字的位數來分,112123...123456789是一段,1..10 1..11 1..12 ... 1..99是一段,1..100 ... 1..999是一段,以此類推。確定了是在上面的哪一段以后,再按類似的思想確定這個下標是在哪個12345...n的序列,最后判斷是在其中哪個數字的 哪一位。

            代碼:
            #include <iostream>
            #include <cmath>
            using namespace std;

            const long long a[] = {0, 45, 9045, 1395495, 189414495, 2147483648};            //112123...9, 11231234...99, ... 的位數
            const long long b[] = {1, 11, 192, 2893, 38894, 488895, 5888896};                //1, 1234...10, 1234...100, ...的位數

            int digit (int n)
            {
                int ans = 1;
                while ( n / 10 != 0 )
                {
                      n /= 10;
                      ans ++;
                }
                return ans;
            }

            char Pos (int n, long long index)      //在1234...n中找到下標為index的字符
            {
                 long long i, j;
                 long long pos = 0;
                 for (i=1; i<=n; i++)
                 {
                     pos += digit(i);
                     if ( pos >= index )
                        break;
                 }
                
                 pos -= digit(i);               //定位在i上
                 j = digit(i) - (index - pos);
                 //if ( j == digit(i) - 1 )
                 //   cout<<endl;
                 while ( j > 0 )
                 {
                       i /= 10;
                       j --;
                 }

                 return (i % 10) + '0';
            }


            char Find (long long pos)
            {
                 long long p, t;
                 int index = 0;
                 int temp;
                 while ( a[index] < pos )
                 {
                       index ++;
                 }
                 p = a[index - 1];
                
                 p += b[index - 1];
                 temp = int(pow(10.0, index-1));
                 t = b[index - 1] + index;
                 while ( p < pos )
                 {
                       p += t;
                       t += index;
                       temp ++;
                 }
                
                 p -= t - index;
                
                 return Pos(temp, pos-p);
            }

            int main ()
            {
                int test;
                long long i;
                cin>>test
                while ( test-- )
                {
                      cin>>i;
                      cout<<Find(i)<<endl;
                }

                return 0;
            posted on 2008-01-19 16:46 quxiao 閱讀(1706) 評論(0)  編輯 收藏 引用 所屬分類: ACM
            亚洲v国产v天堂a无码久久| 久久99精品久久只有精品| 国产精品狼人久久久久影院| 99久久国产综合精品成人影院| 四虎国产精品免费久久久| 国产精品久久久99| 午夜精品久久久久久影视riav | 国产高潮国产高潮久久久91| 99久久成人国产精品免费| 久久精品国产亚洲AV影院| 久久精品亚洲福利| 综合人妻久久一区二区精品| 99久久99久久精品国产| 久久久久久亚洲Av无码精品专口| 久久无码中文字幕东京热| 国产精品99精品久久免费| 精品欧美一区二区三区久久久| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 国产精品热久久无码av| 日批日出水久久亚洲精品tv| 久久精品无码一区二区日韩AV| 2021国产精品午夜久久| 99久久国产主播综合精品| 亚洲国产欧洲综合997久久| 亚洲国产成人精品无码久久久久久综合 | 久久频这里精品99香蕉久| 久久精品国内一区二区三区| 思思久久好好热精品国产| 91精品国产91久久综合| 人妻精品久久久久中文字幕69| 欧美无乱码久久久免费午夜一区二区三区中文字幕| 色婷婷综合久久久久中文字幕| 久久香蕉国产线看观看99 | 婷婷久久香蕉五月综合加勒比| 国产精品久久久久久久久久影院| 一本大道加勒比久久综合| 久久久久99精品成人片欧美| 伊人色综合久久天天人手人婷 | 无码八A片人妻少妇久久| 久久人人爽人人爽人人片AV麻烦| 午夜精品久久久内射近拍高清 |