AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 0. Getting Started

终于要开始系统的做题了。

诶 还是太水了 希望一个暑假过后能够有大的飞跃吧 

好好做题 天天向上!


废话不多说,贴上《算法竞赛入门经典》 刘汝佳 题目的代码吧

AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 0. Getting Started

10055 - Hashmat the Brave Warrior

一开始给wa了。。没有注意到a,b应该用long int类型

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main ()
{
    long int a,b;
    while (scanf("%ld%ld",&a,&b)!=EOF)
    {
        if (a<b)
            printf("%ld\n",b-a);
        else
            printf("%ld\n",a-b);
    }
    return 0;
}


10071 - Back to High School Physics

#include<iostream>
#include<cstdio>
using namespace std;
int main ()
{
    int v,t;
    while (cin>>v>>t)
        cout<<v*t*2<<endl;
    return 0;
}


10300 - Ecological Premium

#include<iostream>
#include<cstdio>
using namespace std;
int main ()
{
    int t,f,a,b,c,s;
    cin>>t;
    while (t--)
    {
        cin>>f;
        int i;
        s=0;
        for (i=0; i<f; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            s+=a*c;
        }
        cout<<s<<endl;
    }
    return 0;
}


458 - The Decoder

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char str[10000];
int main ()
{
    while (gets(str))
    {
        int len=strlen(str);
        int i;
        for (i=0; i<len; i++)
            printf("%c",str[i]-7);
        cout<<endl;
    }
    return 0;
}


494 - Kindergarten Counting Game

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctype.h>
using namespace std;
char str[10000];
int main ()
{
    while (gets(str))
    {
        int len=strlen(str);
        int i,s=0,k=-1,t=-1;
        for (i=0; i<len; i++)
        {
            if (isalpha(str[i]))
            {
                if (t==-1)
                {
                    s++;
                    t=1;
                }
                continue;
            }
            else
                t=-1;

        }
        cout<<s<<endl;
    }
}


414 - Machined Surfaces

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char str[1000];
int main ()
{
    int t,j,i;
    while (cin>>t)
    {
        int s=0,x=0,min=1000;
        getchar();
        if (t==0) break;
        for (j=0; j<t; j++)
        {
            x=0;
            gets(str);
            int len=strlen(str);
            for (i=0; i<len; i++)
            {
                if (str[i]==' ')
                {
                    s++;
                    x++;
                }
            }
            if (x<min) min=x;
        }
        cout<<s-min*t<<endl;
    }
    return 0;
}


490 - Rotating Sentences

不停的在wa,没有发现当输出时,如果后续没有字符。就直接回行,不需要用空格填充。

而且之前以为是多case的。UVa描述不清楚

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main ()
{
    char str[110][110];
    int n=0,max=0;
    while (gets(str[n++]))
    {
        if (str[n-1][0]==0) break;
        if (max<strlen(str[n-1])) max=strlen(str[n-1]);
    }
    int i,j;
    for (j=0; j<max; j++)
    {
        for (i=n-2; i>=0; i--)
        if (j<strlen(str[i])) cout<<str[i][j];
        else cout<<" ";
        cout<<endl;
    }
    return 0;
}


445 - Marvelous Mazes

原来是输入一句输出一句。一开始以为是要整篇文章输入之后再输出。给wa了好多次。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctype.h>
using namespace std;
int main ()
{
    char st[150];
    int i,j,m=0,n=0,max=0;
    while(gets(st))
    {
        if (st[0]!='\0')
        {
            int len=strlen(st),s;
            for (i=0,s=0; i<len; i++)
            {
                if (isdigit(st[i])) s+=st[i]-'0';
                else if (st[i]=='!') cout<<endl;
                else
                while(s>0)
                {
                    if (st[i]=='b') cout<<" ";
                    else cout<<st[i];
                    s--;
                }
            }
        }
        cout<<endl;
    }
    return 0;
}


488 - Triangle Wave

要注意输出的格式,每个case之间才有空行。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main ()
{
    int t,am,fr,i,j;
    cin>>t;
    while (t--)
    {
        cin>>am>>fr;
        while (fr--)
        {
            for (i=1; i<=am; i++)
            {
                for (j=1; j<=i; j++)
                    cout<<i;
                cout<<endl;
            }
            for (i=am-1; i>=1; i--)
            {
                for (j=1; j<=i; j++)
                    cout<<i;
                cout<<endl;
            }
            if (fr>=1) cout<<endl;
        }
        if (t>=1) cout<<endl;
    }
    return 0;
}


489 - Hangman Judge

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main ()
{
    int n,i,j;
    char word[100],alpha[100];
    int visited[30];
    while (cin>>n)
    {
        if (n==-1) break;
        cin>>word>>alpha;
        int s=0,k=0;
        memset(visited,0,sizeof(visited));
        int len=strlen(alpha),LEN=strlen(word);
        for (i=0; i<len; i++)
            if (visited[alpha[i]-'a']==0)
            {
                if (strchr(word,alpha[i])!=NULL)
                {
                    for (j=0; j<LEN; j++)
                        if (word[j]==alpha[i]) k++;
                    visited[alpha[i]-'a']=1;
                }
                else s++;
                if (k==LEN || s==7) break;
            }
        cout<<"Round "<<n<<endl;
        if (k<LEN && s==7) cout<<"You lose."<<endl;
        if (k==LEN && s<7) cout<<"You win."<<endl;
        if (k<LEN && s<7) cout<<"You chickened out."<<endl;
    }
}


694 - The Collatz Sequence

一开始用int给wa了。因为当数字大于int的最大值之后,返回的是一个任意数,有可能是负数。就死循环了

所以必须用long int

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main ()
{
    long int a,l,n,t=0;
    while (cin>>a>>l)
    {
        t++;
        n=a;
        int s=0;
        if (a==-1 && l==-1) break;
        while(a!=1)
        {
            if (a%2) a=3*a+1;
            else a=a/2;
            s++;
            if (a>l) break;
        }
        if (a==1 && n!=1) s++;
        printf("Case %d: A = %ld, limit = %ld, number of terms = %ld\n",t,n,l,s);
    }
    return 0;
}


457 - Linear Cellular Automata

万恶的英语。。看不懂题意啊。。

其实就是算出左右和自己相加之后的值,然后通过dna这个数组得到一个新的值

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main ()
{
    int i,j,t,dna[15],dish[50],dish2[50];
    cin>>t;
    while(t--)
    {
        memset(dna,0,sizeof(dna));
        memset(dish,0,sizeof(dish));
        memset(dish2,0,sizeof(dish2));
        for (i=0; i<10; i++)
            cin>>dna[i];
        dish[20]=1;
        for (i=1; i<=50; i++)
        {
            for (j=1; j<=40; j++)
            {
                switch(dish[j])
                {
                case 0:
                    cout<<" ";
                    break;
                case 1:
                    cout<<".";
                    break;
                case 2:
                    cout<<"x";
                    break;
                case 3:
                    cout<<"W";
                }
                dish2[j]=dish[j];
            }
            cout<<endl;
            for (j=1; j<=40; j++)
            {
                int temp=dish2[j]+dish2[j-1]+dish2[j+1];
                dish[j]=dna[temp];
            }
        }
        if (t>=1) cout<<endl;
    }
    return 0;
}


你可能感兴趣的:(uva)