将一组数据从大到小排列后输出,要求显示每个元素及它们在原数组中的下标(相同数据下标也从大到小)。
函数接口定义:
void sort_index(ArrayIndex *a,int n) ; //sort_index函数声明
编写接口对应的sort_index定义。
裁判测试程序样例:
#includeusing namespace std; struct ArrayIndex {int value,index; }; void sort_index(ArrayIndex *a,int n) ; //sort_index函数声明 int main() { ArrayIndex a[10]; int i; for(i=0;i<10;i++){ cin>>a[i].value; } for( i=0; i<10; i++ ) a[i].index = i; sort_index(a,10) ;// 调用sort_index函数 for(int i=0; i<10; i++ ) cout << a[i].value << ' ' << a[i].index << endl; return 0; }
输入样例:
输入一行10个数据:
1 2 3 4 5 1 2 3 4 5
输出样例:
输出10行,每行空格分隔数据和下标 :
5 9
5 4
4 8
4 3
3 7
3 2
2 6
2 1
1 5
1 0
c++解析:
#include
bool cmp(ArrayIndex a, ArrayIndex b)
{
if (a.value != b.value)return a.value > b.value;
return a.index > b.index;
}
void sort_index(ArrayIndex* a, int n) //sort_index函数声明
{
sort(a, a + n, cmp);
}



