【USACO1.2】解题报告

前言

以后将会不定期刷USACO的题目。每做完一小章会写一份解题报告。这一小章里面较简单或者并不是很重要的题目就会直接放在里面。而比较重要的题目就会单独写博客,在这里面放链接。
这一章很简单,全部都是很基础的题目。就直接在这里面一笔带过。
USACO:http://train.usaco.org


1.2.1.Submitting Solutions

思路:

没啥好说的。

代码:

/*
ID:ssl_zyc2
TASK:test
LANG:C++
*/

#include <iostream>
#include <cstdio>
using namespace std;

int a,b;

int main()
{
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
	cin>>a>>b;
	cout<<a+b<<endl;
	return 0;
}

1.2.2.Your Ride Is Here

思路:

暴力模拟。对于每个字符串的每个字符,按照方法分开处理。

代码:

/*
ID:ssl_zyc2
TASK:ride
LANG:C++
*/

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

char ch1[8],ch2[8];

int main()
{
	freopen("ride.in","r",stdin);
	freopen("ride.out","w",stdout);
    long long a=1,b=1;
    cin>>ch1;
    cin>>ch2;
    int l1,l2;
    l1=strlen(ch1);
    l2=strlen(ch2);  //取出长度
    for(int i=0;i<l1;i++) 
       a=a*(ch1[i]-'A'+1);
    for(int i=0;i<l2;i++) 
       b=b*(ch2[i]-'A'+1);  //模拟
    a%=47;
	b%=47;
    if(a==b) cout<<"GO";
     else cout<<"STAY";
    cout<<endl;
    return 0;
}

1.2.5.Greedy Gift Givers

思路:

简单的爆模。

代码:

/*
ID:ssl_zyc2
TASK:gift1
LANG:C++
*/

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

int n,k,m,a[101],ok,per;
char name[101][1001],c[1001],person[1001];

int main()
{
	freopen("gift1.in","r",stdin);
	freopen("gift1.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
     cin>>name[i];
    for (int i=1;i<=n;i++)
    {
        cin>>person;
        for (per=1;per<=n;per++)
         if (strcmp(person,name[per])==0) break;
        scanf("%d%d",&m,&k);
        a[per]-=m;
        for (int j=1;j<=k;j++)
        {
            cin>>c;
            for (int q=1;q<=n;q++)
            {
                if (strcmp(c,name[q])==0) 
                {
                    a[q]+=m/k;
                    break;
                }
            } 
        }
        if (k==0) a[i]+=m;
        else a[per]+=m%k;
    }
    for (int i=1;i<=n;i++)
    {
        cout<<name[i]<<" "<<a[i];
        cout<<endl;
    } 
    return 0;
}

1.2.6.Friday the Thirteenth

思路:

一天一天处理,时间慢,代码易懂,好打。

代码:

/*
ID:ssl_zyc2
TASK:friday
LANG:C++
*/

#include <iostream>
#include <cstdio>
using namespace std;

const int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};  //储存每一月的天数
int n,ans[8],day,month,year,k;

int leap_year()  //闰年
{
    if (year%400==0) return 1;
    if (year%4==0&&year%100!=0) return 1;
    return 0;
}

void p(int x)
{
    cout<<ans[x]<<" ";
}

int main()
{
	freopen("friday.in","r",stdin);
	freopen("friday.out","w",stdout);
    day=1;
    month=1;
    year=1900;
    k=1;
    cin>>n;
    while (year<=1900+n-1)
    {
        day++;
        k++;
        if (k>7) k=1;
        if (day==13) ans[k]++;
        if (leap_year()==1&&month==2&&day==29)
        {
            day++;
            k++;
        }
        if (k>7) k=1;
        if (day>m[month])
        {
            month++;
            day=1;
        }
        if (month>12)
        {
            month=1;
            year++;
        }
    }
    p(6);p(7);p(1);p(2);p(3);p(4);cout<<ans[5];
    printf("\n");
    return 0;
}

1.2.7Broken Necklace

思路:

枚举每一个位置开始,分别向左和向右,并记录答案。取最大值。

代码:

/*
ID:ssl_zyc2
TASK:beads
LANG:C++
*/

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

int n,maxn,sum;
char c[1001],col;

void left(int x)
{
    while (c[x]==col||col=='w'||c[x]=='w')
    {
        if (c[x]!='w') col=c[x];
        sum++;
        x--;
        if (sum==n) return;
        if (x<0) x=n-1;
    }
}

void right(int x)
{
    while (c[x]==col||col=='w'||c[x]=='w')
    {
        if (c[x]!='w') col=c[x];
        sum++;
        x++;
        if (sum==n) return;
        if (x>=n) x=0;
    }
}

int main()
{
	freopen("beads.in","r",stdin);
	freopen("beads.out","w",stdout);
    scanf("%d",&n);
    cin>>c;
    for (int i=0;i<=n;i++)
    {
        col=c[i];
        left(i);
        if (sum==n) return printf("%d\n",n)%1;
        if (i!=n) col=c[i+1]; else col=c[0];
        if (i!=n) right(i+1); else right(0);
        if (sum==n) return printf("%d\n",n)%1;
        maxn=max(sum,maxn);
        sum=0;
    }
    return  printf("%d\n",maxn)%1;
}
原文地址:https://www.cnblogs.com/hello-tomorrow/p/11998539.html