#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ char testString[20] = {0}; strcpy(testString,"glbRa dadAdadAglbR"); int i = 0; int max = 0; int a[26] = {0}; for(i=0;i<20;i++) { if(isalpha(testString[i])) { if(islower(testString[i])) { a[testString[i]-'a']++; } else { a[testString[i]-'A']++; } } } for(i=0;i<26;i++) { if(a[i] > max) max = a[i]; printf("a[%d] = %d\n",i,a[i]); } printf("max = %d\n",max); return 0;} [解决办法]
Python code
str = "abcnashcduiyqwcceqoizpzczmclaqporwirkxnvxvcmlzkpqe"ret = "abcdefghijklmnopqrstuvwxyz"str = str.lower()a = [0 for x in range(26)]for i in range(len(str)): a[ord(str[i]) - ord('a')] = a[ord(str[i]) - ord('a')] + 1max = reduce(lambda x, y: (x > y and [x, y] or [y, x])[0], a)print ret[a.index(max)], max#print reduce(lambda x, y: x + y, a) [解决办法]
if(pSrc != NULL) { char *pMem = new char[strlen(pSrc)];//用于存放已经比较过的字符
int iIndex = 0; char *p = (char*)pSrc; while(*p!='\0') { if(!bIsExist(*p, pMem)) { max=max>getcharcount(*p, pSrc) ? max : ch=*p, getcharcount(*p, pSrc); pMem[iIndex++]=*p; } p++; } delete []pMem; } return max; } int main() { char *pTemp="The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, applications should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page, unless legacy standards or data formats prevent the use of Unicode. If use of Unicode is not possible, applications should tag the data stream with the appropriate encoding name when protocols allow it. HTML, XML, and HTTP files allow tagging, but text files do not."; char pl; int max = getmaxcount(pl, pTemp);//第一个出现次数做多的字符和其出现的次数