hdoj 1106 排序 【水題】,hdoj1106
排序Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35499 Accepted Submission(s): 10042
Problem Description輸入一行數字,如果我們把這行數字中的‘5’都看成空格,那麼就得到一行用空格分割的若干非負整數(可能有些整數以‘0’開頭,這些頭部的‘0’應該被忽略掉,除非這個整數就是由若干個‘0’組成的,這時這個整數就是0)。
你的任務是:對這些分割得到的整數,依從小到大的順序排序輸出。
Input輸入包含多組測試案例,每組輸入資料只有一行數字(數字之間沒有空格),這行數位長度不大於1000。
輸入資料保證:分割得到的非負整數不會大於100000000;輸入資料不可能全由‘5’組成。
Output對於每個測試案例,輸出分割得到的整數排序的結果,相鄰的兩個整數之間用一個空格分開,每組輸出佔一行。
Sample Input
0051231232050775
Sample Output
0 77 12312320
睡前一水
代碼:
#include <stdio.h>#include <string.h>#include <algorithm>#define M 1005using std::sort;int s[M];char str[M];int main(){while(gets(str)){int len, tot = 0, i;len = strlen(str);i = 0;//while(str[i] == '0') i++;while(str[i] == '5') i++;int temp = 0;while(i < len){if(str[i] == '5'){i++;while(i<len&&str[i] == '5') i++;s[tot++] = temp; temp = 0;}else{temp = temp*10+str[i]-'0'; i++;}}if(str[len-1] != '5') s[tot++] = temp;sort(s, s+tot);printf("%d", s[0]);for(i = 1; i < tot; i ++) printf(" %d", s[i]);printf("\n");}return 0;}