2017年北理工计算机复试上机

题目来源:https://blog.csdn.net/u014552756/article/details/79555984

代码原创

1.身份证号

#include<bits/stdc++.h>
using namespace std; 
int main(){
	int y = 0;
	char p[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '3', '2'};
	int w[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
	
	string s;
	while(cin >> s){
		for(int i = 0; i < 17; i++){
			int tmp = s[i] - '0';
			y += w[i] * tmp;
		}
		y %= 11;
		if( p[y] == s[17]){
			cout << s << " is correct."<<endl; 
		}else{
			s[17] = p[y];
			printf("it is supposed to be :");
			cout << s << endl; 
		}
	}
	return 0;
}

运行结果
2.二分查找

#include<bits/stdc++.h>
using namespace std;

int bySearch(vector<int> a, int num, int &cnt){
	int left = 0;
	int right = a.size() - 1;
	while(left <= right){
		cnt++;
		if(left == right){
			if(a[left] == num || a[right] == num)
				return left;
			else
				return -1;
		}
		int mid = (left + right ) / 2;
		if(num == a[mid]){
			return mid;
		}else if(num < a[mid]){
			right = mid - 1;
		}else{
			left = mid + 1;
		}
	}
	return -1;
} 
int main(){
	vector<int> a;
	int tmp;
	while(1){
		scanf("%d", &tmp);
		a.push_back(tmp);
		if(getchar() == '\n'){
			break;
		}
	}
	cout<<"Please input a number to search : ";
	int num;
	while(cin >> num){
		int cnt = 0;
		int pos = bySearch(a, num, cnt);
		if(pos != -1){
			cout << "Position is " << pos + 1  << ", and compare times are "<<cnt<<endl;
		}else{
			cout << "Search failed, and compare times are " << cnt <<endl;
		}
		cout<<"Please input a number to search : "; 	
	}
}

运算结果
3.成绩单

#include<bits/stdc++.h>
using namespace std;
struct student{
	string name;
	int score[3];
	double average;
};
bool cmp(struct student a, struct student b){
	return a.average > b.average;
} 
int main(){
	int n;
	cin >> n;
	vector<struct student>list; 
	for(int i = 0; i < n; i++){
		struct student tmp;
		cin >> tmp.name >> tmp.score[0]>> tmp.score[1]>> tmp.score[2];
		tmp.average = (tmp.score[0] + tmp.score[1] + tmp.score[2])*1.0/3;
		list.push_back(tmp);
		
	}
	for(int i = 0; i < n; i++){
		if(list[i].score[0] < 60 || list[i].score[1] < 60 || list[i].score[2] < 60){
			cout << "*name:" << list[i].name << "  " << "score: " 
			<< list[i].score[0]<<" "<<list[i].score[1]<<" "<< list[i].score[2]<<endl;
		}
	}
	sort(list.begin(), list.end(), cmp);
	for(int i = 0; i < n; i++){
		cout<<"["<<i+1<<"] name: "<<list[i].name<<"  "
		<< list[i].score[0]<<" "<<list[i].score[1]<<" "<< list[i].score[2]<<endl;
	}
	return 0;
}

运算结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值