菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
232
0

UVA大模拟代码(白书训练计划1)UVA 401,10010,10361,537,409,10878,10815,644,10115,424,10106,465,10494

原创
05/13 14:22
阅读数 82546

白书一:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=64609#overview

注意UVA没有PE之类的,如果PE了显示WA。

UVA401:Palindromes

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
char a[130];
int l,flag,flag2,f3;
void init()
{
    flag=0;
    flag2=0;
    f3=0;
}
void judge();
int main()
{
    while(scanf("%s",a)!=EOF)
    {
        l=strlen(a);
        init();
        for(int i=0; i<l/2; i++)
        {
            if(a[i]!=a[l-1-i])
            {
                flag=1;
                break;
            }
        }
        judge();
        if(flag==1&&flag2==1)
        {
            printf("%s -- is not a palindrome.\n",a);
            cout<<endl;
            continue;
        }
        if(flag==0&&flag2==1)
        {
            printf("%s -- is a regular palindrome.\n",a);
            cout<<endl;
            continue;
        }
        for(int i=0; i<l/2; i++)
        {
            if(a[i]=='A'&&a[l-i-1]=='A') ;
            else   if(a[i]=='M'&&a[l-i-1]=='M') ;
            else   if(a[i]=='H'&&a[l-i-1]=='H') ;
            else   if(a[i]=='I'&&a[l-i-1]=='I') ;
            else   if(a[i]=='Y'&&a[l-i-1]=='Y') ;
            else   if(a[i]=='O'&&a[l-i-1]=='O') ;
            else   if(a[i]=='T'&&a[l-i-1]=='T') ;
            else   if(a[i]=='U'&&a[l-i-1]=='U') ;
            else   if(a[i]=='V'&&a[l-i-1]=='V') ;
            else   if(a[i]=='W'&&a[l-i-1]=='W') ;
            else   if(a[i]=='X'&&a[l-i-1]=='X') ;
            else   if(a[i]=='1'&&a[l-i-1]=='1') ;
            else   if(a[i]=='8'&&a[l-i-1]=='8') ;
            else   if((a[i]=='E'&&a[l-i-1]=='3')||(a[i]=='3'&&a[l-i-1]=='E')) ;
            else   if((a[i]=='J'&&a[l-i-1]=='L')||(a[i]=='L'&&a[l-i-1]=='J')) ;
            else   if((a[i]=='S'&&a[l-i-1]=='2')||(a[i]=='2'&&a[l-i-1]=='S')) ;
            else   if((a[i]=='Z'&&a[l-i-1]=='5')||(a[i]=='5'&&a[l-i-1]=='Z')) ;
            else
            {
                f3=1;
                break;
            }
        }
        if(l%2==1)
        {
            if(a[l/2]=='E'||a[l/2]=='J'||a[l/2]=='L'||a[l/2]=='Z'||a[l/2]=='2'||a[l/2]=='S'||a[l/2]=='3'||a[l/2]=='5')
            {
                f3=1;
            }
        }
        if(flag==1&&f3==1)
        {
            printf("%s -- is not a palindrome.\n",a);
            cout<<endl;
            continue;
        }
        if(flag==0&&f3==1)
        {
            printf("%s -- is a regular palindrome.\n",a);
            cout<<endl;
            continue;
        }
        if(flag==1&&f3==0)
        {
            printf("%s -- is a mirrored string.\n",a);
            cout<<endl;
            continue;
        }
        else
        {
            printf("%s -- is a mirrored palindrome.\n",a);
            cout<<endl;
        }
    }
    return 0;
}
void judge()
{
    for(int i=0; i<l; i++)
    {
        switch(a[i])
        {
        case 'A':
            break;
        case 'E':
            break;
        case 'H':
            break;
        case 'I':
            break;
        case 'L':
            break;
        case 'J':
            break;
        case 'M':
            break;
        case 'O':
            break;
        case 'S':
            break;
        case 'T':
            break;
        case 'U':
            break;
        case 'V':
            break;
        case 'W':
            break;
        case 'X':
            break;
        case 'Y':
            break;
        case 'Z':
            break;
        case '1':
            break;
        case '2':
            break;
        case '3':
            break;
        case '5':
            break;
        case '8':
            break;
        default :
            flag2=1;
        }
    }
}
View Code


