@jimbo
2016-09-19T19:43:03.000000Z
字数 1545
阅读 752
给出一个字符串,输出出现次数最多的三个字母,次数相同按照ASCII码由小到大排列,不缺分大小写,输出为小写字母。
#include<stdio.h>
int main(){
char string[1005] = {0};
int counts[26] = {0};
scanf("%s", string);
for (int i = 0; string[i] != '\0'; i++) {
if ('a' <= string[i] <= 'z')
{
counts[string[i] - 'a']++;
}
else if ('A' <= string[i] <= 'Z')
{
counts[string[i] - 'A']++;
}
}
int maxCount = -1;
int maxNumber = -1;
for (int i = 0; i < 26; i++)
{
if (maxCount < counts[i])
{
maxCount = counts[i];
maxNumber = i;
}
}
printf("%c\n", maxNumber + 'a');
return 0;
}
CCF中出现的类似题目:
#include<stdio.h>
int main(){
int counts[1001] = {0};
int numberOfDate = 0;
int oneDate = 0;
scanf("%d", &numberOfDate);
for (int i = 0; i < numberOfDate; i++) {
scanf("%d", &oneDate);
counts[oneDate]++;
}
int max = -1;
int theMostNumber = -1;
for (int i = 0; i < 1001; i++) {
if (counts[i] > max) {
max = counts[i];
theMostNumber = i;
}
}
printf("%d\n", theMostNumber);
return 0;
}
求1-n之间的质数
#include<stdio.h>
int main(){
int n = 0;
scanf("%d", &n);
for (int i = 2; i < n; i++) {
bool isZhiShu = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isZhiShu = false;
break;
}
}
if (isZhiShu)
{
printf("%d ", i);
}
}
printf("\n");
return 0;
}
求a,b两个数的最小公约数 最大公倍数
#include<stdio.h>
// 辗转相除法
int gcd(int a, int b) {
if (!b) {
return a;
} else {
return gcd(b, a%b);
}
}
int main(){
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
// 最大公约数
int maxCommon = -1;
if (a > b) {
maxCommon = gcd(a, b);
printf("%d\n", maxCommon);
} else {
maxCommon = gcd(b, a);
printf("%d\n", maxCommon);
}
// 最小公倍数
printf("%d\n", a * b / maxCommon);
return 0;
}
水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)
求100 - 999的水仙花数
#include<stdio.h>
int nnn(int n) {
return n * n * n;
}
int main(){
for (int i = 100; i <= 999; i++) {
int n3 = i % 10;
int n2 = i /10 % 10;
int n1 = i / 100;
int n = nnn(n1) + nnn(n2) + nnn(n3);
if (n == i)
{
printf("%d ", n);
}
}
printf("\n");
return 0;
}