1.这道题目其实蛮简单的,只是在排序的时候卡了一下,本来打算找一下能不能用C++自带的快速排序解决的(qsort)但是qsort定义的格式和我传入的格式不符合,实在受不了,就自己直接用快速排序实现了一下
2.还是自己太弱了,网上一搜发现有很多用sort,qsort做的,果然自己还是太弱小了
http://blog.csdn.net/lyy289065406/article/details/6647305
http://blog.csdn.net/woshixingaaa/article/details/5589070
Source Code
Problem: 1007 | User: zhyh2010 | |
Memory: 220K | Time: 16MS | |
Language: C++ | Result: Accepted |
- Source Code
#include <iostream> #include <map> using namespace std; typedef struct myMap { int first; int second; }mYMap,*pmYMap; int partition(pmYMap pA, int p, int r) { int data = pA[r].second; int i = p - 1; for (int j = p; j != r; ++j) { if (pA[j].second < data) { i++; // exchange mYMap temp; temp.first = pA[i].first; temp.second = pA[i].second; pA[i].first = pA[j].first; pA[i].second = pA[j].second; pA[j].first = temp.first; pA[j].second = temp.second; } } mYMap temp; temp.first = pA[i+1].first; temp.second = pA[i+1].second; pA[i+1].first = pA[r].first; pA[i+1].second = pA[r].second; pA[r].first = temp.first; pA[r].second = temp.second; return i + 1; } void quicksort(pmYMap pA, int p, int r) { if (p < r) { int q = partition(pA, p, r); quicksort(pA, p, q - 1); quicksort(pA, q + 1, r); } } int main(int argc, char * argv[]) { int length; int num; cin >> length >> num; //int* m_pindex = new int[num]; pmYMap m_pindex = new mYMap[num]; char ** m_pstr = new char*[num]; for (int i = 0; i != num; ++i) { m_pstr[i] = new char[length + 1]; } for (int i = 0; i != num; ++i) { for (int j = 0; j != length; ++j) { cin >> m_pstr[i][j]; } m_pstr[i][length] = '\0'; // handle int count = 0; for (int ia = 0; ia != length; ++ia) { for (int ib = ia + 1; ib != length; ++ib) { if (m_pstr[i][ia] > m_pstr[i][ib]) { count++; } } } //m_pindex[i] = count; m_pindex[i].first = i; m_pindex[i].second = count; } // quick sort quicksort(m_pindex, 0, num - 1); // output for (int i = 0; i != num; ++i) { cout << m_pstr[m_pindex[i].first] << endl; //cout << m_pindex[i].first <<m_pindex[i].second<< endl; } return 0; }