蓝桥杯 算法提高 排列式

这题经分析之后有两种可能存在的表达式

一种形如

6952 = 4 x 1738

另一种形如

5796 = 42 x 138


所以该题就变成了遍历所有可能的形如以上表达式的结果集

并将其按指定顺序和结构输出出来


结构可能还好控制,那顺序呢

如何将遍历出来的答案按顺序输出出来呢

其实很简单,将那一个int型的表达式经过stringstream转换成string

然后放进set集合里就可以了,因为C++ stl的set集合会自动的帮我们排序


ac代码如下

#include 
#include 
#include 
using namespace std;
set ans;
stringstream ss;

int main()
{    
	for(int a=1;a<10;a++){   //暴力循环出一个不重不漏的1到9的序列
		for(int b=1;b<10;b++){
			if(a!=b){
				for(int c=1;c<10;c++){
					if(a!=c&&b!=c){
						for(int d=1;d<10;d++){
							if(a!=d&&b!=d&&c!=d){
								for(int e=1;e<10;e++){
									if(a!=e&&b!=e&&c!=e&&d!=e){
										for(int f=1;f<10;f++){
											if(a!=f&&b!=f&&c!=f&&d!=f&&e!=f){
												for(int g=1;g<10;g++){
													if(a!=g&&b!=g&&c!=g&&d!=g&&e!=g&&f!=g){
														for(int h=1;h<10;h++){
															if(a!=h&&b!=h&&c!=h&&d!=h&&e!=h&&f!=h&&g!=h){
																for(int i=1;i<10;i++){
																	if(a!=i&&b!=i&&c!=i&&d!=i&&e!=i&&f!=i&&g!=i&&h!=i){
																		int sum = f*1000+g*100+h*10+i;
																		int chen1 = a*10+b;
																		int chen2 = c*100+d*10+e;
																		
																		int chen3 = a;
																		int chen4 = b*1000+c*100+d*10+e;
																	
																		if(sum==chen1*chen2){
																			ss.str("");
																			ss<::iterator it;
	for(it=ans.begin();it!=ans.end();it++){ //用iterator遍历 同时通过标识符确认结果输出的格式
		string anss = *it;
		if(anss[anss.length()-1]!='i')
		cout<


你可能感兴趣的:(蓝桥杯,算法提高,STL)