总结

1:Speeding Ticket

这题我的思路是,把每英里的速度统计在一个数组里。因为最极端的情况就是奶牛每英里都变换速度。它肯定不是最优的做法,但是因为题目的数据范围不大,所以可以用这种方法来做这题。

#include<bits/stdc++.h>
using namespace std;
int a[110],b[110],x[110],s[110],q[110],w[110];
int main()
{
	freopen("speeding.in","r",stdin);freopen("speeding.out","w",stdout);
	int n,m,ans=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i]>>x[i];
		a[i]=a[i]+a[i-1];
		for(int j=a[i-1]+1;j<=a[i];j++)
		{
			q[j]=x[i];
		}
	} 
	for(int i=1;i<=m;i++)
	{
		cin>>b[i]>>s[i];
		b[i]=b[i]+b[i-1];
		for(int j=b[i-1]+1;j<=b[i];j++)
		{
			w[j]=s[i];
		}
	}
	for(int i=1;i<=100;i++)
	{
		if(q[i]<w[i])
		{
			ans=max(ans,w[i]-q[i]);
		}
	}
	cout<<ans;
return 0;
}

2:The Cow-Signal

这题我的思路是:

首先:题目要我们打印一个k倍的矩阵,那我们首先要打印一个原矩阵

for(int j=1;j<=m;j++)
{
    for(int x=1;x<=k;x++)
    {
        cout<<a[i][j];
    }
}

然后:我们要打印一个每个字符都要变成原来的k倍

for(int i=1;i<=n;i++)
{
	for(int j=1;j<=m;j++)
	{
		for(int x=1;x<=k;x++)
		{
			cout<<a[i][j];
		}
	}
	cout<<endl;
}

最后:我们要打印一个k倍的矩阵

for(int i=1;i<=n;i++)
{
	for(int r=1;r<=k;r++)
	{
		for(int j=1;j<=m;j++)
		{
			for(int x=1;x<=k;x++)
			{
				cout<<a[i][j];
			}
		}
		cout<<endl;
	}
}

3:Shell Game

这题我的思路是:如果鹅卵石开始在一号位,那么它猜对了几次。如果在二号位,那么它猜对了几次. . . . . . 。

#include<bits/stdc++.h>
using namespace std;
int a[110],b[110],g[110];
int main()
{
	freopen("shell.in","r",stdin);freopen("shell.out","w",stdout);
	int n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i]>>g[i];
	}
	for(int i=1;i<=3;i++)
	{
		int m=i,sum=0;
		for(int j=1;j<=n;j++)
		{
			if(a[j]==m||b[j]==m)
			{
				if(a[j]==m)
				{
					m=b[j];
				}
				else
				{
					m=a[j]; 
				}
			}
			if(m==g[j])
			{
				sum++;
			}
			if(sum>ans)
			{
				ans=sum;
			}
		}
	}
	cout<<ans;
return 0;
}

4:乒乓球

这题我的思路是:输入完W和L之后判断是不是E,如果不是继续输入,如果是跳出。

另外:这题的输入比较麻烦,要用while循环。 不同的输入

1:cin,不读空格,不读换行

2:getline,读空格,不读换行

#include<bits/stdc++.h>
using namespace std;
string s;
char a[1000005];
long long x=0;
int y;
int main()
{
	freopen("pingpong.in","r",stdin);freopen("pingpong.out","w",stdout);
	while (cin>>s) 
	{
		for (int i=0;i<s.size();i++) 
		{
			a[x++]=s[i];
		}
	}
	long long s1=0,s2=0;
	for (int i=0;i<x;i++) 
	{
		if (a[i]=='E') 
		{
			cout<<s1<<":"<<s2<<endl;
			break;
		}
		if (a[i]=='W') 
		{
			s1++;
		}
		if (a[i]=='L') 
		{
			s2++;
		}
		if ((s1>=11 || s2>=11) && abs(s1-s2)>=2) 
		{
			cout<<s1<<":"<<s2<<endl;
			s1=0;
			s2=0;
		}
	}
	cout<<endl;
	s1=0,s2=0;
	for (int i=0;i<x;i++) 
	{
		if (a[i]=='E') 
		{
			cout<<s1<<":"<<s2;
			break;
		}
		if (a[i]=='W') 
		{
			s1++;
		}
		if (a[i]=='L') 
		{
			s2++;
		}
		if ((s1>=21 || s2>=21) && abs(s1-s2)>=2) 
		{
			cout<<s1<<":"<<s2<<endl;
			s1=0;
			s2=0;
		}
	}
	return 0;
}