颠倒的价牌
小李的店里专卖其它店中下架的样品电视机,可称为:样品电视专卖店。
其标价都是4位数字(即千元不等)。
小李为了标价清晰、方便,使用了预制的类似数码管的标价签,只要用颜色笔涂数字就可以了
这种价牌有个特点,对一些数字,倒过来看也是合理的数字。如:1 2 5 6 8 9 0都可以。这样一来,如果牌子挂倒了,有可能完全变成了另一个价格,比如:1958 倒着挂就是:8561,差了几千元啊!!
当然,多数情况不能倒读,比如,1110 就不能倒过来,因为0不能作为开始数字。
有一天,悲剧终于发生了。某个店员不小心把店里的某两个价格牌给挂倒了。并且这两个价格牌的电视机都卖出去了!
庆幸的是价格出入不大,其中一个价牌赔了2百多,另一个价牌却赚了8百多,综合起来,反而多赚了558元。
请根据这些信息计算:赔钱的那个价牌正确的价格应该是多少?
分析:由题意可知,该题目中提到了几种不可能的数字(存在3,4,7)的数字而且最后一个数不能是0。
思想:
//枚举可颠倒的数1110,2220就不行
//颠倒的-正常的为-200多和颠倒的-正常的为+500多分别记录下来
//再遍历两部分分别相加减最后为588的拿出来(颠倒过的多800多的-颠倒过少200多的)
#include<iostream>
#include<sstream>
#include<vector>
using namespace std;
//int转string
void itos(int num,string& str){
stringstream temp;
temp<<num;
temp>>str;
}
//string转int
void stoi(int& num,string str){
stringstream temp;
temp<<str;
temp>>num;
}
//当个数字颠倒转换
char to(char x){
if(x=='6'){
return '9';
}else if(x=='9'){
return '6';
}else{
return x;
}
}
//颠倒
string reversestr(const string temp){
string num;
for(int i=3;i>=0;i--){
char x=temp[i];
int a=3-i;
num.insert(num.end(),to(temp[i]));
}
return num;
}
/*定义
*now:颠倒前
*after:颠倒后
*cha:差值
*/
typedef struct price{
int now,after,cha;
}price;
vector<price>v1;//存-200多的
vector<price>v2;//存+800多的
int main(){
for(int i=1000;i<10000;i++){
string str;
itos(i,str);
//如果存在3,4,7还有末尾数为零的就continue不做判断
if(str.find('3')!=string::npos||str.find('4')!=string::npos||
str.find('7')! =string::npos||str.rfind('0')==3){
continue;
}
string r=reversestr(str);
int r_int;
stoi(r_int,r);
price p1;
p1.now=i;
p1.after=r_int;
p1.cha=r_int-i;
if(p1.cha<-200&&p1.cha>-300){
v1.push_back(p1);
}else if(p1.cha>800&&p1.cha<900){
v2.push_back(p1);
}
}
//循环结束v1,v2分别存储-200的和800的数
//cout<<reversestr("1958")<<endl;
for(int i=0;i<v1.size();i++){
for(int j=0;j<v2.size();j++){
if((v1[i].cha+v2[j].cha)==558){
cout<<"v1[i].now:"<<v1[i].now<<" after:"<<v1[i].after
<<" cha:"<<v1[i].cha<<endl;
cout<<"v2[j].now:"<<v2[j].now<<" after:"<<v2[j].after
<<" cha:"<<v2[j].cha<<endl;
}
}
}
system("pause");
return 0;
}
通过对每一个满足条件的数全布打印出来见下图:
但是在写答案的时候要写原数不要写错写成颠倒后的
答案:9088