《考研机试》(二)机试题精讲

(一)题1:

《考研机试》(二)机试题精讲_第1张图片

 

解题思路:通过二维数组存取输入数据,之后通过一个函数判断是否存在'E''A''S''Y'四个字母,最后根据返回值打印difficult/easy

     如何判断:传入二维数组的每一行(一行等于一个输入数据),定义2个指针,然后while循环读取

代码:

#include 
#include  
#include 
#include 
using namespace std;

bool match(char a[]){
	int i = 0, j = 0;
	char temp[] = {'E', 'A', 'S', 'Y'};
	while( i<1000&&j<=3 ){
		if( a[i]==temp[j] ){
			i++;
			j++;
		}else{
			i++;
		}
	}
	if( i>=1000 ){
		return false;
	}else{
		return true;
	}
} 

int main(void){
	cout << "输入字符串个数:" << " ";
	int n;
	cin >> n;
	cout << "Sample In:" << endl;
	char str[][1000] = {'0'};
	for(int i=0; i> str[i];
	}
	cout << "Sample Out:" << endl;
	for(int i=0; i 
 

(二)题2:

《考研机试》(二)机试题精讲_第2张图片

 

 解题思路:这道题目更像是一道数学题,找规律,找小三角形数目

《考研机试》(二)机试题精讲_第3张图片

 

 代码:

#include 
#include  
#include 
#include 
using namespace std;

int main(void){
	cout << "输入几组数据:" << " ";
	int n; 
	cin >> n;
	int a[n];
	cout << "Simple In:" << endl; 
	for(int i=0; i> a[i];
	}
	cout << "Simple Out:" << endl; 
	for(int i=0; i 
 

(三)题3:

《考研机试》(二)机试题精讲_第4张图片

 

 解题思路:这道题目首先通过定义二维数组(2行N列)存储数据,第一行存储每个人的折扣率,第二行存储每个人的折扣上限

     然后需要根据折扣率进行排序,从小到大(折扣率越小,折扣力度越大),根据总价格t分类,如果t超过所有人的折扣

     上限之和,那么肯定需要额外掏钱,否则则根据排序结果进行依次扣钱

代码:

#include 
#include  
#include 
#include 
using namespace std;

//数组a已经根据折扣率从小到大排序完成 
int zhekou(double a[2][100], int n, double t){
	
	int sum1 = 0;
	int sum2 = 0;
	int sum = 0;//一共应该付多少钱
	 
	for(int i=0; it ){//全部折扣金额>总价格,说明可以用折扣付钱 
		for(int j=0; j0){//如果不够付 
				sum += (int)(a[1][j]*a[0][j]);//sum加上折扣后的钱 
			}
			int i = j+1;//下一个人 
			if( i> N;
	cout << "输入总价格:" << " ";
	cin >> T; 
	double a[2][100]; 
	for(int i=0; i> temp1;
		a[0][i] = temp1;//折扣率 
		cin >> temp2;
		a[1][i] = temp2;//折扣上限 	
	}
	
	//按照折扣率排序,从小到大 
	for(int i=0; i a[0][j+1] ){
				
				double tempp1 = a[0][j];
				a[0][j] = a[0][j+1];
				a[0][j+1] = tempp1;
				
				double tempp2 = a[1][j];
				a[1][j] = a[1][j+1];
				a[1][j+1] = tempp2;
			}
		}
	}
	
	//传入zhekou(a, N, T)函数中的数组a已经有序了 
	int number = zhekou(a, N, T);
	cout << "最小金额" << number << endl; 
	
	return 0;
}

 

你可能感兴趣的:(《考研机试》(二)机试题精讲)