題目描述
給定n個字符串,請對n個字符串按照字典序排列。
輸入描述:
輸入第一行為一個正整數n(1≤n≤1000),下面n行為n個字符串(字符串長度≤100),字符串中只含有大小寫字母。
輸出描述:
數據輸出n行,輸出結果為按照字典序排列的字符串。
輸入
#輸入 9 cap to cat card two too up boat boot
輸出
#輸出 boat boot cap card cat to too two up
#include <iostream>
#include <string>
#include <vector>
#include <string.h>using namespace std;
void mySort(vector <string> srcVector, int count);
int myCmp(string src1, string src2);
void mySwap(string *src1, string *src2);int main()
{int count = 0;vector <string> srcVector(100);cin >> count;for(int i = 0; i < count; i++)cin >> srcVector[i];mySort(srcVector, count);return 0;
}void mySort(vector <string> srcVector, int count)
{for(int i = 0; i < count; i++){for(int j = 0; j < count; j++){if(myCmp(srcVector[j], srcVector[j+1]) > 0){mySwap(&srcVector[j], &srcVector[j + 1]);}}}for(int i = 0; i < count; i++)cout << srcVector[i] <<endl;
}int myCmp(string src1, string src2)
{int len = 0;int ret = 0;len = (src1.size() > src2.size())?src2.size():src1.size();for (int i = 0; i < len; i++){if(strcmp(src1.c_str(), src2.c_str()) > 0){ret = 1;break;}else{ret = 0;break;}}return ret;
}void mySwap(string *src1, string *src2)
{string myString;myString = *src1;*src1 = *src2;*src2 = myString;
}
?
?