码迷,mamicode.com
首页 > 其他好文 > 详细

地铁换乘

时间:2014-11-13 14:24:15      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   for   div   

【问题】

描写叙述:已知2条地铁线路,当中A为环线,B为东西向线路,线路都是双向的。经过的网站名分别例如以下,两条线交叉的换乘点用T1、T2表示。编敲代码,随意输入两个网站名称,输出乘坐地铁最少须要经过的车站数量(含输入的起点和终点,换乘网站仅仅计算一次)。
地铁线A(环线)经过车站:A1
 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18
地铁线B(直线)经过车站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15
输入:输入两个不同的站名
输出:输出最少经过的站数,含输入的起点和终点,换乘网站仅仅计算一次
输入例子:A1 A3
输出例子:3

【代码】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int jishi3(char *start, char *end)
{	
	int a1[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; //线路A到T1站的距离
	int a2[] = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6}; //线路A到T2站的距离
	int b1[] = {6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; //线路B到T1站的距离
	int b2[] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6}; //线路B到T2站的距离
	//线路A
	char aa[20][5] = {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "T1", "A10", "A11", "A12", "A13", "T2", "A14", "A15", "A16", "A17", "A18"};
	//线路B
	char bb[17][5] = {"B1", "B2", "B3", "B4", "B5", "T1", "B6", "B7", "B8", "B9", "B10", "T2", "B11", "B12", "B13", "B14", "B15"};
	int count=0;
	int i;
	int tmp1, tmp2;
	int posS, posE;
	//起点终点都是换乘站
	if (start[0] == 'T' && end[0] == 'T') 
		count = 6;
	//起点终点都在线路A上
	else if (start[0] != 'B' && end[0] != 'B') {
		for (i = 0; i < 20; i++) {
			if (strcmp(start, aa[i]) == 0)
				posS = i;
			if (strcmp(end, aa[i]) == 0)
				posE = i;
		}
		count = 1 + (((posS - posE) > 0) ? (posS - posE) : (posE - posS));
	}
	//起点终点都在线路B上
	else if (start[0] != 'A' && end[0] != 'A') {
		for (i = 0; i < 17; i++) {
			if (strcmp(start, bb[i]) == 0)
				posS = i;
			if (strcmp(end, bb[i]) == 0)
				posE = i;
		}
		count = 1 + (((posS - posE) > 0) ? (posS - posE) : (posE - posS)) ;
	}
	//起点终点在分别在两条线路上
	else {
		//起点在线路A上,终点在线路B上
		if (start[0] == 'A') {
			for (i = 0; i < 20; i++) 
				if (strcmp(start, aa[i]) == 0) {
					posS = i;
					break;
				}
			for (i = 0; i < 17; i++) 
				if (strcmp(end, bb[i]) == 0){
					posE = i;
					break;
				}
		}
		//起点在线路B上,终点在线路A上
		else {
			for (i = 0; i < 20; i++) 
				if (strcmp(end, aa[i]) == 0) {
					posE = i;
					break;
				}
				for (i = 0; i < 17; i++) 
					if (strcmp(start, bb[i]) == 0){
						posS = i;
						break;
				}
		}
		tmp1 = a1[posS] + b1[posE];
		tmp2 = a2[posS] + b2[posE];
		count = ((tmp1 > tmp2)? tmp2 : tmp1) - 1;

	}
	return count;
}

int main(void)
{
	char start[10], end[10];
	int count;
	scanf("%s", start);
	scanf("%s", end);
	count = jishi3(start, end);
	printf("%d\n", count);
	return 0;
}


地铁换乘

标签:style   blog   io   color   ar   os   sp   for   div   

原文地址:http://www.cnblogs.com/mengfanrong/p/4094725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!