突然发现前面的题目不一定是4个成套出现的。
其实看下题目的分数,就知道题目的难度。
20分的肯定是不需要算法的水题,25~30分的可能是简单dp,或简单搜索。
pat的题目主要是把题目得读懂了,感觉很容易出现bug。
题目地址:点击打开链接
1005
题目大意:给你一个字符串,把他们按位加起来,然后用英文输出即可。
AC代码:
//water
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
char str[105];
int ans[105];
char mp[10][10]={"zero","one","two","three","four","five",
"six","seven","eight","nine"};
int main()
{
int sum,i;
while(cin>>str)
{
sum=0;
int len=strlen(str);
for(i=0;i<len;i++)
sum+=str[i]-'0';
int t=0;
if(sum==0)
{
puts("zero");
continue;
}
while(sum)
{
ans[t++]=sum%10;
sum/=10;
}
cout<<mp[ans[t-1]];
for(i=t-2;i>=0;i--)
cout<<" "<<mp[ans[i]];
cout<<endl;
}
return 0;
}
/*
12345
0
*/
1006
题目大意:给你n个人,分别给名字,进来的时间,出去的时间。找出最早进来的人和最晚出去的人。
直接排序就好了。。
AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
char s1[25];
char s2[25];
char s3[25];
}nod[1005];
int cmp1(node p1,node p2)
{
if(strcmp(p1.s2,p2.s2)<0)
return 1;
return 0;
}
int cmp2(node p1,node p2)
{
if(strcmp(p1.s3,p2.s3)>0)
return 1;
return 0;
}
int main()
{
int n,i;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>nod[i].s1>>nod[i].s2>>nod[i].s3;
sort(nod,nod+n,cmp1);
cout<<nod[0].s1<<" ";
sort(nod,nod+n,cmp2);
cout<<nod[0].s1<<endl;
}
return 0;
}
/*
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
*/
1007
题目大意:找一个最大字串和,还是一维的,不过wa点无数。。
1.这个需要输出的是 首尾元素,不是下标。
2.答案为负数,输出0,第一个元素和最后一个元素。
3.i,j都是最小的,如果不是最小,如有前缀0,需要往前推。
dp方程为dp[i]=max(dp[i-1],0)+a[i];
AC代码:
//一维最大连续子串和。。
//注意这个题目是要输出首尾元素,而不是下标。。
//这题bug真多。。
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=100005;
ll a[maxn],dp[maxn];
ll ma(ll m,ll n)
{
if(m>n) return m;
return n;
}
int main()
{
int n,i;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>a[i];
ll res=-1e9;
int l,r;
dp[0]=a[0];
if(dp[0]>res)
{
res=dp[0];
r=0;
}
for(i=1;i<n;i++)
{
dp[i]=ma(dp[i-1],0)+a[i];
if(dp[i]>res)
{
res=dp[i];
r=i;
}
}
l=r;
ll tmp=res-a[l];
while(tmp)
{
l--;
tmp-=a[l];
}
while(l>=1&&a[l-1]==0)
l--;
if(res==0) cout<<"0 0 0"<<endl;
else if(res<0) cout<<"0"<<" "<<a[0]<<" "<<a[n-1]<<endl;
else cout<<res<<" "<<a[l]<<" "<<a[r]<<endl;
}
return 0;
}
/*
5
-2 3 4 5 1
5
4 -2 1 4 -2
5
-2 -1 -3 -4 -1
10
-10 1 2 3 4 -5 -23 3 7 -21
4
-1 0 0 -1
4
0 1 2 3
4
0 0 0 0
10
0 2 3 4 -11 3 7 -5 -5 -5
10
3 7 -11 1 2 3 4 -5 -5 -5
*/
1008
题目大意:最开始在0层电梯,然后上一层需要6s,下一层需要4s,停一层需要5s,问你总共需要多长时间,直接计算即可。
20分water...
AC代码:
//water
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n;
int i,las;
while(cin>>n)
{
int x;
las=0;
int res=n*5;
for(i=0;i<n;i++)
{
cin>>x;
if(las>x)
res+=(las-x)*4;
else
res+=(x-las)*6;
las=x;
}
cout<<res<<endl;
}
return 0;
}
//3 2 3 1