王道计算机考研机试指南----暴力求解

暴力求解–枚举

例题

1、abc
设a、b、c均是0到9之间的数字,abc,bcc是两个三位数,且有abc+bcc=532,求满足条件的所有a、b、c的值。
输入描述:
题目没有任何输入。
输出描述:
请输出所有满足题目条件的a、b、c的值。 a、b、c之间用空格隔开。 每个输出占一行
代码:

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	for(int a = 0;a <= 9; ++a){
		for(int b = 0;b <= 9;++b){
			for(int c = 0;c <= 9;++c){
				if(a * 100 + b * 110 + c * 12 == 532){
					printf("%d %d %d\n",a,b,c);
				}
			}
		}
	}
} 

输出结果:
在这里插入图片描述

2、反序数
设N是一个四位数,它的9倍恰好是其反序数(例如:1234的反序数是4321)
求N的值
输入描述
程序无任何输入数据。
输出描述:
输出题目要求的四位数,如果结果有多组,则每组结果之间以回车隔开。
代码:

#include <iostream>
#include <cstdio>
using namespace std;
//int main(){
//	for(int a = 1;a <= 9;a++){
//		for(int b = 0;b <= 9;b++){
//			for(int c = 0;c <= 9;c++){
//				for(int d = 0;d <= 9;d++){
//					if((1000*a+100*b+10*c+d)*9==1000*d+100*c+10*b+a){
//						printf("%d%d%d%d",a,b,c,d);
//					}
//				}
//			}
//		}
//	}
//}
int Reverse(int x){
	int revx=0;
	while(x!=0){
		revx*=10;
		revx+=x%10;
		x /=10;
	}
	return revx; 
}
int main(){
	for(int i=1000;i<=9999;++i){
		if(i*9==Reverse(i)){
			printf("%d\n",i);
		}
	}
}

输出结果:
在这里插入图片描述

3、对称平方数1
打印所有不超过256,其平方具有对称性质的数。如2,11就是这样的数,因为22=4,1111=121。
输入描述:
无任何输入数据
输出描述:
输出具有题目要求的性质的数。如果输出数据不止一组,各组数据之间以回车隔开
代码:

#include <iostream>
#include <cstdio>
using namespace std;
int Reverse(int x){
	int revx=0;
	while(x!=0){
		revx=revx*10;
		revx=revx+x%10;
		x=x/10; 
	}
	return revx; 
}
int main(){
	for(int i=0;i<=256;i++)
	{
		if(i*i==Reverse(i*i)){
			printf("%d\n",i);
		}
	}
} 

输出结果:
在这里插入图片描述

习题

1、与7无关的数
一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7, 则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和。
输入描述:
案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)
输出描述:
对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。
代码:

#include <iostream>
#include <cstdio>
using namespace std;
int isRelevant(int a){
	if(a%7==0){
		return 0; 
	}
	else{
		while(a!=0){
			if(a%10==7){
				return 0;
			}
			a = a/10;
		}
		return -1;
	}
}
int main(){
	int x;
	int result=0;
	cin>>x;
	for(int i=0;i<=x;i++){
		if(isRelevant(i)==-1){
			result=result+i*i; 
		}
	}
	printf("%d\n",result);
}

输出结果:
在这里插入图片描述

2、百鸡问题
用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一种小鸡,分别记为x只,y只,z只。编程求解x,y,z所有可能解。
输入描述:
测试数据有多组,输入n。
输出描述:
对于每组输入,请输出x,y,z所有可行解,按照x,y,z依次增大的顺序输出。
代码:

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
	int x,y,z,n;
	cin>>n;
	for(x=0;5*x<n;x++){
		for(y=0;3*y<=n;y++){
			z=100-x-y;
			if(z>=0&&15*x+9*y+z<=3*n){
	     		printf("x=%d,y=%d,z=%d\n",x,y,z);
			}
		}
	}
		
} 

输出结果:
在这里插入图片描述

3、Old BIll
Among grandfather’s papers a bill was found. 72 turkeys $679 The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted _), for they are faded and are illegible. What are the two faded digits and what was the price of one turkey? We want to write a program that solves a general version of the above problem. N turkeys $XYZ The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the turkeys cost the same price. Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. In case that there is more than one candidate for the original price, the output should be the most expensive one. That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.
输入描述:
The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys. In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $XYZ.
输出描述:
For each case, output the two faded digits and the maximum price per turkey for the turkeys.
代码:

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
	int n,x,y,z,price;
	bool flag;
	while(cin>>n>>x>>y>>z){
		flag=false;
		for(int i=9;i>=1;i--){
			for(int j=9;j>=0;j--){
				int sp=i*10000+x*1000+y*100+z*10+j;
				if(sp%n!=0)
				    continue;
				price = sp/n;
				cout<<i<<" "<<j<<" "<<price<<endl;
				flag=true;
				break;
			}
			if(flag==true)
			    break;
		}
		if(flag==false){
			cout<<0<<endl;
		}
	}
}

输出结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值