题目243-交换输出
来源:互联网 发布:什么是linux内核 编辑:程序博客网 时间:2024/06/05 16:55
交换输出
时间限制:3000 ms | 内存限制:65535 KB
难度:1
- 描述
- 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。(如果这个第一个数就是最小的数,则保持原样输出,如果最小的数有相同的按照前面的交换)
- 输入
- 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。
- 输出
- 对于每组输入数据,输出交换后的数列,每组输出占一行。
- 样例输入
4 2 1 3 45 5 4 3 2 10
- 样例输出
1 2 3 41 4 3 2 5
#include <iostream>#include <cstdio>using namespace std;int main(){int n;while(scanf("%d",&n)!=EOF){if(n==0)break;int a[100];for(int i=0;i<n;i++)scanf("%d",&a[i]);int j=0;for(int i=0;i<n;i++){if(a[i]<a[j]){j=i;}}int temp = a[0];a[0] = a[j];a[j] = temp;for(int i=0;i<n;i++){cout<<a[i];if(i!=n-1)cout<<" ";}cout<<endl;}}
最佳程序:
#include<iostream>#include<iterator>#include<algorithm>using namespace std;int data[110];int main(){int n;while(cin>>n && n){for(int i=0;i!=n;i++) cin>>data[i];iter_swap(data,min_element(data,data+n));copy(data,data+n,ostream_iterator<int>(cout," "));cout<<endl;}}
事后多学习C++的 STL算法模板,这一块不补充很麻烦
阅读全文
0 0
- 题目243 交换输出
- 题目243-交换输出
- 题目243-交换输出
- NYOJ 交换输出243
- NYOJ 243 交换输出
- NYOJ-243 交换输出
- 243 交换输出
- NYOJ 243 交换输出
- nyoj 243 交换输出
- 第十二周oj题目输出已经交换的两个数
- NYOJ 243题 交换输出
- 交换输出
- 交换输出
- 交换输出
- 交换输出
- 交换输出
- 交换输出
- 交换输出
- JavaScript的事件对象
- liunx命令
- 棋牌APP从开发到运营,要在哪些地方花钱?
- mavenSSM整合实现HelloWorld
- 搭建DHCP服务器
- 题目243-交换输出
- Scrapy模拟表单登录
- tomcat further occurrences of HTTP header parsing errors will be logged at DEBUG level.
- LDAP 安装部署(六)
- java学习五个阶段
- hdu 1402 A * B Problem Plus(FFT)
- idea sbt Spark环境搭建
- C++知识总结(10)
- c++对象的三种存在形态