题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/4ec4325634634193a7cd6798037697a8

  • 思路直白,肯定还有更好的方法
#include <iostream>
using namespace std;
struct cCount{
    char ch;
    int num;
};
int main() {
    string cstr,str;
    while (getline(cin,cstr)) { 
        if(cstr=="#") break;
        getline(cin,str);
        cCount *chs=new cCount[cstr.length()];
        for(int i=0;i<cstr.length();i++){//结构体初始化
            chs[i].ch=cstr[i];
            chs[i].num=0;
        }
        for(int i=0;i<str.length();i++){//循环对比,cstr可能有重复字符
            for(int j=0;j<cstr.length();j++){
                if(str[i]==chs[j].ch){
                    chs[j].num++;
                }
            }
        }
        for(int i=0;i<cstr.length();i++){//遍历输出
            cout<<chs[i].ch<<" "<<chs[i].num<<endl;
        }
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务