1. 程式人生 > >【USACO1.2】解題報告

【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;
}