1st day
来源:互联网 发布:迪拜舞蹈大厦的数据 编辑:程序博客网 时间:2024/04/28 22:27
找出最大和第二大的数。
思路1:用冒泡排序将数按从大到小的顺序排列,数组第一个数即为最大,第二个即为第二大。
#include <stdio.h>int main(){int i;int j;int temp;int a[5] = {5, 7, 8 ,9 ,1};for(i = 0; i < 5; i++){for(j = i; j < 5; j++){if(a[i] < a[j]){temp = a[i];a[i] = a[j];a[j] = temp;}}}for(i = 0; i < 5; i++){printf("%d ", a[i]);}printf("\n");
printf("the biggest num is %d\n", a[0]);printf("the second big num is %d\n", a[1]);}冒泡排序优化后:
#include <stdio.h>int main(){int i;int j;int temp;int cmpCount = 0; int a[5] = {7, 8, 9, 1, 5};for(i = 0; i < 5; i++){int isExchange = 0;for(j = i+1; j < 5; j++){if(a[i] < a[j]){temp = a[i];a[i] = a[j];a[j] = temp;isExchange = 1;}cmpCount++;if(!isExchange){break;}}}for(i = 0; i < 5; i++){printf("%d ", a[i]);}printf("\n");printf("the biggest num is %d\n", a[0]);printf("the second big num is %d\n", a[1]);printf("the compare Count is %d\n", cmpCount);return 0;}
0 0
- 1st day:基本概念
- 1st day here
- 1st day
- [RHCE training] 1st day
- Java SE 1st day
- 1st day of MySql
- 黑马程序员--面向对象--1st day
- THE 1st day at CSDN
- javascript学习,1st day after Z-day
- St. Valentine's Day
- St.Valentine’s day
- 『算法学习笔记』1st day. 顺序结构程序设计
- Begin OpenGL with Python-1st Day,draw a simple sample.
- 1st
- 1st
- 1st
- 1st
- 1st POST
- lintcode 452 删除链表中的元素
- LeetCode 47. Permutation ii java solution
- 快速排序
- C++获取输入法名字,调整输入法候选框位置
- okhttp 同步请求和异步请求
- 1st day
- 获得物品的预期数
- 编写高质量代码—Web前端开发啊修炼之道(二)
- Ubuntu 16.04安装配置ftp服务器
- 2017.3.31notes
- hibernate
- Java多线程学习1,使用线程的三种方式
- 链表求和
- RxJava的初步使用(一)