【HDOJ】4608 I-number

【題目】http://acm.hdu.edu.cn/showproblem.php?pid=4608php

【報告】spa

    最直接的,一個很簡單粗暴的思路,就是1個1個加上去,加的時候和進位的時候維護一下整個數的數位和。理論上應該是可行的,並且應該不會加不少次(我也不知道加幾回,隨機了幾個數感受10次到頂了。。)get

    我不是那樣作的。採用構造法。個位特殊判斷,直接判斷個位上的數字加上去以後能不能使符合要求,能的話就直接加了(好比202->208)。對於十位以及更高位數的,若是當前位是9,那麼忽略之,若是不是9,那麼+1,而後把後面所有清零,個位上再補上不夠的。class

    而後就一次AC了。。程序

【程序】word

// Task: 4608 I-number
// Designer: Rsky 2013/09/03
#include
#include
#include
#include
#include
using namespace std;
const int N = 100000;
char c[N+1000];
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        memset(c,0,sizeof(c));
        strcpy(c,"000");
        scanf("%s",c+1);
        int k=0;
        for (int i=strlen(c)-1;i>=0;i--)
            k+=c[i]-'0';
        for (int i=strlen(c)-1;i>=0;i--)
        {
            if (c[i]-'0'<9)
            { // 能夠加
                int w=strlen(c)-1-i;  // 記錄當前位後面的位數,0的個數
                if (w*9+9-c[i]+'0'>=10-k)  // 能夠構造
                {
                    if (w>0)  // 後面有位數
                    {
                        c[i]++;
                        c[strlen(c)-1]+=10-k-1;
                     di

相關文章
相關標籤/搜索