苏州大学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.


题意理解:

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

例如

[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<stdio.h>
#include<string.h> 
#include<math.h> 
#include<stdlib.h>

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<num_node;i++)
		if(!strcmp(node[i].no,a))
			return i;
	return -1;//点不存在置为-1 
}

bool islink(char a[],char b[])//判断两结点是否连通 
{
	for(int i=0;i<node[ni(a)].degree;i++)
		if(!strcmp(node[ni(a)].joint[i],b))
			return true;
	return false;
}

void to_zero()//将每个结点的访问标志位置零 
{
	for(int i=0;i<num_node;i++)
		node[i].visited=0;
}

void DFS(char t[],char des[],int hop,int max_hop)//t表当前点,des表目的点,hop表示当前跳数,max_hop表示要求最大跳数 
{
	if(ni(t)!=-1&&!node[ni(t)].visited) //ni(t)!=-1不加也可以 
	{
		if(!strcmp(t,des))
		{
			if(hop<=max_hop)
				flag=1;
			return;
		}
		node[ni(t)].visited=1;
		for(int i=0;i<num_node;i++)
		{
			if(islink(t,node[i].no))
				DFS(node[i].no,des,hop+1,max_hop);		
		}		
	}
}

int get_num(char x[],int s)//从x字符串中提取数字 ,s表示字符串中数字个位数是倒数第几个字符,如[PVG, DLC, 2]中,2是第倒数第三个字符(算回车),s就赋值为3
{
	int num=0,j=0;
	for(int i=strlen(x)-s;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<n;i++)//构造图 
	{
		fgets(x,15,fpi);
		a[0]=x[1],a[1]=x[2],a[2]=x[3],a[3]=0;
		b[0]=x[6],b[1]=x[7],b[2]=x[8],b[3]=0;
		if(ni(a)!=-1)//如果输入结点不在图中 
			strcpy(node[ni(a)].joint[node[ni(a)].degree++],b);
		else//如果输入结点在图中 
		{
			strcpy(node[num_node].no,a);
			strcpy(node[num_node].joint[node[num_node++].degree++],b);
		}
		if(ni(b)!=-1)//如果输入结点不在图中 
			strcpy(node[ni(b)].joint[node[ni(b)].degree++],a);
		else//如果输入结点在图中 
		{
			strcpy(node[num_node].no,b);
			strcpy(node[num_node].joint[node[num_node++].degree++],a);			
		}
	}
	fgets(x,15,fpr);
	m=get_num(x,2);//将读取的数字字符串转化为int类型
	for(int i=0;i<m;i++)
	{
		flag=0;
		to_zero();
		fgets(x,15,fpr);
		a[0]=x[1],a[1]=x[2],a[2]=x[3],a[3]=0;
		b[0]=x[6],b[1]=x[7],b[2]=x[8],b[3]=0;
		max_hop=get_num(x,3);//[PVG, DLC, 2]所有类似这样的输入s都等于3
		DFS(a,b,0,max_hop);
		if(flag)
			fprintf(fop,"[%s, %s, YES]\n",a,b);
		else
			fprintf(fop,"[%s, %s, NO]\n",a,b);
	}
	fclose(fpi);
	fclose(fpr);
	fclose(fop);
	return 0;
} 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 研究生复试通常包括专业基础知识考察和技术能力测试等环节。在C语言试题方面,一般主要考察对C语言的基本语法和常用库函数的掌握程度,以及对算法和数据结构的理解与应用能力。以下是一些常见的C语言试题及答案示例: 1. C语言的基本数据类型有哪些?它们的内存空间分别是多少? 答案:C语言的基本数据类型包括int(4字节)、char(1字节)、float(4字节)、double(8字节)等。 2. 请写一个C语言程序,实现两个整数相加并输出结果。 答案: ```C #include <stdio.h> int main() { int a = 10; int b = 20; int sum = a + b; printf("两个整数的和为:%d\n", sum); return 0; } ``` 3. 请编写一个C语言程序,实现计算1到n的累加和,并输出结果。 答案: ```C #include <stdio.h> int main() { int n; int sum = 0; printf("请输入一个正整数n:"); scanf("%d", &n); for (int i = 1; i <= n; i++) { sum += i; } printf("1到%d的累加和为:%d\n", n, sum); return 0; } ``` 4. 请实现一个简单的冒泡排序算法,对输入的n个整数进行升序排序,并输出结果。 答案: ```C #include <stdio.h> void bubbleSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } int main() { int arr[] = {5, 2, 8, 9, 1}; int n = sizeof(arr)/sizeof(arr[0]); bubbleSort(arr, n); printf("排序结果为:"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } ``` 以上仅为一些基础的C语言试题及答案示例,研究生复试中具体的试题内容会根据学校和专业的要求而有所不同。希望以上回答能够对您有所帮助。 ### 回答2: 研究生复试中的C语言试题及答案因不同学校和不同专业可能有所不同,以下给出一个示例: 试题1:请写出用递归实现的阶乘函数,计算n的阶乘。 答案1: ```c #include <stdio.h> int factorial(int n) { if(n==0 || n==1) return 1; else return n*factorial(n-1); } int main() { int n; printf("请输入一个正整数n:"); scanf("%d", &n); printf("%d的阶乘为:%d\n", n, factorial(n)); return 0; } ``` 试题2:请编写函数swap,实现两个整数值的交换。 答案2: ```c #include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int num1, num2; printf("请输入两个整数值:"); scanf("%d %d", &num1, &num2); printf("交换前的两个数为:%d %d\n", num1, num2); swap(&num1, &num2); printf("交换后的两个数为:%d %d\n", num1, num2); return 0; } ``` 试题3:请编写程序,实现判断一个字符串是否为回文。 答案3: ```c #include <stdio.h> #include <string.h> int isPalindrome(char str[]) { int len = strlen(str); for(int i=0; i<len/2; i++) { if(str[i] != str[len-1-i]) { return 0; } } return 1; } int main() { char str[100]; printf("请输入一个字符串:"); gets(str); if(isPalindrome(str)) { printf("是回文字符串\n"); } else { printf("不是回文字符串\n"); } return 0; } ``` 以上只是给出的一个示例,实际的研究生复试C语言试题可能会更加复杂和多样化。希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值