微软实习生第一题

来源:互联网 发布:佣金宝交易软件 编辑:程序博客网 时间:2024/05/18 02:49
/*时间限制:10000ms单点时限:1000ms内存限制:256MBFor this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’). Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment. Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).InputInput consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.OutputFor each case, print exactly one line with the reordered string based on the criteria above.样例输入aabbccdd007799aabbccddeeff113355zz1234.89898abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee样例输出abcdabcd013579abcdefz013579abcdefz<invalid input string>abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa*/



#include <stdio.h>#include<string.h>#define Length_MAX 500000char input[Length_MAX];int count[36] = {0};//清空数组int count[36]void clear_count(){int i = 0;for(i = 0; i < 36; i++){count[i] = 0;}}//判断int count[36]数组中存储的所有元素之和是否为0int isOver(){int sum = 0;int i = 0;for(i = 0; i < 36; i++){sum = sum + count[i];}return sum;}/**参数:str[]为字符串数字,length为该字符串数组的长度(包括'\0')*返回值:如果str[]为有效字符串(只包含0-9或者a-z),则返回1,无效字符串则返回0.*功能:统计str[]字符串数组中各个字符的数量,并存储到数组int count[36]中,count[0-9]分别对应字符'0'到'9'*的数量, count[10-36]分别对应字符'a'-'z'的数量*/int init_Count(char str[], int length){int i = 0;int pos = 0;for(i = 0; i < length; i++){if(str[i] >= '0' && str[i] <= '9'){pos = (int)(str[i] - '0');count[pos]++;}else if(str[i] >= 'a' && str[i] <= 'z'){pos = (int)(str[i] - 'a') + 10;count[pos]++;}else{return 0;}}return 1;}int main(){int i = 0;int j = 0;int length = 1; int valid = 0;gets(input);for(i = 0; i < length; i++){valid = init_Count(input, strlen(input));if(valid ==0){printf("<invalid input string>\n");}else{char temp = '0';while(isOver() != 0){for(j = 0; j < 36; j++){if(count[j] > 0){if(j>= 0 && j <=9){temp = (char)('0'+j);printf("%c", temp);}else{temp = (char)('a'+j-10);printf("%c", temp);}count[j]--;}}}printf("\n");}clear_count();}}


6 0
原创粉丝点击