C++ 排序、查找的应用
// order.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "string.h"
#include <iostream> #define length 26
using namespace std;
/*********选择排序*********/
void order(char *a,int n)
{
int i = ,j = ;
char temp;
for(i = ; i < n; i++)
for(j = i+; j < n; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
} }
}
/**********查找函数*************/
void seek(char *a,int n ,char c)
{
int i = ,flag = ;
for(i = ; i < n; i++)
{
if(a[i] == c)
{
flag = ;
}
}
if(flag == )
{
cout << "找到所要找的字母"<< endl;
}
else
cout << "没有找到所要找的字母" << endl;
} int main()
{
char a[length];
char find;
int b;
cout << "请输入"<< length <<"个字母" <<endl;
for(int i = ;i < length; i++)
{
cin >> a[i];
} for(int j = ;j < length; j++)
{
cout << a[j]<<" ";
}
cout << endl; order(a,length);
cout <<"输出有序数组:"<<endl;
for(int j = ;j < length; j++)
cout << a[j]<<" ";
cout << endl; cout << "请输入需要查找的字母" <<endl;
cin >> find;
cout << endl; seek(a,length,find);
cin >> b;
return ;
}
实验名称:排序、查找的应用
实验目的:学会如何应用排序算法和查找算法实现排序、查找。
实验要求:先从键盘上输入26个字母生成无序数组,对数组进行排序,再从键盘输入一个字符进行查找。
实验步骤及内容:
1、从键盘输入26个字母并输出。
cout << "请输入"<< length <<"个字母" <<endl;
for(int i = 0;i < length; i++)
{
cin >> a[i];
}
for(int j = 0;j < length; j++)
{
cout << a[j]<<" ";
}
cout << endl;
2、 对字符数组进行排序。这里用了选择排序来对数组进行排序。
void order(char *a,int n)
{
int i = 0,j = 0;
char temp;
for(i = 0; i < n; i++)
for(j = i+1; j < n; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
3、 查找函数,若找到则提示已经找到,否则提示没有找到。
void seek(char *a,int n ,char c)
{
int i = 0,flag = 0;
for(i = 0; i < n; i++)
{
if(a[i] == c)
{
flag = 1;
}
}
if(flag == 1)
{
cout << "找到所要找的字母"<< endl;
}
else
cout << "没有找到所要找的字母" << endl;
}
实验总结:
在弄选择排序的时候,把j = i + 1,写成了j = i,然后老是错,最后终于找到错误的根源。
C++ 排序、查找的应用的更多相关文章
- Sublime文本排序&查找重复行&删除重复行
排序 按F9或者选择菜单:Edit > Sort Lines,对每行文本进行排序 查找重复行 排序好后,按Ctrl+F,调出查找面板 查找字符串: ^(.+)$[\r\n](^\1$[\r\n] ...
- Java进阶(三十九)Java集合类的排序,查找,替换操作
Java进阶(三十九)Java集合类的排序,查找,替换操作 前言 在Java方向校招过程中,经常会遇到将输入转换为数组的情况,而我们通常使用ArrayList来表示动态数组.获取到ArrayList对 ...
- 猪八戒吃西瓜(wmelon)-排序-查找
问题 A: 猪八戒吃西瓜(wmelon) 时间限制: 1 Sec 内存限制: 64 MB提交: 30 解决: 14[提交][状态][讨论版] 题目描述 有一天,贪吃的猪八戒来到了一个大果园,果园里 ...
- C/C++ 排序&&查找算法(面试)
一.排序 1.冒泡排序 void BubbleSort(int array[],int n) { ; ; ; ; ;i<n - ;i++) /*外循环控制排序的总趟数*/ { flag = ; ...
- javascript排序 查找算法大全
在pptv的实习结束了, 忙着找工作的事,顺便把数据结构的那本书重新复习了一遍.为了加深印象,特意把里面的常用的排序.查找算法用js写了一遍 具体的实例在我的github上,大家可以访问的: http ...
- 深入JDK源码之Arrays类中的排序查找算法(转)
原文出处: 陶邦仁 binarySearch()方法 二分法查找算法,算法思想:当数据量很大适宜采用该方法.采用二分法查找时,数据需是排好序的. 基本思想:假设数据是按升序排序的,对于给定值x,从序列 ...
- [Day7]循环、数组方法、排序查找
1. ASCII(American Standard Code for Information Interchange) (1)数字0-9对应ASCII编码十进制为48-57, 字母a-z对应ASCI ...
- Java常用的排序查找算法
public static void main(String[] args) { // bubbleSort(); // int[] a = {20,2,10,8,12,17,4,25,11 ...
- python排序查找
无序表查找 def seq_search(lst, key): found = False pos = 0 while pos < len(lst) and not found: if lst[ ...
- mysql 实现经纬度排序查找功能
需求如下: 商品有多个门店,用户使用App时需要查找附近门店的商品,商品要进行去重分页. 思路: 1.确认mysql自带经纬度查询函数可以使用. 2.该需求需要利用分组排序,取每个商品最近门店的商品i ...
随机推荐
- iOS 杂笔-20(UIView和CALayer的区别与联系)
iOS 杂笔-20(UIView和CALayer的区别与联系) 每个 UIView 内部都有一个 CALayer 在背后提供内容的绘制和显示,并且 UIView 的尺寸样式都由内部的 Layer 所提 ...
- C语言中的字符和字符串
C语言在中常常出现字符和字符串,而一串字符或者字符串其实就是数组 字符数组的定义 char arr[]={'h','e','l','l','o','\0'}; 而定义字符串: char arr1[]= ...
- 真机调试出现Could not find Developer Disk Image问题解决办法
1.升级Xcode 2. 在使用Xcode进行真机调试的时候,有时根据真机的系统不同,会出现could not find developer disk image 错误,这是由于真机系统过高或者过低, ...
- 网络开始---多线程---NSThread-02-线程状态(了解)(三)
#import "HMViewController.h" @interface HMViewController () @property (nonatomic, strong) ...
- WPF 自定义控件,在ViewModel里面获取自定义控件的值
上图: 用户自定义CS里面代码如下: 自定义控件XAML里面的代码如下: 调用用户自定义控件的页面代码如下: CItySelected的属性值就是我们点击确定按钮以后得到的值,通过双向绑定在VIewM ...
- Mac显示隐藏文件的终端命令
显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults write com.apple.finder Ap ...
- cocos2d-x之xml文件读取初试
auto doc=new tinyxml2::XMLDocument(); doc->Parse(FileUtils::getInstance()->getStringFromFile(& ...
- hbase常用命令总结
创建表:表名:csliyb:testuser列族:name 例子:create 'csliyb:testuser','name','age' 添加记录: put 'csliyb:testuser',' ...
- C语言关键字详解
相对于其他语言来说,C语言的关键字算是少的了.在C98中关键子总共只有32个,我们来分析一下每个关键字在C语言中它独特的作用. 1.关于数据类型的关键字 (1) char :声明字符型变量或函数 ( ...
- Hadoop step by step _ install and configuration environment
1.安装centos linux系统. 2.配置静态IP 3.配置防火墙 4.添加hadoop用户 5.检查并安装jdk 配置环境变量 6.配置sshd服务 7.配置ssh免密码登录 8.格式化nam ...