poj 3508 Hide That Number(類比大數減法)

來源:互聯網
上載者:User
題目連結

http://poj.org/problem?id=3508 題意:

給出一個數字,這個數位前幾位被擦去了,求能否得到一個x,讓x*10+x等於這個數字原來的樣子 題解:

被擦去的幾位只可能是1-10這10個數字,用99,999之類的數舉個例子就可以了,然後仔細觀察這題發現有個規律:
a b c 0
a b c
———- (a>0)
b c c
a d
因此,能求到x的情況下,最後的x首位必然不能為0,其他位如果減去後一位元字後小於0向前一位借1即可 注意點

輸出的char數組要在最後一位輸入一個“\0” AC代碼

#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;char num[1000010];int main(){    int cnt = 1;    while(scanf("%s", num))    {        //cout << num <<endl;        int len = strlen(num);        char ans[1000010];        //cout << len << endl;        if(strcmp(num, "0") == 0)break;        ans[len-1] = num[len-1];        int sigh = 0;        int number = num[len-1];        for(int i = len - 2; i >= 0; i--)        {            number = num[i] - number + sigh;            if(number < 0)            {                ans[i] = number + 10 + '0';                sigh = -1;            }            else            {                ans[i] = number + '0' ;                sigh = 0;            }            number = ans[i];        }        ans[len] = '\0';        if (ans[0] != '0')            printf("%d. %s\n",cnt++,ans);        else            printf("%d. IMPOSSIBLE\n",cnt++);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.