HD-ACM算法专攻系列(8)——排序
题目描述:
源码:
#include"iostream" #include"string" using namespace std; void Order(int *p, int n) { int tmp; if(n < 2)return; for(int i = 0; i < n - 1; i++) { for(int j = 0; j < n - i - 1; j++) { if(p[j] > p[j + 1]) { tmp = p[j]; p[j] = p[j + 1]; p[j + 1] = tmp; } } } } int main() { string str; int nums[501], count, start, len, num; bool ok; while(cin>>str) { count = 0; start = 0; len = str.length(); for(int i = 0; i < len; i++) { if(str[i] == '5') { start++; } else { break; } } num = 0; ok = true; for(int i = start; i < len; i++) { if(str[i] != '5') { ok = true; num = num * 10 + (str[i] - '0'); } else { if(ok) { nums[count] = num; count++; num = 0; ok = false; } } } if(str[len - 1] != '5') { nums[count] = num; count++; } Order(nums, count); for(int i = 0; i < count; i++) { if(i > 0)cout<<" "; cout<<nums[i]; } cout<<endl; } return 0; }