POJ 3508 Hide That Number

来源:互联网 发布:蒙古语自学软件 编辑:程序博客网 时间:2024/05/21 20:30

思路:

题意是将得到的数从左边开始删去位数,直到与最开始的数位数一样。
1)设原来的数是x,那么x*10 + x = 11x 所以通过枚举1-10来判断这个大数是否可以模开11。注意是枚举1到10,因为有可能出现进位导致的删去了两位。
2)如图
这里写图片描述
最后只要判断首位能否为0就行了。

模拟大数取模

#include <iostream>#include <cstdio>#include <string.h>#include <queue>#include <algorithm>typedef long long int lli;using namespace std;char s[10001000];char ans[10001000];int len;int cnt;bool ju(){    for(int i = 1;i <= 10;i++){        cnt = 0;        int temp = i;        for(int j = 0;j < len;j++){            ans[cnt++] = ( temp*10 + s[j] - '0' ) / 11 + '0';            temp  = ( temp*10 + s[j] - '0' ) % 11;        }        if(temp == 0){            ans[cnt] = '\0';            return true;        }    }    return false;}int main(){    int tcase = 0;    while(scanf("%s",s)){        len = strlen(s);        if(len == 1 && s[0] == '0')            break;        tcase++;        if(ju()){            printf("%d. %s\n",tcase,ans);        }        else{            printf("%d. IMPOSSIBLE\n",tcase);        }    }}

错位相减

#include <iostream>#include <cstdio>#include <string.h>#include <queue>#include <algorithm>typedef long long int lli;using namespace std;char s[10001000];char ans[10001000];int len;int cnt;bool ju(){    for(int i = 1;i <= 10;i++){        cnt = 0;        int temp = i;        for(int j = 0;j < len;j++){            ans[cnt++] = ( temp*10 + s[j] - '0' ) / 11 + '0';            temp  = ( temp*10 + s[j] - '0' ) % 11;        }        if(temp == 0){            ans[cnt] = '\0';            return true;        }    }    return false;}int main(){    int tcase = 0;    while(scanf("%s",s)){        len = strlen(s);        if(len == 1 && s[0] == '0')            break;        tcase++;        if(ju()){            printf("%d. %s\n",tcase,ans);        }        else{            printf("%d. IMPOSSIBLE\n",tcase);        }    }}
0 0
原创粉丝点击