算法基础知识——暴力求解

算法基础知识——暴力求解

目录:

  1. 基础知识
    1. 枚举
    2. 模拟(图形排版、日期、其他)
  2. 应用实例
    1. 反序数【清华大学】
    2. 对称平方数【清华大学】
    3. 与7无关的数【北京大学】
    4. 百钱买百鸡问题【华为机试】
    5. Old Bill【上海交通大学】
    6. 输出梯形【清华大学】
    7. 叠框【王道机试】
    8. Repeater【北京大学】
    9. Hello World for U【浙江大学】
    10. 今年的第几天?【清华大学】
    11. 打印日期【华中科技大学】
    12. 日期累加【北京理工大学】
    13. 日期差值【上海交通大学】
    14. Day of Week【上海交通大学】
    15. 日期类【北京理工大学】
    16. 剩下的树【清华大学】
    17. 手机键盘【清华大学】
    18. xxx定律【浙江大学】
    19. Grading【浙江大学】
    20. 路径打印【上海交通大学】
    21. 坠落的蚂蚁【北京大学】

一、基础知识

1、枚举

  • 枚举是指对每个可能的解进行逐一判断,直到找到符合题目要求的答案。

2、模拟

  • 模拟类题目在考研机试题目中出现频率很高,不涉及特别高深的知识,只需利用程序实现题目的要求。
  • 常见题型:
    • 图形排版:按照规定输出字符,主要考查对输出格式的把握。
    • 日期问题:把握问题题面的核心规律。
    • 其他模拟:类型多变。

二、应用实例

1、题目描述:设N是一个四位数,它的9倍恰好是其反序数(例如:1234的反序数是4321),求N的值。【清华大学】

  • 输入格式:程序无任何输入数据。
  • 输出格式:输出题目要求的四位数,如果结果有多组,则每组结果之间以回车隔开。
  • 样例输入:无
  • 样例输出:无

示例代码:

#include <iostream>

using namespace std;

int main(){
	int tmp, answer;
	for(int i = 1000; i <= 1111; i++){
		answer = 0, tmp = i;
		while(tmp != 0){
			answer = answer * 10 + tmp % 10;
			tmp /= 10;
		}
		if(i * 9 == answer){
			cout << i << endl;
		}
	}

	return 0;
}

2、题目描述:打印所有不超过256,其平方具有对称性质的数。如2,11就是这样的数,因为2*2=4,11*11=121。【清华大学】

  • 输入格式:无输入
  • 输出格式:输出具有题目要求的性质的数。如果输出数据不止一组,各组数据之间以回车隔开。
  • 样例输入:无
  • 样例输出:无

示例代码:

#include <iostream>

using namespace std;

int main(){
	int answer, tmp;
	for(int i = 0; i <= 256; i++){
		tmp = i * i, answer = 0;
		while(tmp != 0){
			answer = answer * 10 + tmp % 10;
			tmp /= 10;
		}
		if(answer == i * i){
			cout << i << endl;
		}
	}
	return 0;
}

3、题目描述:一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7, 则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和。【北京大学】

  • 输入格式:案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)
  • 输出格式:对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。
  • 样例输入:
    • 21
  • 样例输出:
    • 2336

示例代码:

#include <iostream>

using namespace std;

bool IsSevenRelate(int n){
	if(n % 7 == 0){
		return true;
	}
	while(n != 0){
		if(n % 10 == 7){
			return true;
		}
		n /= 10;
	}
	return false;
}

int main(){
	int N;
	while(cin >> N){
		int answer = 0;
		for(int i = 1; i <= N; i++){
			if(!IsSevenRelate(i)){
				answer += i * i;
			}
		}
		cout << answer << endl;
	}
	return 0;
}

