[Blue Bridge Cup] Exam Training 2013 C++A Group Question 4 Inverted Price Tag

Upside-down price tag 

 Xiao Li's store exclusively sells sample TVs that are off the shelves in other stores, which can be called a sample TV store.
    The prices are all 4 digits (that is, thousands of yuan).
    In order to make the price clear and convenient, Xiao Li used a pre-made price tag similar to a digital tube, as long as he painted the numbers with a colored pen (see p1.jpg).
    This kind of price tag has a characteristic, for some figures, it is also reasonable when viewed backwards. For example: 1 2 5 6 8 9 0 is all right. In this way, if the brand hangs upside down, it may completely change to another price. For example: 1958 upside down is: 8561, which is a few thousand yuan off!! 
    Of course, in most cases, you can’t read it backwards. For example, 1110 is It cannot be reversed, because 0 cannot be used as the starting number.

    One day, the tragedy finally happened. A clerk accidentally hung up two price tags in the store. And the TV sets of these two price brands have been sold!
    Fortunately, the price difference is not big. One of the price tags lost more than 200, while the other price tag made more than 800. Together, they made more. 558 yuan.
    Please calculate based on this information: What is the correct price of the price tag that lost money?

The answer is a 4-digit integer, please submit the number directly through the browser.
Note: Do not submit the answering process or other supporting explanation content.

 Enumerate all four-digit numbers that can be reversed

Reverse it and make the difference with the original value, save it in two sets, traverse the sum of the two sets, and the result is 558 as the correct answer.

Answer: 9088

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

//

void i2s(int num, string &str){
	stringstream ss;
	ss << num;
	ss >> str;
} 
void s2i(string &str,  int &num){
	stringstream ss;
	ss << str;
	ss >> num;
} 
char to(char x){
	if(x == '6')
		return '9';
	else if(x == '9')
		return '6';
	else 
		return x;
}
string reverse(const string & str){
	string res;
	for(int i = 3; i >= 0; i--){
		res.insert(res.end(), to(str[i]));
	}
	return res;
}

struct price{
	int a, b, c;
};

vector<price> v1;	//-200
vector<price> v2;	//+800

int main(int argc, char** argv) {
	
	for(int i = 1000; i < 10000; i++){
		string str;
		i2s(i, str);
		if(str.find('3') != string::npos || str.find('4') != string::npos || 
					str.find('7') != string::npos || str.rfind('0') == 3 ){
			continue;
		}
		string r = reverse(str);
		int r_int;
		s2i(r, r_int);
		int plus = r_int - i;
		if(plus > -300 && plus < -200){
			price p = {i, r_int, plus};
			v1.push_back(p);
		}else if(plus > 800 && plus < 900){
			price p = {i, r_int, plus};
			v2.push_back(p);
		}
		for(int i = 0; i < v1.size(); i++){
			for(int j = 0; j < v2.size(); j++){
				if(v1[i].c  + v2[j].c == 558){
					cout << v1[i].a << " "<< v1[i].b << " " << v1[i].c << endl;
					cout << v2[j].a << " "<< v2[j].b << " " << v2[j].c << endl;
					break;
				}
			}
		}
	}
		
	
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/115100099