HDU 2016 (水)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2016

题目大意:给你 n 个数,把最小的数和第一个数字互换,然后输出

解题思路:

很水,开数组,遍历并记录 min ,最后 swap 输出

代码

 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 int num;
 8 int s[105];
 9 int main()
10 {
11     int n;
12     int x;
13     int p;
14     int min = 100;
15     while(cin >> n && n)
16     {
17 //       没必要.  memset(s, 0, sizeof(int));
18         min = 100;
19         for(int i = 0; i < n; i ++)
20         {
21             cin >> s[i];
22             if(s[i] < min)
23             {
24                 min = s[i];
25                 p = i;
26             }
27         }
28         swap(s[0], s[p]);
29         for(int i = 0; i < n; i ++)
30         {
31             if(i != n - 1)
32                 cout << s[i] << " " ;
33             else
34                 cout << s[i]   ;
35         }
36         //if(n != 0)
37             cout << endl;
38     }
39 }

猜你喜欢

转载自www.cnblogs.com/mimangdewo-a1/p/9379176.html
今日推荐