2017年北理复试上机题

1、输入身份证号,通过计算比较校验位来判断身份证号是否正确。

如,aaaaaayyyymmddxxsp共18位,其中:

年份代码yyyy共4位。最后一位p为校验位。

校验规则是:

(1)对前17位数字的权求和 S=Sum(Ai*Wi),i=0,...,16

Ai:表示第i位置上的身份证号码数字值

Wi:表示第i位置上的加权因子

Wi:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

(2)计算模 Y=mod(S,11)

(3)通过模得到对应的校验码

        Y:0 1 2 3 4 5 6 7 8 9 10

校验码:1 0 X 9 8 7 6 5 4 3 2

例如,如果得到Y为9则最后的校验位p应该为3

如果校验位不是3,则该身份证号码不正确。

输入示例:

110130197606175317

输出示例:

110130197606175317 正确.

输入示例:

110200197501175220

输出示例:

应为:11020019750117522X
 

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main()
{
	char y[11]={'1','0','X','9','8','7','6','5','4','3','2'};
	int w[18]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	int s;
	string str;
	while(1)
	{
		cout<<"请输入。。。"<<endl;
		cin>>str;
		s=0;
		for(int i=0;i<19;i++)
		{
			if(i<18 && str[i]=='\0' || i==18 && str[i]!='\0')   //检查位数是否有误
			{
				cout<<"输入有误"<<endl;
				break;
			}
			else if(i<17)            //注意是前17位,易错
				s+=(str[i]-'0')*w[i];

		}
		if(i!=19)
			continue;
		s=s%11;
		if(y[s]==str[17])           //正确
		{
			cout<<str<<"正确"<<endl;

		}
		else
		{
			str[17]=y[s];
			cout<<"应为"<<str<<endl;
		}
	}
	return 0;
}
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main()
{
	int w[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	char p[18]={"10X98765432"};
	int s=0;
	string str;
	cin>>str;
	if(str.length()!=18)
	{
		cout<<"ERRO"<<endl;
		return 0;
	}
	for(int i=0;i<str.length()-1;i++)
	{
		s+=w[i]*(str[i]-'0');

	}
	int y=s%11;
	if(str[17]==p[y])
	{
		cout<<str<<"Yes"<<endl;
	}
	else
	{
		str[17]=p[y];
		cout<<"Should be"<<str<<endl;
	}
	return 0;
}

 

2、显示出如下数组中的所有元素,并使用二分查找法在数组中查找元素。

int a[]={-90,-32,12,16,24,36,45,59,98,120};

输入输出示例

-90   -32   12   16   24   36   45   59   98   120

请输入所要查找的元素:24

输出:第5个元素为24,比较次数为1

请输入所要查找的元素:120

输出:第10个元素,比较次数为4

请输入所要查找的元素:6

输出:查找失败 比较次数为3


 

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
	int a[10]={-90,-32,12,16,24,36,45,59,98,120};
	int i,find,left,mid,right,count;

	for(i=0;i<10;i++)
	{
		printf("%d ",a[i]);
	}
	printf("\n");

	while(1)
	{

		cout<<"请输入要查找的元素: ";
		cin>>find;
		left=0;
		right=9;
		count=0;
		while(left<=right)
		{
			count++;
			mid=(left+right)/2;
			if(a[mid]==find)
				break;
			else if(a[mid]<find)
				left=mid+1;
			else if(a[mid]>find)
				right=mid-1;

		}
		if(left<=right)
			printf("第%4d个元素为%4d,比较次数为%4d\n",mid+1,a[mid],count);
		else
			printf("查找失败,比较次数为%4d\n",count);
	}
}
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	int a[]={-90,-32,12,16,24,36,45,59,98,120};
	int i,n,j,x,mid;
	for(i=0;i<10;i++)
	{
		cout<<a[i]<<" ";
	}
	cout<<endl;
	printf("请输入所要查找的元素:");
	while(cin>>x)
	{
		i=0;j=9;
		n=0;
		while(i<=j)
		{
			mid=(i+j)/2;
			n++;
			if(a[mid]==x)
			{
				printf("第%d个元素是%d,比较次数为%d\n",mid+1,x,n);
				break;
			}
			else if(a[mid]>x)
			{
				j=mid-1;
			}
			else
				i=mid+1;

		}
		if(i>j)
		{
			printf("查找失败,查找次数为%d\n",n);
		}
		printf("请输入所要查找的元素:");
	}
}

3、输入学生个数以及每个学生的姓名和3门课程成绩:输出不及格学生的信息;按平均成绩排序,从高到低输出学生信息。

输入示例:

5

zhaoyi     70 80 91

zhanger   68 40 90

zhangsan 60 70 80

lisi            70 80 90

wangwu   52 70 100

输出示例:

*name: zhanger   score:68 40 99

*name: wangwu   score:52 70 100

[1]  name:zhaoyi     70 80 91

[2]  name:lisi           70 80 90

[3]  name:wangwu  52 70 100

[4]  name:zhangsan 60 70 80

[5]  name:zhanger   68 40 99

#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct 
{
	char name[30];
	int grade1,grade2,grade3;
	float avg;
}student;

bool cmp(student a,student b)
{
	return a.avg>b.avg;
}

int main()
{
	int n;
	student a;
	vector<student> vi;
	printf("请输入学生个数\n");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%s%d%d%d",a.name,&a.grade1,&a.grade2,&a.grade3);
		a.avg=(a.grade1+a.grade2+a.grade3)/3.0;
		vi.push_back(a);
	}
	for(vector<student>::iterator it=vi.begin();it!=vi.end();it++)
	{
		if(it->grade1<60 ||it->grade2<60 ||it->grade3<60 )
			printf("*name: %-16s%5d%5d%5d\n",it->name,it->grade1,it->grade2,it->grade3);
	}

	sort(vi.begin(),vi.end(),cmp);
	for(i=1,it=vi.begin();it!=vi.end();it++)
	{	
		printf("[%d] name: %-16s%5d%5d%5d\n",i,it->name,it->grade1,it->grade2,it->grade3);
	}

	return 0;

}

输入学生姓名时,名字后面有很多空格,但没关系,因为scanf()会跳过空格。
 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct 
{
	string name;
	int grade[3];
	float avg;
}student;
bool cmp(student a,student b)
{
	return a.avg>b.avg;
	
}
int main()
{
	int n;
	vector<student> vi;
	student a;
	cin>>n;

	int i,j;
	for(i=0;i<n;i++)
	{
		cin>>a.name;
		a.avg=0;
		for(j=0;j<3;j++)
		{
			cin>>a.grade[j];
			a.avg+=a.grade[j];
		}
		vi.push_back(a);
	}
	for(vector<student>::iterator it=vi.begin();it!=vi.end();it++)
	{
		for(i=0;i<3;i++)
		{
			if((*it).grade[i]<60)
			{
				break;
			}
		}
		if(i<3)
			cout<<"*name:"<<(*it).name<<" score:"<<(*it).grade[0]<<" "<<(*it).grade[1]<<" "<<(*it).grade[2]<<endl;
	}
	sort(vi.begin(),vi.end(),cmp);
	i=1;
	for(it=vi.begin();it!=vi.end();it++)
	{
		cout<<"["<<i++<<"] ";
		printf("%-16s ",(*it).name.c_str());
		cout<<(*it).grade[0]<<" "<<(*it).grade[1]<<" "<<(*it).grade[2]<<endl;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值