蓝桥杯 颠倒的价牌 C++
思路:
1.由于最大价钱<10000(要是100000,亲测要一二十秒的计算时间,要是1000000,暴力法就凉了),所以二重循环遍历所有情况
2.先写了一个函数判断一个数是否可以颠倒,又写了一个函数求颠倒后的数
代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define ll long long
using namespace std;
int flag(int n) {//判断是否可以倒置
if(n%10==0) return 0;
while(n>0) {
int t=n%10;
if(t==3||t==4||t==7)
return 0;
n/=10;
}
return 1;
}
int reverse(int n) {//求倒置后的数
int ans=0;
while(n>0) {
int t=n%10;
if(t==6) t=9;
else if(t==9) t=6;
ans=ans*10+t;
n/=10;
}
return ans;
}
int main() {
int ans1,ans2;//价牌1和价牌2
for(int i=1000; i<=10000; i++) {//二重循环遍历所有情况
for(int j=1000; j<=10000; j++) {
if(flag(i)&&flag[j]) {
int t1=i-reverse(i);
int t2=j-reverse(j);
if(t1>200&&t1<300&&t2<-800&&t2>-900&&t2+t1==-558) {
ans1=i,ans2=j;//验证结果是否正确
cout<<ans1<<" "<<ans2<<endl;
cout<<reverse(ans1)<<" "<<reverse(ans2)<<endl;
cout<<ans1-reverse(ans1)<<" "<<reverse(ans2)-ans2<<endl;
cout<<ans1<<endl;//输出结果
break;
}
}
}
}
return 0;
}