UVA10010:Where's Waldorf?

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct node
{
    int x,y;
};
int n,m,k,l,flag,txx,tyy;
char map[60][60],ch[60];
void init()
{
    flag=0;
    txx=0;
    tyy=0;
}
void dfs1(int x,int y,int le)
{
    if(le==l)
    {
        flag=1;
        return ;
    }
    struct node t;
    t.x=x;
    t.y=y-1;
    if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]==ch[le])
    {
        dfs1(t.x,t.y,le+1);
    }
    return ;
}
void dfs2(int x,int y,int le)
{
    if(le==l)
    {
        flag=1;
        return ;
    }
    struct node t;
    t.x=x-1;
    t.y=y-1;
    if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]==ch[le])
    {
        dfs2(t.x,t.y,le+1);
    }
    return ;
}
void dfs3(int x,int y,int le)
{
    if(le==l)
    {
        flag=1;
        return ;
    }
    struct node t;
    t.x=x-1;
    t.y=y;
    if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]==ch[le])
    {
        dfs3(t.x,t.y,le+1);
    }
    return ;
}
void dfs4(int x,int y,int le)
{
    if(le==l)
    {
        flag=1;
        return ;
    }
    struct node t;
    t.x=x-1;
    t.y=y+1;
    if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]==ch[le])
    {
        dfs4(t.x,t.y,le+1);
    }
    return ;
}
void dfs5(int x,int y,int le)
{
    if(le==l)
    {
        flag=1;
        return ;
    }
    struct node t;
    t.x=x;
    t.y=y+1;
    if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]==ch[le])
    {
        dfs5(t.x,t.y,le+1);
    }
    return ;
}
void dfs6(int x,int y,int le)
{
    if(le==l)
    {
        flag=1;
        return ;
    }
    struct node t;
    t.x=x+1;
    t.y=y-1;
    if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]==ch[le])
    {
        dfs6(t.x,t.y,le+1);
    }
    return ;
}
void dfs7(int x,int y,int le)
{
    if(le==l)
    {
        flag=1;
        return ;
    }
    struct node t;
    t.x=x+1;
    t.y=y;
    if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]==ch[le])
    {
        dfs7(t.x,t.y,le+1);
    }
    return ;
}
void dfs8(int x,int y,int le)
{
    if(le==l)
    {
        flag=1;
        return ;
    }
    struct node t;
    t.x=x+1;
    t.y=y+1;
    if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]==ch[le])
    {
        dfs8(t.x,t.y,le+1);
    }
    return ;
}
int main()
{
    int T,ll;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%*c%s",map[i]);
            ll=strlen(map[i]);
            for(int j=0;j<ll;j++)
            {
                if(map[i][j]>='A'&&map[i][j]<='Z')
                  map[i][j]+=32;
            }
        }
        scanf("%d",&k);
        while(k--)
        {
            init();
            scanf("%s",ch);
            l=strlen(ch);
            for(int i=0;i<l;i++)
            {
                if(ch[i]>='A'&&ch[i]<='Z')
                ch[i]+=32;
            }
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    if(map[i][j]==ch[0])
                    {
                        txx=i+1;
                        tyy=j+1;
                        dfs1(i,j,1);
                        if(flag)
                            break;
                        dfs2(i,j,1);
                        if(flag)
                            break;
                        dfs3(i,j,1);
                        if(flag)
                            break;
                        dfs4(i,j,1);
                        if(flag)
                            break;
                        dfs5(i,j,1);
                        if(flag)
                            break;
                        dfs6(i,j,1);
                        if(flag)
                            break;
                        dfs7(i,j,1);
                        if(flag)
                            break;
                        dfs8(i,j,1);
                        if(flag)
                            break;
                    }
                }
                if(flag) break;
            }
            printf("%d %d\n",txx,tyy);
        }
        if(T) printf("\n");
    }
    return 0;
}
View Code


