鱼C论坛

 找回密码
 立即注册
查看: 3860|回复: 23

[已解决]统计个数怎么写这程序

[复制链接]
发表于 2022-3-31 21:21:08 | 显示全部楼层 |阅读模式
5鱼币
题目描述
小名的好朋友A送给了小B一大包水果,里面有苹果(apple)、桃子(peach)、香蕉(banana),各种水果放在了一个列表里,请你计算一下苹果的个数。
输入格式
输入一行,包含若干个字符串,字符串之间用空格隔开,字符串只可能是apple、peach、banana,至多有 100100 个字符串。
输出格式
输出一个数字,表示苹果的个数。
输入样例 复制
apple peach peach banana banana banana
输出样例 复制
1


这个程序怎么写有大佬指点一下吗?
最佳答案
2022-3-31 21:21:09
本帖最后由 傻眼貓咪 于 2022-4-1 10:52 编辑

  1. #include <iostream>
  2. #include <map>

  3. int main(){
  4.         std::map<std::string, size_t> fruits = {{"apple", 0}, {"peach", 0}, {"banana", 0}};
  5.         std::string fruit;
  6.         while(std::cin >> fruit) fruits[fruit]++;
  7.         for(auto const& [key, value] : fruits){
  8.                 std::cout
  9.                         << key
  10.                         << ": "
  11.                         << value
  12.                         << std::endl;
  13.         }
  14.         return 0;
  15. }
复制代码
  1. apple: 1
  2. banana: 3
  3. peach: 2
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-31 21:21:09 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
本帖最后由 傻眼貓咪 于 2022-4-1 10:52 编辑

  1. #include <iostream>
  2. #include <map>

  3. int main(){
  4.         std::map<std::string, size_t> fruits = {{"apple", 0}, {"peach", 0}, {"banana", 0}};
  5.         std::string fruit;
  6.         while(std::cin >> fruit) fruits[fruit]++;
  7.         for(auto const& [key, value] : fruits){
  8.                 std::cout
  9.                         << key
  10.                         << ": "
  11.                         << value
  12.                         << std::endl;
  13.         }
  14.         return 0;
  15. }
复制代码
  1. apple: 1
  2. banana: 3
  3. peach: 2
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
人造人 + 5 + 5 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-31 21:34:15 | 显示全部楼层
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void) {
  5.     char buff[7];
  6.     size_t count = 0;
  7.     while(scanf("%s", buff) == 1) {
  8.         if(!strcmp(buff, "apple")) ++count;
  9.     }
  10.     printf("%lu\n", count);
  11.     return 0;
  12. }
  13. $ gcc-debug -o main main.c
  14. $ ./main
  15. apple peach peach banana banana banana
  16. 1
  17. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-1 08:16:43 | 显示全部楼层

大佬这个是C++的吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-1 08:36:11 | 显示全部楼层

#include <iostream>
#include <string.h>
#include <string>
#include <cstring>
using namespace std;
int main()
{
        string a[6];
        int conp=0;
        cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5];
        for(int i=0;i<6;i++)
        {
                if(a=="apple") conp++;
        }
        cout<<conp;
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-1 09:36:59 | 显示全部楼层
asd74827 发表于 2022-4-1 08:16
大佬这个是C++的吗

