北航机试题2017(题目+代码)

 

https://apriljia.com/2018/07/12/%E5%8C%97%E8%88%AA%E4%B8%8A%E6%9C%BA%E8%AF%95%E9%A2%982017%EF%BC%88%E9%A2%98%E7%9B%AE%E4%BB%A3%E7%A0%81%EF%BC%89/

1.求中位数的位置

先输入一个整形数字N,接着输入N个无序的数字。要求输出升序排列后的中位数,以及该中位数输入的次序。如果N为偶数,则输出有二个中位数,如果N为奇数,输出最中间的数即可。

样例1:

输入:5

9 2 7 1 6

输出:6 5

样例2:

输入:6

9 6 7 1 2 3

输出:3 6

6 2

 

#include "stdafx.h"
#include 
#include 
#include "math.h"
#include "stdio.h"
#include "string.h"
#include 
#include 
#include 
#include 

using namespace std;
int max(int x,int y){
	return x>y?x:y;
}

struct number{
	int value;
	int index;
	bool operator <(number b){
		return value>n;
	for(int i=0;i>num[i].value;
		num[i].index=i;
	}
	sort(num,num+n);

	if(n&1==1) cout<

 

2.查找未定义变量

 

输入两个C语言语句,第一句为正常的C语言变量定义语句,符合C语言语法要求,变量间可以有多个空格,包含数组,指针定义等。第二句为变量运算语句,要求输出第二个C语言语句中未定义的变量。

样例:

输入: int x12, y=1, num_stu=89, a[30], *p;

Sum=num+x12*y;

输出:

Sum num

#include "stdafx.h"
#include 
#include 
#include "math.h"
#include "stdio.h"
#include "string.h"
#include 
#include 
#include 
#include 

using namespace std;
int max(int x,int y){
	return x>y?x:y;
}

int nextvar(string str,int &curindex,string &nextstr){				
	if(curindex>=str.size()-1) return 0;
	int startindex,endindex;
	int delindex;
	while(str[curindex]=='*'||str[curindex]==' ') curindex++;   //è¥3y±?á????°?àóàμ?????oí*
	startindex=curindex;
	endindex=str.find(",",startindex);
	if(endindex==string::npos) endindex=str.find(";",startindex);

	curindex=endindex+1;
	if(!(endindex>startindex)) return 0;
	nextstr=str.substr(startindex,endindex-startindex);

	delindex=nextstr.find("[");									
	if(delindex!=string::npos){
		nextstr.erase(delindex,nextstr.size());
	}
	delindex=nextstr.find("=");									
	if(delindex!=string::npos){
		nextstr.erase(delindex,nextstr.size());
	}
	if(!nextstr.empty()) return 1;
	else return 0;
}

int nextvar2(string str,int &curindex,string &nextstr){
	if(curindex>=str.size()-1) return 0;
	int startindex,endindex;
	while(str[curindex]=='*'||str[curindex]==' ') curindex++;   //è¥3y±?á????°?àóàμ?????oí*
	startindex=curindex;
	for(int i=startindex;i='a')&&(str[i]<='z'))
			  ||((str[i]>='A')&&(str[i]<='Z'))
			  ||((str[i]>='0')&&(str[i]<='9'))
			  ||(str[i]=='_')))
		break;
	}
	endindex=i;
	curindex=endindex;
	if(str[curindex]=='[') {
		curindex++;
		while(str[curindex]!=']') curindex++;
	}
	if(str[curindex]==']') curindex++;
	curindex++;
	while((str[curindex]>='0')&&(str[curindex]<='9')) curindex++;

	//cout<startindex)) return 0;
	nextstr=str.substr(startindex,endindex-startindex);
	if(!nextstr.empty()) return 1;
	else return 0;
}
int main(int argc, char* argv[])
{
	string str1,str2,nextstr;
	char s[100];
	vector var;

	gets(s);
	str1=s;
	gets(s);
	str2=s;
	int length=str1.size();
	int curindex=0;
	while(str1[curindex]==' '){							
		str1.erase(curindex,curindex+1);
	}
	
	curindex=str1.find(" ");								
	while(nextvar(str1,curindex,nextstr)){				
		var.push_back(nextstr);
	}

	curindex=0;
	while(str2[curindex]==' '){							
		str2.erase(curindex,curindex+1);
	}
	while(nextvar2(str2,curindex,nextstr)){				
		int result=0;
		for(int i=0;i::iterator iter=var.begin();iter!=var.end();iter++) 
	//	cout<<*iter<

 

3.找家谱成员

 

输入若干行,每一行的第一个输入为家谱中的某成员,该行接着输入的信息为每个孩子姓名。最后一行的输入为要求查找的两个家谱成员的关系。要求:根据输入的家谱成员信息,建立二叉树家谱关系图,并输出二位待查找成员在家谱中的关系,包括输出他们最邻近的共同祖先以及在家谱中相差的层次数。

样例:

输入:

YE SHU MEI

SHU GE MEI1

BA SELF MEI2

GE SON1 SON2

SON2 MEI1

输出:

SHU 1

#include "stdafx.h"
#include 
#include 
#include "math.h"
#include "stdio.h"
#include "string.h"
#include 
#include 
#include 
#include 

using namespace std;
int max(int x,int y){
	return x>y?x:y;
}
struct treeNode{
	treeNode *parent;
	char lchild[20];
	char rchild[20];
	char name[20];
	int depth;
	treeNode(){
		depth=0;
		parent=NULL;
	}
};

treeNode node[100];

int main(int argc, char* argv[])
{
	char line[100];
	char nametmp[3][20];
	int spaceindex,curstart=0;
	int n=0,curcount=0,c=0;
	int nodeindex=0;
	bool readend=false,find=0,find1=0,find2=0;
	while(gets(line)){
		for(int i=0;iname<name<name<depthdepth)
				{
					p1=p1->parent;
					cout<<"p2:"<name<depth==p2->depth&&p1!=p2)
				{
					p1=p1->parent;
					p2=p2->parent;
					cout<<"p1:"<name<name<name<

 

你可能感兴趣的:(C++,算法,北航机试)