UVA10361:Automatic Poetry

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,l1,l2,l3,l4,l5,l6;
char a[110],b[110];
void init()
{
    l3=-1;
    l4=-1;
    l5=-1;
    l6=-1;
}
int main()
{
    scanf("%d",&n);
    getchar();
    while(n--)
    {
       gets(a);
       gets(b);
       l1=strlen(a);
       l2=strlen(b);
       init();
       for(int i=0;i<l1;i++)
       {
           if(a[i]!='<'&&a[i]!='>')
           {
               printf("%c",a[i]);
           }
           else if(a[i]=='<'&&l3==-1)
           {
               l3=i;
           }
           else if(a[i]=='>'&&l4==-1)
           {
               l4=i;
           }
           else if(a[i]=='<'&&l5==-1)
           {
               l5=i;
           }
           else if(a[i]=='>'&&l6==-1)
           {
               l6=i;
           }
       }
       printf("\n");
       for(int i=0;i<l2;i++)
       {
           if(b[i]=='.')
              break;
           printf("%c",b[i]);
       }

       for(int i=l5+1;i<l6;i++)
       {
            printf("%c",a[i]);
       }
       for(int i=l4+1;i<l5;i++)
       {
           printf("%c",a[i]);
       }
        for(int i=l3+1;i<l4;i++)
       {
           printf("%c",a[i]);
       }
       for(int i=l6+1;i<l1;i++)
       {
           printf("%c",a[i]);
       }
       printf("\n");
    }
    return 0;
}
View Code

UVA537:Artificial Intelligence?

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#define inf 0x3f3f3f3f
using namespace std;
char a[1001],b[1001],c[1001],d[1001];
int l,U,I,P,t1,t2,t3;
double v1,i1,w1;
void init()
{
    U=inf;
    I=inf;
    P=inf;
    v1=1;
    i1=1;
    w1=1;
    t1=t2=t3=0;
}
int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    for(int z=1; z<=T; z++)
    {
        init();
        gets(a);
        l=strlen(a);
        for(int i=0; i<l; i++)
        {
            if(a[i]=='U'&&a[i+1]=='=')
            {
                for(int j=i+2; j<l; j++)
                {
                    if((a[j]>='0'&&a[j]<='9')||a[j]=='.')
                    {
                        b[t1++]=a[j];
                    }
                    else
                    {
                        b[t1]='\0';
                        if(a[j]=='k')
                            v1=1000;
                        else if(a[j]=='V')
                            v1=1;
                        else if(a[j]=='m')
                            v1=0.001;
                        else if(a[j]=='M')
                            v1=1000000;
                        break;
                    }
                }
                U=0;
            }
            else if(a[i]=='I'&&a[i+1]=='=')
            {
                for(int j=i+2; j<l; j++)
                {
                    if((a[j]>='0'&&a[j]<='9')||a[j]=='.')
                    {
                        c[t2++]=a[j];
                    }
                    else
                    {
                        c[t2]='\0';
                        if(a[j]=='m')
                            i1=0.001;
                        else if(a[j]=='M')
                            i1=1000000;
                        else if(a[j]=='k')
                            i1=1000;
                        else if(a[j]=='A')
                            i1=1;
                        break;
                    }
                }
                I=0;
            }
            else if(a[i]=='P'&&a[i+1]=='=')
            {
                for(int j=i+2; j<l; j++)
                {
                    if((a[j]>='0'&&a[j]<='9')||a[j]=='.')
                    {
                        d[t3++]=a[j];
                    }
                    else
                    {
                        d[t3]='\0';
                        if(a[j]=='M')
                            w1=1000000;
                        else if(a[j]=='m')
                            w1=0.001;
                        else if(a[j]=='k')
                            w1=1000;
                        else if(a[j]=='W')
                            w1=1;
                        break;
                    }
                }
                P=0;
            }
        }
        printf("Problem #%d\n",z);
        double sum;
        if(U==0&&I==0)
        {
            sum=atof(b)*atof(c)*v1*i1;
            printf("P=%.2lfW\n",sum);
        }
        else if(U==0&&P==0)
        {
            sum=(atof(d)*w1)/(atof(b)*v1);
            printf("I=%.2lfA\n",sum);
        }
        else if(I==0&&P==0)
        {
            sum=(atof(d)*w1)/(atof(c)*i1);
            printf("U=%.2lfV\n",sum);
        }
        cout<<endl;
    }
    return 0;
}
View Code