4、题目描述:公元前五世纪,我国古代数学家张丘建在《算经》一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
接口说明原型:int GetResult(vector &list):list  鸡翁、鸡母、鸡雏组合的列表(指针指向的内存区域保证有效),返回-1 失败,0 成功。【华为机试】

  • 输入格式:输入任何一个整数,即可运行程序。
  • 输出格式:鸡翁、鸡母、鸡雏组合的列表
  • 样例输入:
    • 1
  • 样例输出:
    • 0 25 75
    • 4 18 78
    • 8 11 81
    • 12 4 84

示例代码:

#include <iostream>
#include <vector>

using namespace std;

const double FATHER_PRICE = 5;
const double MOTHER_PRICE = 3;
const double CHILD_PRICE = 1/3.0;
const int TOTAL_MONEY = 100;

int GetResult(vector<int> &list){
	for(int i = 0; i <= TOTAL_MONEY / FATHER_PRICE; i++){
		for(int j = 0; j <= (TOTAL_MONEY - i * FATHER_PRICE) / MOTHER_PRICE; j++){
			for(int k = 0; k <= (TOTAL_MONEY - i * FATHER_PRICE - j * MOTHER_PRICE) / CHILD_PRICE; k++){
				if(i * FATHER_PRICE + j * MOTHER_PRICE + k * CHILD_PRICE == TOTAL_MONEY
					&& i + j + k == TOTAL_MONEY){
					list.push_back(i);
					list.push_back(j);
					list.push_back(k);
				}
			}
		}
	}
	if(list.size() == 0){
		return -1;
	}else{
		return 0;
	}
}

int main(){
	int input;
	vector<int> list;
	while(cin >> input){
		GetResult(list);
		for(int i = 0; i < list.size(); i += 3){
			cout << list[i] << " " << list[i + 1] << " " << list[i + 2] << endl;
		}
		list.clear();
	}
	return 0;
}

5、题目描述: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.
  • 样例输入:
    • 72
    • 6 7 9
    • 5
    • 2 3 7
    • 78
    • 0 0 5
  • 样例输出:
    • 3 2 511
    • 9 5 18475
    • 0

示例代码:

#include <iostream>

using namespace std;

const int MAX_PRICE = 99999;

int main(){
	int N, x, y, z;
	while(cin >> N){
		cin >> x >> y >> z;
		int tmp = x * 1000 + y * 100 + z * 10;
		int max_price = MAX_PRICE, result_i, result_j;
		for(int i = 1; i < 10; i++){
			for(int j = 0; j < 10; j++){
				if((tmp + i * 10000 + j) % N == 0 && N <= max_price){
					max_price = (tmp + i * 10000 + j) / N;
					result_i = i;
					result_j = j;
				}
			}
		}
		if(max_price != MAX_PRICE){
			cout << result_i << " " << result_j << " " << max_price << endl;
		}else{
			cout << 0 << endl;
		}
	}
	return 0;
}

6、输入一个高度h,输出一个高度为h,上底边长度为h的梯形。【清华大学】

  • 输入格式:一个整数h(1≤ h ≤ 1000)
  • 输出格式:h所对应的梯形
  • 样例输入:
    • 4
  • 样例输出:
    •          ****
    •       ******
    •    ********
    • **********

示例代码:

#include <iostream>

using namespace std;

int main(){
	int level, tmp;
	while(cin >> level){
		tmp = level;
		for(int i = 1; i <= level; i++){
			int space = 2 * (tmp - i), star = tmp + 2 * (i - 1);
			while(space != 0){
				cout << " ";
				space--;
			}
			while(star != 0){
				cout << "*";
				star--;
			}
			cout << endl;
		}
	}
	return 0;
}

7、题目描述:把一个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。【王道机试】

  • 输入格式:输入是一个三元组,分别是:外筐尺寸n(n为满足0 < n < 80的奇整数),中心花色字符,外筐花色字符,后二者都为Ascii可见字符。
  • 输出格式:输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉,叠筐与叠筐之间应有一行空格。
  • 样例输入:
    • 11
  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值