洛谷P1125笨小猴C语言

news/2024/10/20 20:49:43/

先放题目

本题较简单,我个人觉得本题关键在于统计每个字母出现的次数。用以下的代码来处理这件事应该算是比较方便的(详情请看下面的代码及注释,我觉得应该是比较好理解的)。

for (int i=0; i<len; i++)
    {
        b=ch[i];
        cnt[b-'a']++;  //遍历字符数组,使对应的计数器加1;如:若ch[i]=='a',则cnt[0]加1,以此类推
    }

下面是AC代码:(如果有更好的方法欢迎大家提出来)

#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{int primer (int a);  //判断质数函数声明char ch[100],b;int cnt[26]= {0};  //初始化计数器,记录每个字母出现的次数int maxn=0,minn=200;scanf("%s",ch);int len=strlen(ch);for (int i=0; i<len; i++){b=ch[i];cnt[b-'a']++;  //遍历字符数组,使对应的计数器加1;如:若ch[i]=='a',则cnt[0]加1,以此类推}for (int i=0; i<26; i++){if (cnt[i]!=0){if (cnt[i]>maxn)  //找出maxn和minnmaxn=cnt[i];if (cnt[i]<minn)minn=cnt[i];}}if (primer(maxn-minn))  //调用函数判断是不是质数{printf("Lucky Word\n");printf("%d",maxn-minn);}else{printf("No Answer\n");printf("0");}
}int primer (int a)  //判断质数的函数
{int n=0,b=sqrt(a);if (a==0||a==1)return 0;else for (int i=2; i<=b; i++){if (a%i==0){return 0;break;}}return 1;
}

继续加油!!!


http://www.ppmy.cn/news/373635.html

相关文章

poj1125

思路&#xff1a;求出一行的最大值&#xff0c;同时求解出一列中的最下值&#xff0c;并记下下标&#xff0c;采用Floyd算法。 #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int maxx1005; const int inf0x3f3f3f…

信息学奥赛一本通 1125:矩阵乘法 | OpenJudge NOI 1.8 08

【题目链接】 ybt 1125&#xff1a;矩阵乘法 OpenJudge NOI 1.8 09:矩阵乘法 【题目考点】 1. 二维数组遍历 【题解代码】 解法1&#xff1a; #include<bits/stdc.h> using namespace std; #define N 105 int main() {int m, n, k, a[N][N], b[N][N], r[N][N] {};…

1125: 上三角矩阵的判断

1125: 上三角矩阵的判断 时间限制: 1 Sec 内存限制: 128 MB 提交: 373 解决: 341 [提交] [状态] [讨论版] [命题人:eilene] 题目描述 编写程序&#xff0c;输入一个正整数n&#xff08;1<n<10&#xff09;和n阶方阵a中的元素&#xff0c;如果a是上三角矩阵&#xff…

信息学奥赛一本通(c++):1125:矩阵乘法

信息学奥赛一本通&#xff08;c&#xff09;&#xff1a;1125&#xff1a;矩阵乘法 一、题目 1125&#xff1a;矩阵乘法时间限制: 1000 ms 内存限制: 65536 KB 【题目描述】 计算两个矩阵的乘法。nm阶的矩阵A乘以mk阶的矩阵B得到的矩阵C 是nk阶的&#xff0c;且C[i]…

PAT 1125

第二题很多人都说是赫夫曼树,我就排了个序就过了,之后推敲了一下这个逻辑是可行的,当然赫夫曼树也是正解,有兴趣的同学可以用赫夫曼树做一下 #include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std;int main…

P1125 [NOIP2008 提高组] 笨小猴

P1125 [NOIP2008 提高组] 笨小猴 题目描述 笨小猴的词汇量很小&#xff0c;所以每次做英语选择题的时候都很头疼。但是他找到了一种方法&#xff0c;经试验证明&#xff0c;用这种方法去选择选项的时候选对的几率非常大&#xff01; 这种方法的具体描述如下&#xff1a;假设…

PAT甲级 1125

PAT甲级 1125 题目 Chain the Ropes解析代码 题目 Chain the Ropes Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as shown by the figure. The resulti…

P1125

数组/桶的思想&#xff1a;笨小猴 要求 1.计算一个单词中每个字母出现的次数&#xff0c;并找出出现的次数与出现最小的次数 方案 1.利用桶的思想 桶算法模板 用处&#xff1a;记录数组或字符串中每个值出现的次数 场景&#xff1a;字符串&#xff0c;数组&#xff0c;字…