这代码哪里像C++了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-1 10:12:21 | 显示全部楼层
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main()
{
    char a[100100],b[7]="apple",c[7];
    int i,j,count=0;
    for (i = -1; a[i++]!='\n'; )
        scanf("%c", &a[i]);
    a[i] = '\0';
    for (i = 0,j=0; a[i]; i++)
    {
        if (a[i] != 32)
            c[j++] = a[i];
        else
        {
            c[j] = '\0';
            if (strcmp(b, c) == 0)
                count++;
            j = 0; c[j] = { 0 };
        }
    }
    printf("%d", count);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-1 10:31:56 From FishC Mobile | 显示全部楼层
  1. #include <iostream>

  2. int main(){
  3.         std::string fruit;
  4.         int apple = 0;
  5.         while(std::cin >> fruit){
  6.                 if(fruit == "apple"){
  7.                         apple++;
  8.                 }
  9.         }
  10.         std::cout << apple << std::endl;
  11.         return 0;
  12. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-1 17:20:31 | 显示全部楼层
按照题目要求,这道题并没那么简单。需要用到动态分配内存。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define BUF 1024

  5. /*
  6.         从字符串str中获取以空格分割的第1个单词,然后修改指针指向下一个单词,直到'\0'为止,最后置*str==NULL。
  7.         参数:
  8.                 str 以'\0'结束的字符串。
  9.         返回值:
  10.                 成功 返回单词所在的内存指针。
  11.                 失败 返回NULL。
  12. */
  13. char *getWord(char **str)
  14. {
  15.         char *buf=NULL, c;
  16.         int i, len;

  17.         if(*str==NULL)
  18.         {
  19.                 return NULL;
  20.         }

  21.         // 判断字符串长度为0返回
  22.         len = strlen(*str);
  23.         if(len==0)
  24.     {
  25.         return NULL;
  26.     }

  27.     // 删除字符串前面的所有空格
  28.         for(i=0; i<=len; i++)
  29.         {
  30.                 if((*str)[i]!=' ')
  31.                 {
  32.                     break;
  33.                 }               
  34.         }
  35.         *str += i;

  36.         // 再次判断字符串长度为0返回(到了尾部的空格处理)
  37.         len = strlen(*str);
  38.         if(len==0)
  39.     {
  40.         return NULL;
  41.     }

  42.         // 为单词分配内存
  43.         buf = (char*)malloc(sizeof(char)*BUF);
  44.         if(buf==NULL)
  45.         {
  46.                 printf("内存分配失败。-3");
  47.                 return NULL;
  48.         }

  49.         // 获取单词
  50.         for(i=0; i<=len; i++)
  51.         {
  52.                 c = (*str)[i];
  53.                 if(c==' ' || c=='\0')
  54.                 {
  55.                         buf[i] = '\0';
  56.                         break;
  57.                 }
  58.                 buf[i] = c;
  59.         }
  60.         if(c=='\0')
  61.         {
  62.                 *str = NULL;
  63.         }
  64.         else
  65.         {
  66.                 *str = *str+i*sizeof(char);
  67.         }

  68.         return buf;
  69. }

  70. /*
  71.         从用户输入的字符串获取以空格分割的个单词。
  72.         参数:
  73.                 *words[] 存储单词的指针数组。
  74.                 len *words指针数组的总长度。
  75.         返回值:
  76.                 返回所有单词的个数。
  77. */
  78. int getWords(char *words[], int len)
  79. {
  80.         char *str=NULL, *str_temp=NULL;
  81.         char *buff=NULL;
  82.         int i;

  83.         // 获取字符串
  84.         str = (char *)malloc(sizeof(char)*BUF);
  85.         if(str==NULL)
  86.         {
  87.                 printf("内存分配失败-1。");
  88.                 return 0;
  89.         }
  90.         i=1;
  91.         while(gets(str)==NULL)
  92.         {
  93.                 str = (char *)realloc(str, sizeof(char)*BUF*(++i));
  94.                 if(str==NULL)
  95.                 {
  96.                         printf("内存分配失败-2。");
  97.                         return 0;
  98.                 }
  99.         }

  100.         str_temp = str;
  101.         i=0;
  102.         while( (buff=getWord(&str_temp)) != NULL )
  103.         {
  104.                 words[i] = buff;
  105.                 i++;
  106.         }
  107.         free(str);
  108.         str=NULL;

  109.         return i;
  110. }

  111. /*
  112.         释放 *words[]指针数组中元素的内存
  113. */
  114. void freeWords(char *words[])
  115. {
  116.         char *buf;

  117.         while( (buf=*words++)!=NULL )
  118.         {
  119.                 free(buf);
  120.         }
  121. }

  122. /*
  123.         统计单词的个数。
  124.         参数:
  125.                 *words[] 存储单词的指针数组。
  126.                 *word 目标单词。
  127.         返回值:
  128.                 返回匹配到的数量。
  129. */
  130. int countWord(char *words[], char *word)
  131. {
  132.         char *buf;
  133.         int count = 0;

  134.         while( (buf=*words++)!=NULL)
  135.         {
  136.         if(!strcmp(buf, word)) ++count;
  137.     }

  138.         return count;
  139. }

  140. int main(void) {

  141.     char *words[100100]={NULL}; // 最多保存100100个字符串
  142.     char word[BUF]={'\0'};
  143.         int len = 0, count;

  144.         len = getWords(words, 100100);

  145.         printf("请输入要统计的单词(区分大小写):");
  146.         scanf("%s", &word);
  147.         count = countWord(words, word);
  148.         printf("%d\n", count);

  149.         freeWords(words);

  150.     return 0;
  151. }
复制代码

评分

参与人数 1鱼币 +5 收起 理由
1613551 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-1 20:27:46 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main() {
  4.     char fruit[20];
  5.     int count = 0;
  6.     char ch;
  7.     while (1) {
  8.         scanf("%s", fruit);
  9.         ch = getchar();
  10.         if (strcmp(fruit, "apple") == 0)
  11.             count++;
  12.         if (ch == '\n')
  13.             break;
  14.     }
  15.     printf("%d\n", count);


  16.     return 0;
  17. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-1 21:44:05 | 显示全部楼层

学到了新的知识,感谢
^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-1 22:08:21 | 显示全部楼层
人造人 发表于 2022-4-1 21:44
学到了新的知识,感谢
^_^

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-1 22:31:42 | 显示全部楼层
ba21 发表于 2022-4-1 17:20
按照题目要求,这道题并没那么简单。需要用到动态分配内存。

严重怀疑你是不是个大佬(我去,我感觉我白学了啊)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-2 15:25:59 | 显示全部楼层
本帖最后由 jhq999 于 2022-4-2 15:38 编辑
  1. int main()
  2. {
  3.         const long long suiguo[3]={*(long long*)("apple"),*(long long*)("peach"),*(long long*)("banana")};
  4.         int suiguocount[3]={0};

  5.         do
  6.         {
  7.                 long long tmp=0;
  8.                 scanf("%s",&tmp);
  9.                 for (int i = 0; i < 3; i++)
  10.                 {
  11.                         if (tmp==suiguo[i])
  12.                         {
  13.                                 suiguocount[i]++;
  14.                                 break;
  15.                         }
  16.                 }
  17.         } while (getchar()!='\n');
  18.         for (int i = 0; i < 3; i++)
  19.         {
  20.                 printf("%s:%d\n",suiguo+i,suiguocount[i]);
  21.         }                             
  22.         return 0;
  23. }
复制代码
  1. apple peach peach banana banana banana
  2. apple:1
  3. peach:2
  4. banana:3
复制代码
  1. int main()
  2. {
  3.         const long long suiguo[3][4]={{*(long long*)("apple")},{*(long long*)("peach")},{*(long long*)("banana")}};
  4.         int suiguocount[3]={0};

  5.         do
  6.         {
  7.                 long long tmp[4]={0};
  8.                 scanf("%s",tmp);
  9.                 for (int i = 0; i < 3; i++)
  10.                 {
  11.                         int j = 0;
  12.                         for (; j < 4; j++)
  13.                         {
  14.                                 if (tmp[j]!=suiguo[i][j])break;
  15.                         }
  16.                         if (4==j)
  17.                         {
  18.                                 suiguocount[i]++;
  19.                                 break;
  20.                         }
  21.                 }
  22.         } while (getchar()!='\n');
  23.         for (int i = 0; i < 3; i++)
  24.         {
  25.                 printf("%s:%d\n",suiguo+i,suiguocount[i]);
  26.         }                             
  27.         return 0;
  28. }
复制代码
  1. apple peach peach banana banana banana apple peach peach banana banana
  2. apple:2
  3. peach:4
  4. banana:5
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-16 12:16:40 | 显示全部楼层
#include <stdio.h>
using namespace std;
int main()
{
       //freopen("fruit.in","r",stdin);
       //freopen("fruit.out","w",stdout); 本地测试加上,否则死循环,online judge做题没事
        char s[10];
        int cnt=0;
        while (scanf("%s",s))//scanf读入失败返回0
                if (s[0]=='a')//第一个字符是a一定是apple
                        ++cnt;
        printf("%d",cnt);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-16 19:11:02 | 显示全部楼层
  1. // C++ 写的
  2. #include <bits/stdc++.h> // 万能文件头
  3. using namespace std;

  4. string fruit; // 每次读入水果名
  5. int ans = 0; // 答案

  6. int main(){
  7.     ios::sync_with_stdio(0);
  8.     cin.tie(0); cout.tie(0); // 写了 9-10 行会大大加快 cin 和 cout 的速度, 缺点是无法使用 scanf 和 printf
  9.    
  10.     while(cin >> fruit){ // 如果后面还有, 就一直读入
  11.         if(fruit == "apple") ans++; // 如果是 apple 就加一
  12.     }

  13.     cout << ans << endl; // 最后输出答案
  14.    
  15.     return 0;  // 结束
  16. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-16 19:12:48 | 显示全部楼层

这个是什么版本啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-16 20:19:24 | 显示全部楼层

你是说 auto const& [key, value] : fruits 这句代码?
版本好像是 C++17
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-16 23:01:32 | 显示全部楼层
以下代码只适用于使用in/out文件评测情况下

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    int ans=0;
    while(cin.get()!=EOF)
    {
        cin.unget();
        cin>>s;
        if(s[0]=='a')ans++;
    }
    cout<<ans;
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-17 09:44:33 | 显示全部楼层
傻眼貓咪 发表于 2022-8-16 20:19
你是说 auto const& [key, value] : fruits 这句代码?
版本好像是 C++17

用不了啦
只能用迭代器->first
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 12:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表