苏州大学2015年计算机研究生复试上机试题(c语言版)

Introduction

The project will read flight data from an input file andflight path requests from another input file and output the requiredinformation.

 

Your Task

Your program should determine if aparticular destination airport can be reached from a particular originatingairport within a particular number of hops.

A hop (leg of a flight) is a flight from one airport to anotheron the path between an originating and destination airports.

For example, the flight plan from PVG to PEK might be PVG→ CAN → PEK. So PVG → CAN would be a hop and CAN → PEK would be a hop.

 

Input Data Files

Path InputFile(PathInput.txt)

This input file will consist of a number of single origination/destinationairport pairs (direct flights). The first line of the file will contain aninteger representing the total number of pairs in the rest of the file.

6

[PVG, CAN]

[CAN, PEK]

[PVG, CTU]

[CTU, DLC]

[DLC, HAK]

[HAK, LXA]

 

PathRequest File(PathRequest.txt)

This input file will contain a sequence of pairs oforigination/destination airports and a max number of hops. The first line ofthe file will contain an integer representing the number of pairs in the file.

2

[PVG, DLC, 2]

[PVG, LXA, 2]

 

OutputFile(Output.txt)

For each pair in the Path Request File, your programshould output the pair followed by “YES” or “NO” indicating that it is possible to get from the origination to destinationairports within the max number of hops or it is not possible,respectively.

[PVG, DLC, YES]

[PVG, LXA, NO]

Assumptions youcan make:

You may make the following simplifying assumptions inyour project:

l  C/C++ is allowed to be used.

l  All airport codes will be 3 letters and will be in allcaps

l  Origination/destination pairs are unidirectional. Toindicate that both directions of flight are possible, two entries would appearin the file. For example, [PVG, PEK] and [PEK, PVG] would have to be present inthe file to indicate that one could fly from Shanghaito Beijing and from Beijingto Shanghai.

苏州大学2015年计算机研究生复试上机试题(c语言版)_第1张图片

题意理解:

给定一个图,任意输入图中两点,输出这两个点是否能在给定最大跳数内连通

例如

[PVG, DLC, 2] 给定最大跳数是2,实际跳数是2 未超过最大跳数 输出YES
[PVG, LXA, 2]给定最大跳数是2,实际跳数是4 超过最大跳数 输出NO

程序使用方法:

在D盘根目录下建立PathInput.txt文件(注意文件名一个字母都不能错,包括大小写)输入:

6
[PVG, CAN]
[CAN, PEK]
[PVG, CTU]
[CTU, DLC]
[DLC, HAK]

[HAK, LXA]

在D盘根目录下建立PathRequest.txt文件(注意文件名一个字母都不能错,包括大小写)输入:

2
[PVG, DLC, 2]
[PVG, LXA, 2]

注意空格问题,例如[PVG, DLC, 2]中,逗号与DLC之间有一个空格,逗号与2之间有一个空格,题目中的输入也是有空格的,所以从文件读取时,不能用fscanf函数,要用fgets函数,注意fgets函数回车换行也是要读进来的。

在D盘根目录下建立Output.txt(注意文件名一个字母都不能错,包括大小写)文件,运行程序,就会在Output.txt文件里生成如下文字:

[PVG, DLC, YES]

[PVG, LXA, NO]

#include
#include 
#include 
#include

struct node_info
{
	char no[4];//结点的名称;
	char joint[20][4];//与该结点相连的所有结点的集合
	int degree=0;//结点的度数
	int visited;//访问标志位 
}node[100];

int num_node=0,flag;// num_node是图中结点的个数 

int ni(char a[])//找出字符结点在结构体数组中的位置 
{
	for(int i=0;i=0&&x[i]>='0'&&x[i]<='9';i--)
		num+=(x[i]-'0')*(int)pow(10.0,j++);
	return num;
}
int main()
{
	char a[4],b[4],x[15],ch;
	int n,m,max_hop;
	FILE *fpi,*fpr,*fop;
	fpi=fopen("D:\\PathInput.txt","r");
	fpr=fopen("D:\\PathRequest.txt","r");
	fop=fopen("D:\\Output.txt","w");
	if(!fpi||!fpr||!fop)
		return 1;
	fgets(x,15,fpi);
	n=get_num(x,2);//将读取的数字字符串转化为int类型
	for(int i=0;i

你可能感兴趣的:(SZU)