微软2014实习生及秋令营技术类职位在线测试-题目1 : String reorder

 我的思路是给输入的string中每个字符出现次数进行计数,然后按ASCLL码顺序,循环遍历打印出来,每次打印时,记得把字符出现的次数-1。
1
#include<stdio.h> 2 int main(void) 3 { 4 int cnt[36]; 5 char data[1024]; 6 int i=0; 7 int j=0; 8 int temp=0; 9 int f = 0,g=1; 10 int len =0; 11 while(gets(data)) 12 { 13 g=1;//错在这里,忘记把g给初始化 14 for(i=0;i<36;i++) 15 cnt[i]=0; 16 i=0; 17 //counting 18 while(data[i]!='') 19 { 20 if(data[i]>='0' && data[i]<='9') 21 temp = data[i]-'0'; 22 else if(data[i]>='a' && data[i]<='z') 23 temp = data[i]-'a'+10; 24 else 25 { 26 printf("<invalid input string> "); 27 g=0; 28 } 29 cnt[temp]++; 30 i++; 31 } 32 len =i;//the length of the string; 33 //printf("%d ",len); 34 //when input is invalid, the rest do not need to run 35 if(g == 0) 36 continue; 37 i=0,f=0; 38 while(1) 39 { 40 if(cnt[i%36]>0) 41 { 42 if(i%36>=0 && i%36<=9) 43 printf("%c",i%36+'0'); 44 else 45 printf("%c",i%36-10+'a'); 46 cnt[i%36]--; 47 f++; 48 //当打印出的元素个数与输入个数相等时,跳出打印循环 49 if(f==len) 50 { 51 printf(" "); 52 break; 53 } 54 } 55 i++; 56 } 57 58 } 59 return 0; 60 61 }
有问题可以和我联系,bettyting2010#163 dot com
原文地址:https://www.cnblogs.com/echoht/p/3661478.html