1º 查找里程
给你这样一张里程表,如何写一个程序,输入两地的地名,能输出期间的里程?
#include <stdio.h> #include <string.h> #define C_LEN 30typedef struct city {char name1[C_LEN];char name2[C_LEN];int distance; } City; int main(void) {City cities[] = { // 数据如何表示,感觉这样的方法太繁杂,有没有更好的表示方法???{"Atlanta", "Boston", 1108},{"Atlanta", "Chicago", 708},{"Boston", "Chicago", 994},{"Atlanta", "Denver", 1430},{"Boston", "Denver", 1998},{"Chicago", "Denver", 1021},{"Atlanta", "Detroit", 732},{"Boston", "Detroit", 799},{"Chicago", "Detroit", 279},{"Denver", "Detroit", 1283},{"Atlanta", "Houston", 791},{"Boston", "Houston", 1830},{"Chicago", "Houston", 1091},{"Denver", "Houston", 1034},{"Detroit", "Houston", 1276},{"Atlanta", "Los Angeles", 2191},{"Boston", "Los Angeles", 3017},{"Chicago", "Los Angeles", 2048},{"Denver", "Los Angeles", 1031},{"Detroit", "Los Angeles", 2288},{"Houston", "Los Angeles", 1541},{"Atlanta", "Miami", 663},{"Boston", "Miami", 1520},{"Chicago", "Miami", 1397},{"Denver", "Miami", 2107},{"Detroit", "Miami", 1385},{"Houston", "Miami", 1190},{"Los Angeles", "Miami", 2716},{"Atlanta", "New York", 854},{"Boston", "New York", 222},{"Chicago", "New York", 809},{"Denver", "New York", 1794},{"Detroit", "New York", 649},{"Houston", "New York", 1610},{"Los Angeles", "New York", 2794},{"Miami", "New York", 1334},{"Atlanta", "Philadelphia", 748},{"Boston", "Philadelphia", 315},{"Chicago", "Philadelphia", 785},{"Denver", "Philadelphia", 1739},{"Detroit", "Philadelphia", 609},{"Houston", "Philadelphia", 1511},{"Los Angeles", "Philadelphia", 2703},{"Miami", "Philadelphia", 1230},{"New York", "Philadelphia", 101},{"Atlanta", "San Francisco", 2483},{"Boston", "San Francisco", 3128},{"Chicago", "San Francisco", 2173},{"Denver", "San Francisco", 1255},{"Detroit", "San Francisco", 2399},{"Houston", "San Francisco", 1911},{"Los Angeles", "San Francisco", 387},{"Miami", "San Francisco", 3093},{"New York", "San Francisco", 2930},{"Philadelphia", "San Francisco", 2902},{"Atlanta", "Seattle", 2625},{"Boston", "Seattle", 3016},{"Chicago", "Seattle", 2052},{"Denver", "Seattle", 1341},{"Detroit", "Seattle", 2327},{"Houston", "Seattle", 2369},{"Los Angeles", "Seattle", 1134},{"Miami", "Seattle", 3303},{"New York", "Seattle", 2841},{"Philadelphia", "Seattle", 2816},{"San Francisco", "Seattle", 810}};char city1[C_LEN];char city2[C_LEN];printf("请输入第一个城市名: ");gets(city1);printf("请输入第二个城市名: ");gets(city2);for(int i = 0; i < sizeof(cities) / sizeof(cities[0]); i++){if(strcmp(city1, city2) == 0){printf("两个城市之间的距离: %d\n", 0);break;}else if((strcmp(city1, cities[i].name1) == 0 && strcmp(city2, cities[i].name2) == 0) ||(strcmp(city1, cities[i].name2) == 0 && strcmp(city2, cities[i].name1) == 0)){printf("两个城市之间的距离: %d\n", cities[i].distance);break;}}return 0; }
2º 排序
题目内容:
程序读入一个正整数n(0<n<=100000),然后读入n个整数,均为32位下的整数。输出对这个整数排序后的结果,每个整数后面有一个空格。
输入格式:
一个表示个数的正整数n,和n个整数,以空格间隔。
输出格式:
排序后的n的整数,每个整数后面有一个空格。
输入样例:
6 2 23 54 12 6 8
输出样例:
2 6 8 12 23 54
解答:(So easy)
#include <stdio.h> #include <stdlib.h> void sort(int a[], int len);int main(void) {int count, i;scanf("%d", &count);int *number_array = (int *)malloc(count * sizeof(int));if(number_array != NULL){for(i = 0; i < count; i++)scanf("%d", &number_array[i]);}sort(number_array, count);for(i = 0; i < count; i++)printf("%d ", number_array[i]);printf("\n");free(number_array);return 0; }void sort(int a[], int len) {int temp;for(int i = 0; i < len - 1; i++){for(int j = i; j < len; j++)if(a[i] > a[j]){temp = a[i];a[i] = a[j];a[j] = temp;}} }