UVA409:Excuses, Excuses!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef struct Node
{
    struct Node *next[26];
    int flag;
} Node,*Tree;
int n,m,K,ll,M;
void Creat(Tree &T)
{
    T=(Node *)malloc(sizeof(Node));
    T->flag=0;
    for(int i=0; i<26; i++)
        T->next[i]=NULL;
}
void insert(Tree &T,char *s)
{
    Tree p=T;
    int t;
    int l=strlen(s);
    for(int i=0; i<l; i++)
    {
        if(s[i]>='a'&&s[i]<='z')
            t=s[i]-'a';
        else  t=s[i]-'A';
        if(p->next[t]==NULL)
            Creat(p->next[t]);
        p=p->next[t];
    }
    p->flag=1;
}
void search(Tree &T,char *s)
{
    Tree p=T;
    int t;
    int l=strlen(s);
    for(int i=0; i<l; i++)
    {
        if(s[i]>='a'&&s[i]<='z')
            t=s[i]-'a';
        else  t=s[i]-'A';
        if(p->next[t]==NULL)
            return ;
        p=p->next[t];
    }
    if(p->flag==1)
        M++;
    return ;
}
void D(Tree p)
{
    for(int i=0; i<26; i++)
    {
        if(p->next[i]!=NULL)
            D(p->next[i]);
    }
    free(p);
}
int main()
{
    K=0;
    char ch[210],q[210],aa[210][210];
    int f1,p[210],maxx;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        Tree T;
        maxx=-1;
        memset(p,0,sizeof(p));
        Creat(T);
        for(int i=0; i<n; i++)
        {
            scanf("%s",ch);
            insert(T,ch);
        }
        getchar();
        for(int i=0; i<m; i++)
        {
            gets(ch);
            strcpy(aa[i],ch);
            ll=strlen(ch);
            f1=0;
            M=0;
            for(int j=0; j<ll; j++)
            {
                if((ch[j]>='a'&&ch[j]<='z')||(ch[j]>='A'&&ch[j]<='Z'))
                {
                    q[f1++]=ch[j];
                }
                else
                {
                    q[f1]='\0';
                    if(f1!=0)
                        search(T,q);
                    f1=0;
                }
            }
            if(f1!=0)
            {
                q[f1]='\0';
                search(T,q);
            }
            //cout<<"M=="<<M<<endl;
            maxx=max(maxx,M);
            p[i]=M;
        }
        printf("Excuse Set #%d\n",++K);
        for(int i=0; i<m; i++)
        {
            if(p[i]==maxx)
                printf("%s\n",aa[i]);
        }
        D(T);
        printf("\n");
    }
    return 0;
}
View Code

UVA10878: Decode the tape

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int t,l,ll,tt;
char a[1100];
int main()
{
    tt=0;
    while(gets(a)!=NULL)
    {
        if(strcmp(a,"___________")==0)
        {
           continue;
        }
        l=strlen(a);
        t=0;
        ll=0;
        for(int i=l-1;i>=0;i--)
        {
            if(a[i]==' ')
            {
               ll++;
            }
            else if(a[i]=='o')
            {
                t+=pow(2,ll);
                ll++;
            }
        }
        printf("%c",t);
    }
    return 0;
}
View Code

