苏州大学计算机专业2013年复试上机真题

题目描述:

Introduction
The project will read flight data from an input file and flight path requests from another input file and output the required information.
Your Task
Your program should determine if a particular destination airport can be reached from a particular originating airport within a particular number of hops.
A hop (leg of a flight) is a flight from one airport to another on 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.

输入描述:

Path Input File(PathInput.txt)
This input file will consist of a number of single origination/destination airport pairs (direct flights). The first line of the file will contain an integer 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]

Path Request File(PathRequest.txt)
This input file will contain a sequence of pairs of origination/destination airports and a max number of hops. The first line of the file will contain an integer representing the number of pairs in the file.
2
[PVG, DLC, 2]
[PVG, LXA, 2]

输出描述:

Output File(Output.txt)
For each pair in the Path Request File, your program should output the pair followed by “YES” or “NO” indicating that it is possible to get from the origination to destination airports within the max number of hops or it is not possible, respectively.

[PVG, DLC, YES]
[PVG, LXA, NO]

Assumptions you can make:
1.You may make the following simplifying assumptions in your project:
C/C++ is allowed to be used.
2.All airport codes will be 3 letters and will be in all caps
3.Origination/destination pairs are unidirectional. To indicate that both directions of flight are possible, two entries would appear in the file. For example, [PVG, PEK] and [PEK, PVG] would have to be present in the file to indicate that one could fly from Shanghai to Beijing and from Beijing to Shanghai.

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void inputData(struct flight f[],int* count);
void inputRequestData(struct request req[],int* count);
int judgeRequest(char* ori,char* des,struct flight f[],int count1,int hopnum);
struct flight{
	char origination[4];
	char destination[4];
};
struct request{
	struct flight fl;
	int maxhop;
};

int main(){
	struct flight f[100];
	int flightnum=0;
	struct request req[10];
	int requestnum=0;
	int i;
	FILE *ft;
	if((ft=fopen("D:\\2019Post\\pat\\pat\\Output.txt","w"))==NULL){
		printf("can not open the file");
		exit(0);
	}
	inputData(f,&flightnum);
	inputRequestData(req,&requestnum);
	for(i=0;i<requestnum;i++){
		if(judgeRequest(req[i].fl.origination,req[i].fl.destination,f,flightnum,req[i].maxhop))
			fprintf(ft,"[%s,%s,yes]\n",req[i].fl.origination,req[i].fl.destination);
		else
			fprintf(ft,"[%s,%s,no]\n",req[i].fl.origination,req[i].fl.destination);
	}

}
void inputData(struct flight f[],int* count){
	FILE *fp;
	int i,j,k;
	char temp;
	if((fp=fopen("D:\\2019Post\\pat\\pat\\PathInput.txt","r"))==NULL){
		printf("can not open the file");
		exit(0);
	}
	fscanf(fp,"%d",count);
	for(i=0;i<*count;i++){
		do{
			fscanf(fp,"%c",&temp);
		}while(temp<'A'||temp>'Z');
		for(j=0;j<3;j++){
			f[i].origination[j]=temp;
			fscanf(fp,"%c",&temp);
		}
		f[i].origination[j]='\0';
		do{
			fscanf(fp,"%c",&temp);
		}while(temp<'A'||temp>'Z');

		for(k=0;k<3;k++){
			f[i].destination[k]=temp;
			fscanf(fp,"%c",&temp);
		}
		f[i].destination[k]='\0';
	}
	fclose(fp);
}
void inputRequestData(struct request req[],int* count){
	FILE *fa;
	int i,j,k;
	char temp;
	if((fa=fopen("D:\\2019Post\\pat\\pat\\PathRequest.txt","r"))==NULL){
		printf("can not open the file");
		exit(0);
	}
	fscanf(fa,"%d",count);
	for(i=0;i<*count;i++){
		do{
			fscanf(fa,"%c",&temp);
		}while(temp<'A'||temp>'Z');
		for(j=0;j<3;j++){
			req[i].fl.origination[j]=temp;
			fscanf(fa,"%c",&temp);
		}
		req[i].fl.origination[j]='\0';
		do{
			fscanf(fa,"%c",&temp);
		}while(temp<'A'||temp>'Z');

		for(k=0;k<3;k++){
			req[i].fl.destination[k]=temp;
			fscanf(fa,"%c",&temp);
		}
		req[i].fl.destination[k]='\0';
		do{
			fscanf(fa,"%c",&temp);
		}while(temp<'1'||temp>'9');
		req[i].maxhop=temp-'0';
	}
	fclose(fa);

}

int judgeRequest(char* ori,char* des,struct flight f[],int count1,int hopnum){
	int i;
	for(i=0;i<count1&&hopnum>0;i++){
		if(strcmp(ori,f[i].origination)==0){
			if(strcmp(des,f[i].destination)==0){
				return 1;
			}else{
				if(judgeRequest(f[i].destination,des,f,count1,hopnum-1)){
					return 1;
				}
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值