A:骰子比大小
题解:对于每三种牌,按照组合的强弱进行依次判断,对于同种类型的组合,需要进行同类型判断(也就是比较其和大小),然后考虑该类型单独出现的情况(若A单独出现则A赢,若B单独出现则B赢),特殊的,三个点数一样,还需要特殊考虑235的情况(A为三个点数一样,B为235或者A为235,B为三个点数一样)。
方法1:
#include<bits/stdc++.h>
using namespace std;
int a[4],b[4];
int main() {
int p1,p2;
cin>>a[1]>>a[2]>>a[3]>>b[1]>>b[2]>>b[3];
sort(a+1,a+4);
sort(b+1,b+4);
//强弱依次是 1-4 0:最牛 2 3 5
if(a[1]==2&&a[2]==3&&a[3]==5){
p1=0;
}else if(a[1]==a[2]&&a[1]==a[3]&&a[2]==a[3]){ //小A
p1=1;
}else if(a[2]-a[1]==1&&a[3]-a[2]==1){
p1=2;
}else if(a[1]==a[2]&&a[2]!=a[3]||a[1]==a[3]&&a[3]!=a[2]||a[2]==a[3]&&a[1]!=a[2]){
p1=3;
}else{
p1=4;
}
//小B
if(b[1]==2&&b[2]==3&&b[3]==5){
p2=0;
}else if(b[1]==b[2]&&b[1]==b[3]&&b[2]==b[3]){
p2=1;
}else if(b[2]-b[1]==1&&b[3]-b[2]==1){
p2=2;
}else if(b[1]==b[2]&&b[2]!=b[3]||b[1]==b[3]&&b[3]!=b[2]||b[2]==b[3]&&b[1]!=b[2]){
p2=3;
}else{
p2=4;
}
//特判 2 3 5 最牛
if(p1==0&&p2==1){
cout<<"A";
return 0;
}else if(p2==0&&p1==1){
cout<<"B";
return 0;
}
//判断胜负
if(p1==1&&p2!=1||p1==2&&p2==3||p1==2&&p2==4){
cout<<"A";
}else if(p2==1&&p1!=1||p2==2&&p1==3||p2==2&&p1==4){
cout<<"B";
}else{
//按照点数总和分胜负
int t1=a[1]+a[2]+a[3];
int t2=b[1]+b[2]+b[3];
if(t1>t2) cout<<"A";
else cout<<"B";
}
return 0;
}
方法2:
#include<bits/stdc++.h>
using namespace std;
int a,b,c,x,y,z;
bool _same(int a,int b,int c) {
if(a==b&&b==c) {
return true;
} else {
return false;
}
}
bool _continue(int a,int b,int c) {
int maxn=max(a,max(b,c));
int minn=min(a,min(b,c));
int midn=a+b+c-maxn-minn;
if(maxn-midn==1&&midn-minn==1) {
return true;
} else {
return false;
}
}
bool _twoSame(int a,int b,int c) {
if(a==b && b!=c) return true;
if(a==c && b!=c) return true;
if(b==c && a!=b) return true;
return false;
}
bool _235(int a,int b,int c) {
int maxn=max(a,max(b,c));
int minn=min(a,min(b,c));
int midn=a+b+c-maxn-minn;
if(minn==2&&midn==3&&maxn==5) {
return true;
} else {
return false;
}
}
int main() {
cin>>a>>b>>c>>x>>y>>z;
//三个一样
if(_same(a,b,c)&&_same(x,y,z)) {
if(a+b+c>x+y+z) {
cout<<"A";
} else {
cout<<"B";
}
return 0;
}
//三个一样,一个235
else if(_same(a,b,c)&&_235(x,y,z)) {
cout<<"B";
return 0;
}
//一个235,三个一样
else if(_235(a,b,c)&&_same(x,y,z)) {
cout<<"A";
return 0;
}//有一个是三个一样
else if(_same(a,b,c)){
cout<<"A";
return 0;
}//令一个是三个一样
else if(_same(x,y,z)){
cout<<"B";
return 0;
}
//三个连续
if(_continue(a,b,c)&&_continue(x,y,z)) {
if(a+b+c>x+y+z) {
cout<<"A";
} else {
cout<<"B";
}
return 0;
}//一个连续
else if(_continue(a,b,c)){
cout<<"A";
return 0;
}//另一个连续
else if(_continue(x,y,z)){
cout<<"B";
return 0;
}
//两个一样,另一张不一样
if(_twoSame(a,b,c)&&_twoSame(x,y,z)) {
if(a+b+c>x+y+z) {
cout<<"A";
} else {
cout<<"B";
}
return 0;
}//有一个是两个一样,另一张不一样
else if(_twoSame(a,b,c)){
cout<<"A";
return 0;
}//另一个是两个一样,另一张不一样
else if(_twoSame(x,y,z)){
cout<<"B";
return 0;
}
//除以上其他的组合
if(a+b+c>x+y+z) {
cout<<"A";
} else {
cout<<"B";
}
return 0;
}
B 变大变大变成回文数
题解:对于输入的数据,先判断其是否是回文,若是回文则继续进行下一次操作,否则将其数值增加一个,再判断其是否为回文直到是回文为止,然后给ans答案增加其累加的数值,最后中断。等待循环结束,输出ans即可。
参考代码:
#include<bits/stdc++.h>
using namespace std;
int n,x,a,ans;
bool pd(int x){
int t=x,y=0;
while(x){
y=y*10+x%10;
x/=10;
}
if(y==t) return true;
else return false;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>x;
if(pd(x)) continue;
for(int j=1;;j++){
if(pd(x+j)){
ans+=j;
break;
}
}
}
cout<<ans;
return 0;
}
C 移动序列
题解:题意说了n个数移动了a次,每次移动b个,相当于移动了a×b个数字,若a×b超过了n,那就相当于移动了(a×b)%n个数字,也就是把(a×b)%n个数字往后挪动了。即先输出(a×b)%n+1到n,再输出1到(a×b)%n。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
int n,a,b;
signed main(){
cin>>n>>a>>b;
int x=(a*b)%n;
for(int i=x+1;i<=n;i++){
cout<<i<<' ';
}
for(int i=1;i<=x;i++){
cout<<i<<' ';
}
return 0;
}
D 新年快乐
题解:对于每个左右区间,先从主串中截取左区间到右区间的字符串,若子串中都是'a'字符直接可退出,那么就需要统计子串中是否存在不是'a'的字符,若存在,说明可以找到上一个字符串,若不存在,直接输出NULL以及Happy Chinese New Year!继续进行下一轮。否则从截取的字符串倒序来进行判断,看看该位置是否为字符'a',若是则将其改为字符'z',否则将字符减少一个,然后中断。最后看看截取的子串能否在主串中找到,若能找到输出子串和Happy New Year!否则输出子串和Happy Chinese New Year!
参考代码:
#include<bits/stdc++.h>
using namespace std;
string s;
int n,l,r;
int main() {
cin>>s>>n;
while(n--) {
cin>>l>>r;
l--,r--;
int flag = 1;
string t=s.substr(l,r-l+1);
for (int i = 0; i < t.length(); ++i) {
if (t[i] != 'a') {
flag = 0;
break;
}
}
if (flag) {
cout << "NULL\nHappy Chinese New Year!\n";
continue;
}
for (int i = t.length() - 1; i >= 0; --i)
if (t[i] == 'a') {
t[i] = 'z';
} else {
--t[i];
break;
}
if(s.find(t)!=-1)
cout<<t<<endl<<"Happy New Year!"<<endl;
else
cout<<t<<endl<<"Happy Chinese New Year!"<<endl;
}
return 0;
}