第6周 搜索与排序

   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;
}
View Code

   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;}}
}
View Code

 

转载于:https://www.cnblogs.com/yerenyuan/p/5161414.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543697.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

(转) Twisted :第十九部分 改变之前的想法

2019独角兽企业重金招聘Python工程师标准>>> 简介 Twisted是一个正在进展的项目,它的开发者会定期添加新的特性并且扩展旧的特性. 随着Twisted 10.1.0发布,开发者向 Deferred 类添加了一个新的特性—— cancellation ——这正是我们今天要研究的. 异步编程将请求和响…

stl list 删除元素_删除所有出现的元素,并从列表中删除一些特定的元素。 C ++ STL...

stl list 删除元素list.remove()和list.remove_if()函数 (list.remove() and list.remove_if() functions) remove() function is used to remove all occurrences of a given element from the list and function remove_if() is used to remove set of some specific element…

Mac 获取 Brew

2019独角兽企业重金招聘Python工程师标准>>> 终端输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 转载于:https://my.oschina.net/fdstudio/blog/610680

express 项目生成器_用于项目的Express模板生成器(2)| 应用程序结构研究

express 项目生成器Hello! In express template generator for your projects (1), we looked at express generator and how we can start an express application with stressing to build a brand new structure of all required files. 你好&#xff01; 在针对您的项目的E…

简单的block

int multi 7; int (^myBlock)(int) ^(int num){ return num * multi; }; int result myBlock(5); NSLog("结果是&#xff1a;%d",result);//输出结果是&#xff1a; 结果是&#xff1a;35 void (^printBlock)(NSS…

c# 浮点数十六进制字符串_从C#中包含十六进制值的字符串数组中打印整数值...

c# 浮点数十六进制字符串将十六进制字符串数组转换为整数 (Converting array of hexadecimal strings to integers) Let suppose you have some of the strings (i.e. array of strings) containing hexadecimal values like "AA", "ABCD", "ff21&quo…

Linux 服务器中文乱码编码解决

Linux环境的ECS中&#xff0c;若出现如下中文显示为乱码的情况。 一般原因如下: 1. 未安装中文语言包 2. 未设置正确的默认语言 3. SSH 终端未正确配置 本文以Centos 6.5为例&#xff0c;演示如何解决中文乱码问题。 1. 使用 locale -a |grep zh_CN查看系统是否已经安装…

Python | 如何强制除法运算为浮点数? 除数一直舍入为0?

Until the python version 2, the division of two integers was always being rounded down to 0. 在python版本2之前&#xff0c; 两个整数的除法总是四舍五入为0 。 Consider the below example, being executed in python version 2.7, 考虑下面的示例&#xff0c;该示例在…

Python程序输入一个字符串并查找总数的大写和小写字母

Given a string str1 and we have to count the total numbers of uppercase and lowercase letters. 给定字符串str1 &#xff0c;我们必须计算大写和小写字母的总数。 Example: 例&#xff1a; Input: "Hello World!"Output:Uppercase letters: 2Lowercase lette…

Android(Xamarin)之旅(三)

原文:Android&#xff08;Xamarin&#xff09;之旅&#xff08;三&#xff09;前面两篇说到了Xamarin的安装和一些简单的控件&#xff0c;今天来说说一些对话框和提示信息&#xff0c;以及简单的布局元素。 一、对话框和提示信息 一、对话框 我们首先从简单的对话框开始。 1、普…

java中为按钮添加图片_我们可以在Java接口中为成员定义私有和受保护的修饰符吗?...

java中为按钮添加图片No, it is not possible to define private and protected modifiers for the members in interfaces in Java. 不可以&#xff0c;无法为Java接口中的成员定义私有修饰符和受保护的修饰符。 As we know that, the members defined in interfaces are imp…

android Monkey 测试技巧

MonkeyTest 测试流程1、常用的命令参数说明&#xff1a;-sseed值&#xff0c;设置这个参数的主要作用是程序员可以重复执行这个命令&#xff0c;seed值相同则monkey测试序列也大致一样。-p 指定要测试的包&#xff0c;参数跟的是apk的package id--pct-touch 调整触摸…

十六进制数制到二进制,八进制和十进制数制的转换

Prerequisite: Number systems 先决条件&#xff1a; 数字系统 1)将十六进制数制转换为二进制数制 (1) Conversion of Hexadecimal Number System to Binary Number System) To convert hexadecimal numbers into binary numbers, we can use the relationship between hexade…

ldo regula_使用C中的Regula Falsi方法找到复多项式方程的根

ldo regulaRegula Falsi方法 (Regula Falsi method) About the method: 关于方法&#xff1a; We often hear many children and even many adults complaining about the difficulty level that they face while solving complex polynomial equations. It is also difficult…

解决一次由于SSL证书到期导致的网站不能访问的问题(Nginx,php,Apache)

1. 现象放假期间收到zabbix报警&#xff0c;提示主站访问不了&#xff0c;报502。2.排查思路及过程因为是过年休息&#xff0c;放假前又没有更新&#xff0c;基本可以排除是更新和配置导致的问题。ssh连上服务器发现服务器连接和资源都没问题。这是一套lnamp架构的网站&#xf…

python字典按键值排序_在Python中按键或值按升序和降序对字典排序

python字典按键值排序Problem Statement: Write a Python program to sort (ascending and descending) a dictionary by key or value. 问题陈述&#xff1a;编写一个Python程序&#xff0c;以按键或值对字典进行排序(升序和降序)。 Example: 例&#xff1a; Input: diction…

Try Redis : Redis 入门教程

开篇 Redis 是一种以键值对&#xff08;key-value&#xff09;存储数据的NoSQL数据库。 键值对存储数据的本质是以某个键存储某个值。之后你可以用这个键把存储的值取出来。可以用SET命令以键‘servername’存储值‘fido’&#xff1a; SET servername fido这样&#xff0c;数据…

在C ++中使用getter和setter函数创建具有X和Y轴的类Point

We have two declare a point class with two Axis X and Y and we have to create/design its getter and setter functions. 我们有两个声明带有两个Axis X和Y的点类&#xff0c;并且我们必须创建/设计其getter和setter函数。 As we know that a class has some data member…

go newscanner判断文件读取结束_Go单元测试-testing

在开发程序中&#xff0c;很重要一点就是测试&#xff0c;测试可以保证代码的质量&#xff0c;保证每个函数可以正常运行。但是如何保证写出来的程序是否正确。单元测试一般是用来测试我们的代码逻辑有没有问题&#xff0c;有没有按照我们期望的运行&#xff0c;以保证代码质量…

_.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素

_.uniqRuby Array.compact和Array.uniq方法 (Ruby Array.compact and Array.uniq Methods) In the last article, we have gone through two different methods of deleting elements from the Array. We have seen their implementation with the help of their syntaxes and …