蓝桥杯2013A-4颠倒的价牌(字符串与整型的相互转换)

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

void i2s(int x, string &s) {
	stringstream ss;
	ss << x;
	ss >> s;
}
void s2i(string &s,int &x) {
	stringstream ss;
	ss << s;
	ss >> x;
}
char to(char s) {
	if (s == '6')
		return '9';
	else if (s == '9')
		return '6';
	return s;
}
string reverse(const string &str) {
	string ans;
	for (int k = 3; k >= 0; k--) {
		ans.insert(ans.end(), to(str[k]));//在ans.end()位置插入字符to(str[k])
	}
	return ans;
}

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

vector<price> v1;
vector<price> v2;

int main() {
	//cout << reverse("1958") << endl;
	//string s = "a";
	//int n;
	//s2i(s, n);
	//cout <<n << endl;
	for (int i = 1000; i <= 9999; i++) {
		string s;
		i2s(i, s);
	    if (s.find('3') != string::npos || s.find('4') != string::npos || s.find('7') != string::npos || s.rfind('0') == 3)
			continue;
		string str=	reverse(s);
		int x_int;
		s2i(str, x_int);
		int plus = x_int-i;
		if (plus> -300 && plus < -200) {
			price p = { i,x_int,plus };
			v1.push_back(p);
		}else if(plus> 800 && plus < 900) {
			price p = { i,x_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 << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhao2chen3/article/details/87986194