poj 2325

来源:互联网 发布:mac os系统镜像 编辑:程序博客网 时间:2024/04/29 07:41
#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<string>#include<algorithm>using namespace std;/*如果这个数有>7的质因子,那么there is no possible answer*//*一位的质数*/const int maxlength = 1005;char digit[maxlength];char division[maxlength];char res[maxlength];bool div(int d){    int i,cnt = 0,tmpnum = 0;    for(i = 0; digit[i]; ++i){        tmpnum = tmpnum * 10 + digit[i] - '0';        division[cnt++] = tmpnum / d + '0';        tmpnum %= d;    }    division[cnt] = '\0';    if(tmpnum == 0){        i = 0;        while(division[i] == '0'){            ++i;        }        strcpy(digit,division+i);        return true;    }    else        return false;}int main(){   // freopen("1.txt","r",stdin);    int i,cnt;    bool exist,noexist;    while(scanf("%s",digit)!=EOF){        if(digit[0] == '-'){            break;        }        noexist= false;        cnt = 0;        int len = strlen(digit);        if(len < 2){            printf("1%s\n",digit);            continue;        }        while(true){            exist = false;            for(i = 9; i > 1; --i){                if(div(i)){                    exist = true;                    res[cnt++] = '0' + i;                    break;                }            }            if(!exist){ //没有找到合适的除数                if(strlen(digit) > 1){                    noexist = true;                }                break;            }        }         if(noexist){            printf("There is no such number.\n");        }        else{            for(i = cnt - 1; i >= 0; --i){                printf("%c",res[i]);            }            printf("\n");        }    }    return 0;}

greedy algoritthm倒是想出来了,但是忘了读入的是大整数(理解题意不力...)

高精度除法还是很值得借鉴的~

0 0
原创粉丝点击