UVA10815:Andy's First Dictionary

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef struct Node
{
    struct Node *next[26];
    int flag;
} Node,*Tree;
int f1,l,tt;
char a[5002][210],CH[210];
void Creat(Tree &T)
{
    T=(Node *)malloc(sizeof(Node));
    T->flag=0;
    for(int i=0; i<26; i++)
        T->next[i]=NULL;
}
void insert(Tree &T,char *s)
{
    Tree p=T;
    int t;
    int l=strlen(s);
    for(int i=0; i<l; i++)
    {
        if(s[i]>='a'&&s[i]<='z')
            t=s[i]-'a';
        else  t=s[i]-'A';
        if(p->next[t]==NULL)
            Creat(p->next[t]);
        p=p->next[t];
    }
    p->flag=1;
}
void D(Tree p)
{
    for(int i=0; i<26; i++)
    {
        if(p->next[i]!=NULL)
            D(p->next[i]);
    }
    free(p);
}
void dfs(Tree &P,int r,int l)
{
    if(P->flag>0)
    {
        CH[l]='a'+r;
        CH[l+1]='\0';
        printf("%s\n",CH);
    }
    else CH[l]=CH[l]='a'+r;
    for(int i=0; i<26; i++)
    {
        if(P->next[i]!=NULL)
            dfs(P->next[i],i,l+1);
    }
}
int main()
{
    Tree T;
    tt=0;
    while(gets(a[tt])!=NULL)
    {
        tt++;
    }
    Creat(T);
    char ch[210];
    for(int i=0; i<tt; i++)
    {
        l=strlen(a[i]);
        f1=0;
        for(int j=0; j<l; j++)
        {
            if((a[i][j]>='a'&&a[i][j]<='z')||(a[i][j]>='A'&&a[i][j]<='Z'))
            {
                ch[f1++]=a[i][j];
            }
            else
            {
                ch[f1]='\0';
                // printf("%s\n",ch);
                if(f1!=0)
                    insert(T,ch);
                f1=0;
            }
        }
        if(f1!=0)
        {
            ch[f1]='\0';
            insert(T,ch);
            //printf("%s\n",ch);
        }
    }
    for(int i=0; i<26; i++)
    {
        if(T->next[i]!=NULL)
            dfs(T->next[i],i,0);
    }
    D(T);
    return 0;
}
View Code

UVA644: Immediate Decodability

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef struct Node
{
    struct Node *next[2];
    int flag;
} Node,*Tree;
int f1;
int cmp(const void *aa,const void *bb)
{
    return strcmp((char *)bb,(char *)aa);
}
void Creat(Tree &T)
{
    T=(Node *)malloc(sizeof(Node));
    T->flag=0;
    for(int i=0; i<2; i++)
        T->next[i]=NULL;
}
void insert(Tree &T,char *s)
{
    Tree p=T;
    int t;
    int l=strlen(s);
    for(int i=0; i<l; i++)
    {
        t=s[i]-'0';
        if(p->next[t]==NULL)
            Creat(p->next[t]);
        p=p->next[t];
        p->flag++;
    }
    if(p->flag>1)
    {
       f1=0;
    }
}
void D(Tree p)
{
    for(int i=0; i<2; i++)
    {
        if(p->next[i]!=NULL)
            D(p->next[i]);
    }
    free(p);
}
char a[10000001][100];
int main()
{

    Tree T;
    int K=0,tt=0;
    while(scanf("%s",a[0])!=EOF)
    {
        if(strcmp(a[0],"9")==0) break;
        tt=1;
        while(scanf("%s",a[tt])!=EOF)
        {
            if(strcmp(a[tt],"9")==0)
                break;
            tt++;
        }
        Creat(T);
        f1=1;
        qsort(a,tt,sizeof(a[0]),cmp);
        for(int i=0; i<tt; i++)
        {
            insert(T,a[i]);
            if(f1==0) break;
        }
        if(f1)
            printf("Set %d is immediately decodable\n",++K);
        else printf("Set %d is not immediately decodable\n",++K);
        D(T);
    }
    return 0;
}
View Code

UVA10115:Automatic Editing

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n;
char a[20][100],b[20][100],ch[10001],t[10001];
int main()
{
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        getchar();
        for(int i=0;i<n;i++)
        {
            gets(a[i]);
            gets(b[i]);
        }
        gets(ch);
        for(int i=0;i<n;++i)
        {
            if(strstr(ch,a[i])!=NULL)//如果没有返回空
            {
                char *p=strstr(ch,a[i]);
                int l=strlen(a[i]);
                strcpy(t,p+l);
                *p='\0';
                strcat(ch,b[i]);
                strcat(ch,t);
                i--;
            }
        }
       printf("%s\n",ch);
    }
    return 0;
}
View Code

UVA424:Integer Inquiry

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int l1;
    char a[1001];
    int b[1001];
    memset(b,0,sizeof(b));
    while(scanf("%s",a)!=EOF)
    {
        if(a[0]=='0') break;
        l1=strlen(a);
        int j=0;
        for(int i=l1-1; i>=0; i--)
        {
            b[j]=b[j]+a[i]-'0';
            j++;
        }
    }
    int t;
    for(int i=0; i<1001; i++)
    {
        t=b[i];
        if(b[i]>9)
        {
            b[i]=t%10;
            b[i+1]=b[i+1]+t/10;
        }
        else b[i]=t;
    }
    int flag=0;
    for(int i=1001-1; i>=0; i--)
    {
        if (flag || b[i])
        {
            flag = 1;
            printf("%d",b[i]);
        }
    }
    if (!flag) printf("0");
    printf("\n");
    return 0;
}
View Code

