微软2014年在线笔试题前两题

来源:互联网 发布:青海干部网络教育平台 编辑:程序博客网 时间:2024/06/05 15:16

Description
  For 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 “” when the input contains any invalid characters (i.e., outside the ’0′-’9′ and ‘a’-’z’ range).
  Input
  Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.
  Output
  For each case, print exactly one line with the reordered string based on the criteria above.
  样例输入
  aabbccdd
  007799aabbccddeeff113355zz
  1234.89898
  abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
  样例输出
  abcdabcd
  013579abcdefz013579abcdefz
  
  abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
我只实现了要求一,后面的还要再想想

#include <iostream>#include <string>using namespace std;extern int max1=0;void f(string s,int hash[]){    int i,t;    //将输入的字符存到hash数组其ASCII码对应的下标下,同时标记重复字符的最大值    for(i= 0;i<s.size();i++)    {        t=int(s[i]);        hash[t]++;        if(hash[t]>max1)            max1=hash[t];    }}int _tmain(int argc, _TCHAR* argv[]){    string s;    cout<<"Input:"<<endl;    char c;    getline(cin,s);    int hash[200]={};    f(s,hash);    //遍历hash数组    while(max1)    {        int i=0;        for(i;i<200;i++)        {            if(hash[i]!=0)            {                if(hash[i]==max1)                {                    max1--;                }            c=(char)i;            cout<<c;            hash[i]--;            }      }    }    getchar();    getchar();    return 0;}
0 0
原创粉丝点击