A 考试周破防
A题应该算是签到题,直接写一个string数组保存周一到周四的课,然后判断输出即可。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int a;
string s[4]={"Higher mathematics","Linear algebra","Principle of computer composition","Database system concept"};
cin>>a;
if(a<5)
cout<<"You were studying "<<s[a-1]<<"!"<<endl;
else
cout<<"You were playing games!"<<endl;
return 0;
}
B 咖啡店
B题进行简单的判断5元纸币是否足够,如果足够的话尽可能多的用5元,剩下的用1元,如果不够的话就用全部的5元,其他用1元
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n,s;
cin>>n>>s;
int sum=0;
if(s/5>n)
{
cout<<n<<" ";
s-=n*5;
}
else {
cout<<s/5<<" ";
s=s%5;
}
cout<<s;
return 0;
}
C kiki和bob玩取石子
C题博弈论,我们可以很简单的判断出4的倍数后手取胜,其他的先手取胜
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n;
cin>>n;
if(n%4!=0)
cout<<"kiki";
else cout<<"bob";
return 0;
}
D 猴王kiki分桃
D题思维题,判断能否多n-1个桃子,不能的话就取所有桃子即可
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
long long s[50];
int main()
{
long long n,l,r,k;
cin>>n>>l>>r;
if(r/n-1>=l/n)
{
cout<<n-1;
}
else
{
cout<<r%n;
}
return 0;
}
E 很二的拆分
二进制按位与运算即可
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
long long s[50];
int main()
{
long long a;
cin>>a;
if(a%2==1)
cout<<"-1";
else{
int t=0;
int b=2;
while(a>=b)
{
if((a&b)!=0)
{
s[t++]=b;
}
b=b*2;
}
for(int i=t-1;i>=0;i--)
cout<<s[i]<<" ";
}
return 0;
}
F 构造字符串
贪心,如果相同的话就while循环直到不同的那一个。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
char s[3000],s1[3000];
int main()
{
int l;
cin>>l;
cin>>s;
int k=0;
for(int i=0;i<l;)
{
if(s[i]>s[l-1])
{
s1[k++]=s[l-1];
l--;
}
else if(s[i]<s[l-1]){
s1[k++]=s[i];
i++;
}
else{
int p=i;
while(s[p]==s[l-(p-i)-1])
p++;
if(s[p]<s[l-(p-i)-1])
{
s1[k++]=s[i++];
}
else
{
s1[k++]=s[l-1];
l--;
}
}
}
cout<<s1;
return 0;
}
G 信号之旅
如果两点的x或y相等,就判断中间是否有障碍,否则,计算|x1-x2|+|y1-y2|。有障碍的话+2就可以了。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int x1,x2,y1,y2,xp,yp;
cin>>x1>>y1>>x2>>y2>>xp>>yp;
if(x1==x2&&x1==xp)
{
if((yp<y1&&yp>y2)||(yp>y1&&yp<y2))
cout<<abs(y2-y1)+2<<endl;
else cout<<abs(y2-y1)<<endl;
}
else if(y1==y2&&y1==yp)
{
if((xp<x1&&xp>x2)||(xp>x1&&xp<x2))
cout<<abs(x2-x1)+2<<endl;
else cout<<abs(x2-x1)<<endl;
}
else cout<<abs(x2-x1)+abs(y2-y1)<<endl;
}
return 0;
}
H 小球滚动
小球的碰撞可以忽视(自己模拟试试),发现这一点之后就很简单了。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int l;
cin>>l;
int n;
cin>>n;
int max=0,min=0;
while(n--)
{
int a;
cin>>a;
if(a>max) max=a;
if(l-a>max) max=l-a;
if(a>l-a)
{
if(l-a>min)
min=l-a;
}
else{
if(a>min)
min=a;
}
}
cout<<min<<" "<<max;
return 0;
}
I kiki看球
从这题开始难度迅速上升,看似很麻烦,其实分类讨论就行了
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
long long a,b,t,sum=1,a1=0,b1=0;
cin>>t;
for(long long i=0;i<t;i++)
{
cin>>a>>b;
if(b1>a1)
{
if(a>=b1)
{
if(a>b)
{
sum+=b-b1+1;
}
else
{
sum+=a-b1+1;
}
}
}
else if(a1==b1)
{
sum+=(a-a1)>(b-b1)?(b-b1):(a-a1);
}
else
{
if(b>=a1)
{
if(a>b)
{
sum+=b-a1+1;
}
else
{
sum+=a-a1+1;
}
}
}
a1=a;
b1=b;
}
cout<<sum;
return 0;
}
J 跳一跳
long long很重要,定义一个已知的最远点max=1,从第一个点开始判断,若点i所能跳到的最远距离i+a[i]>max,则更新max=i+a[i],知道i>max或max>瓷砖数n为止。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
long long n;
long long maxlen=1;
cin>>n;
for(long long i=1;i<=maxlen;i++){
long long s;
cin>>s;
if(i+s>maxlen) maxlen=i+s;
if(maxlen>=n)
{
maxlen=n;
break;
}
}
cout<<maxlen;
return 0;
}
K Jay的小迷弟
*2一定比+1多(众所周知,别杠),所以能连续的就不拆开,除非没有零散的Jay。有一点卡时间,所以要优化代码,查找Jay的代码如下(kmp也可以):
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
char s[200000000];
int main()
{
scanf("%s",s);
long long j=0,a=0,y=0,jay=0;
long long k=strlen(s);
for(int i=0;i<k;)
{
if(s[i]=='J'&&i<k-2)
{
if(s[i+1]=='a')
{
if(s[i+2]=='y')
{
jay++;
i+=2;
}
else{
a++;
j++;
i++;
}
}
else{
j++;
}
}
else if(s[i]=='J')
j++;
else if(s[i]=='a')
a++;
else if(s[i]=='y')
y++;
i++;
}
long long sum=0;
if(j>=a&&y>=a)
sum=a;
else if(y>=j&&a>=j)
sum=j;
else if(j>=y&&a>=y)
sum=y;
if(sum==0&&jay>0)
{
sum+=1;
jay--;
}
sum=sum%(1000000007);
while(jay>0)
{
sum=2*sum%(1000000007);
jay--;
}
printf("%lld",sum);
return 0;
}
L 反转卡片
用dp可以ac,没做