UVA10106:Product

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
char str1[300],str2[300];
int d[300],f[300],c[601];
int l1,l2,l,w,e;
void init()
{
    memset(c,0,sizeof(c));
    memset(d,0,sizeof(d));
    memset(f,0,sizeof(f));
}
int main()
{
    while(scanf("%s",str1)!=EOF)
    {
        init();
        scanf("%s",str2);
        l1=strlen(str1);
        l2=strlen(str2);
        l=max(l1,l2);
        w=l1;
        e=l2;
        for(int i=0; i<l; i++)
        {
            if(l1-1>=0)
            {
                d[i]=str1[l1-1]-'0';
                l1--;
            }
            else d[i]=0;
            if(l2-1>=0)
            {
                f[i]=str2[l2-1]-'0';
                l2--;
            }
            else f[i]=0;
        }
        for(int i=0; i<w; i++)
        {
            for(int j=0; j<e; j++)
            {
                c[i+j]=c[i+j]+d[i]*f[j];
            }
        }
        for(int i=0; i<600; i++)
        {
            if(c[i]>=10)
            {
                c[i+1]+=c[i]/10;
                c[i]=c[i]%10;
            }
        }
        int flag=0;
        for(int i=600; i>=0; i--)
        {
            if(flag||c[i])
            {
                flag=1;
                printf("%d",c[i]);
            }
        }
        if(flag==0) printf("0");
        printf("\n");


    }
    return 0;
}
View Code

UVA465:Overflow

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>//atof函数
#include <algorithm>
#define inf 0x7fffffff// inf==2147483647
using namespace std;
char a[1001],b[1001],c[1001];
double x,y;
int main()
{
    while(scanf("%s%s%s",a,b,c)!=EOF)
    {
        printf("%s %s %s\n",a,b,c);
        x=atof(a);
        y=atof(c);
        if(x>inf)printf("first number too big\n");
        if(y>inf)printf("second number too big\n");
        if(strcmp(b,"+")==0&&x+y>inf)printf("result too big\n");
        else if(strcmp(b,"*")==0&&x*y>inf)printf("result too big\n");
    }
    return 0;
}
/*
函数名: atof
功  能: 把字符串转换成浮点数
用  法: double atof(const char *nptr);
程序例:
*/
View Code

UVA10494: If We Were a Child Again

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char a[10001],b[10001],c[10001];
long long d;
long long MOD(char * src, long long mod)//模板
{
    long long len=strlen(src),i;
    long long t=0;//中间变量,最终存储余数
    for(i=0; i<len; i++)
    {
        t*=10;
        t+=src[i]-'0';
        if(t>=mod)
            t=t%mod;
    }
    return t;
}
void division(char * src, long long n)//模板
{
    long long len = strlen(src),i,k,t=0,s=0;
    char dest[10001];
    bool flag = true;    //商是否有了第一个有效位,防止商首部一直出现0
    for(i=0,k=0; i<len; i++)
    {
        t = s*10+(src[i]-48);    //新余数
        if(t/n>0 || t==0)        //余数为0要修改商
        {
            dest[k++] = t/n+48,s = t%n,flag = false;
        }
        else                    //不够除,修改余数
        {
            s = t;
            if(!flag)            //商已经有有效位了,补零
                dest[k++] = '0';
        }
    }
    for(i=0; i<k; i++)
        cout<<dest[i];
    cout<<endl;
}
int main()
{
    while(scanf("%s%s%lld",a,b,&d)!=EOF)
    {
        if(strcmp(b,"/")==0)
        {
            division(a,d);
        }
        else if(strcmp(b,"%")==0)
        {
            cout<<MOD(a,d)<<endl;
        }
    }
    return 0;
}
View Code

 

发表评论

0/200
232 点赞
0 评论
收藏