zoukankan      html  css  js  c++  java
  • HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛

    题目链接

    题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge。

    题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位数为偶数的时候3456就分为34和56,34-1=33,回文数3333,3456-3333=123然后继续算;当位数为奇数的时候34567就分为34和67,5-1=4,回文数34443,34567-34443=124然后继续算。但是一年都没有写过高精度减法的题了,这个大模拟写了很久最后判断奇偶性都判断蒙了- - !,汗。考虑的太复杂了,前导0,奇偶性,如果前面比后面小就不减1的情况,有的是我想多了,找了一份比较短的代码虚心学习一下,写的很棒。

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1000+9;
    
    bool getPal(char *s) { //得到回文数
        int n=strlen(s);
        if(n==1)return 1;
        if(n==2&&s[0]=='1') {
            s[0]='9';
            s[1]=0;
            return 0;
        }
        s[(n+1)/2-1]--;
        for(int i=(n+1)/2-1; i>0; i--) {
            if(s[i]<'0')s[i]+=10,s[i-1]--;
            else break;
        }
        if(s[0]=='0') {
            for(int i=0; i<n; i++)s[i]=s[i+1];
            n--;
        }
        for(int i=(n+1)/2; i<n; i++)
            s[i]=s[n-i-1];
        return 0;
    }
    bool subStr(char *a,char *b) { //减去回文数
        int na=strlen(a);
        int nb=strlen(b);
        for(int i=na-1,j=nb-1; j>=0; i--,j--) {
            if(a[i]>=b[j])a[i]=a[i]-b[j]+'0';
            else a[i]=a[i]+10-b[j]+'0',a[i-1]--;
        }
        int p;
        for(p=0; a[p]=='0'; p++);
        if(p==na)return 1;
        for(int i=0; p<=na; p++,i++)a[i]=a[p];
        return 0;
    }
    char s[N],ans[50][N];
    int cnt;
    int main() {
        //freopen("f.txt","r",stdin);
        int T;
        scanf("%d",&T);
        for(int cas=1; cas<=T; cas++) {
            scanf("%s",s);
            for(cnt=0;; cnt++) {
                strcpy(ans[cnt],s);
                if(getPal(ans[cnt])||subStr(s,ans[cnt]))break;
            }
    
            printf("Case #%d:
    %d
    ",cas,cnt+1);
            for(int i=0; i<=cnt; i++)
                printf("%s
    ",ans[i]);
        }
        return 0;
    }
  • 相关阅读:
    百度之星资格赛1001——找规律——大搬家
    HDU1025——LIS——Constructing Roads In JGShining's Kingdom
    DP(递归打印路径) UVA 662 Fast Food
    递推DP UVA 607 Scheduling Lectures
    递推DP UVA 590 Always on the run
    递推DP UVA 473 Raucous Rockers
    博弈 HDOJ 4371 Alice and Bob
    DFS(深度) hihoCoder挑战赛14 B 赛车
    Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
    DP(DAG) UVA 437 The Tower of Babylon
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5942885.html
Copyright © 2011-2022 走看看