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

            coreBugZJ

            此 blog 已棄。

            PALIN - SPOJ 5. The Next Palindrome

            A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

            Input

            The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

            Output

            For each K, output the smallest palindrome larger than K.

            Example

            Input:
            2
            808
            2133

            Output:
            818
            2222

            Warning: large Input/Output data, be careful with certain languages


            問題:
            求出比輸入整數大的最小的回文數,輸入整數不超過 1000000 個數字。

            解法:
            貪心。

            代碼 LISP SBCL :
             1(defun digit-succ(x)
             2 (elt "0123456789A" (position x "*0123456789")))
             3
             4(let ((num (read)))
             5 (dotimes (ca num)
             6  (let* ((str (read-line))
             7         (len (length str))
             8         (cmp 0)
             9         (i nil)
            10         (j nil))
            11   (dotimes (i (floor (/ (1+ len) 2)))
            12    (setf j (1- (- len i)))
            13    (when (char> (elt str i) (elt str j))
            14     (setf cmp 1))
            15    (when (char< (elt str i) (elt str j))
            16     (setf cmp -1))
            17    (setf (elt str j) (elt str i)))
            18   (when (<= cmp 0)
            19    (setf i (1- (floor (/ (1+ len) 2))))
            20    (setf j (if (oddp len) i (1+ i)))
            21    (do ()
            22     ((or (minusp i) (char< (elt str i) #\9)))
            23     (setf (elt str i) #\0)
            24     (setf (elt str j) #\0)
            25     (decf i)
            26     (incf j))
            27    (when (minusp i)
            28     (setf (elt str 0) #\1)
            29     (write-string str)
            30     (write-char #\1))
            31    (when (not (minusp i))
            32     (setf (elt str i) (digit-succ (elt str i)))
            33     (setf (elt str j) (elt str i))
            34     (write-string str)))
            35   (when (plusp cmp)
            36    (write-string str))
            37   (fresh-line))))
            38
            39


            posted on 2012-02-19 16:18 coreBugZJ 閱讀(416) 評論(0)  編輯 收藏 引用 所屬分類: ACMAlgorithmLisp

            91精品国产综合久久精品| 精品久久久久久无码国产| 99久久99久久精品国产片果冻 | 99久久国产综合精品女同图片| 久久久久久久精品妇女99| 久久国产色AV免费看| 国产精品亚洲美女久久久| 精品久久久久久中文字幕大豆网| 久久w5ww成w人免费| 狠狠久久综合伊人不卡| 老色鬼久久亚洲AV综合| 精品无码久久久久久久久久| 少妇高潮惨叫久久久久久| 91精品国产高清久久久久久91 | 久久久久亚洲AV无码专区首JN | 亚洲精品高清一二区久久| 国内精品伊人久久久久av一坑| 久久久WWW成人| 国内精品人妻无码久久久影院 | 18岁日韩内射颜射午夜久久成人| 久久伊人色| 99久久精品国产综合一区| 久久国产精品无码一区二区三区| 免费精品久久久久久中文字幕 | 国内精品久久国产| 99久久精品无码一区二区毛片| 无码人妻精品一区二区三区久久久| 精品久久久久久国产牛牛app| 99国产欧美精品久久久蜜芽| 久久久久99这里有精品10| 久久免费大片| 精品久久人人做人人爽综合 | 久久久亚洲欧洲日产国码二区| 欧美久久久久久午夜精品| 91精品国产91久久久久久蜜臀 | 中文精品久久久久人妻| 久久青青国产| 日韩精品久久久久久| 久久美女人爽女人爽| 久久99久久无码毛片一区二区| 91精品国产综合久久香蕉|