• <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

            每天進(jìn)步一點(diǎn)點(diǎn)!

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

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

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

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

            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中找到下標(biāo)為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 閱讀(1708) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACM
            2020最新久久久视精品爱| 色诱久久av| 国产成人久久精品区一区二区| 亚洲国产精品久久电影欧美| 国产亚洲综合久久系列| 内射无码专区久久亚洲| 无码日韩人妻精品久久蜜桃| 国产成人99久久亚洲综合精品| 人妻无码久久精品| 狠狠狠色丁香婷婷综合久久五月| 久久国产香蕉视频| 麻豆成人久久精品二区三区免费| 国产精品狼人久久久久影院| 久久午夜免费视频| 狠狠精品久久久无码中文字幕 | 亚洲精品综合久久| 国产成人精品免费久久久久| 中文字幕无码久久精品青草| 国产免费久久久久久无码| 久久精品国产亚洲AV久| 久久精品国产亚洲5555| 久久亚洲AV成人出白浆无码国产| 中文字幕精品无码久久久久久3D日动漫 | 国内精品九九久久精品| 久久午夜综合久久| 91久久成人免费| 97久久精品人人澡人人爽| 久久久久人妻精品一区 | 久久精品国产久精国产果冻传媒| 一级做a爰片久久毛片16| 精品久久久久久亚洲精品| 午夜天堂av天堂久久久| 亚洲中文字幕无码久久2017| 亚洲国产综合久久天堂 | 久久精品日日躁夜夜躁欧美| 午夜精品久久久久久影视777| 国产精品成人久久久久三级午夜电影| 国内精品久久久久影院日本| 国产亚洲色婷婷久久99精品| 久久99精品久久久久子伦| 久久精品国产亚洲av麻豆色欲|