http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3202
题意:拍卖东西,以第二高价的价格卖给出第一高价的人。输出最后获得东西的人的序号和要出的价钱。
思路:(最笨的方法)用结构体来排序。
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct hzx { 4 int id; 5 int a; 6 }; 7 int comp1(const void *p,const void *q) { 8 return ((struct hzx *)p)->a-((struct hzx *)q)->a; 9 } 10 int main() { 11 int t,n; 12 struct hzx aa[101]; 13 cin>>t; 14 while(t--) { 15 cin>>n; 16 for(int i=0; i<n; i++) { 17 cin>>aa[i].a; 18 aa[i].id=i; 19 } 20 qsort(aa,n,sizeof(struct hzx),comp1); 21 printf("%d %d ",aa[n-1].id+1,aa[n-2].a); 22 23 } 24 return 0; 25 }