【校选题解】7-5 谁是龙王 (15分)

it2025-05-06  22

晚上复习了一下字符串函数 重现的时候豁然开朗(我太菜了5555) 字符串函数(笔记)

代码如下:

#include<iostream> #include<string.h> using namespace std; //定义结构体数组 存放名字和对应名字的出现次数 struct _stds { char name[1000]; int sum=0; } stds[1000]; int main() { int index=0;//记录名字数 后面遍历 int n;cin>>n; char a[10000];//char a[] 会有奇怪的bug 不知道为啥 for(int i=1;i<=n;i++) { //初始化 bool pd=1;//判断 名字是否出现过 memset(a,0,sizeof(a)); cin>>a; for(int j=0;j<index;j++) { if(!strcmp(a, stds[j].name))//名字出现过 { stds[j].sum++; pd=0; } } if(pd)//名字没出现过 { strcpy(stds[index].name,a);//将新名字拷贝到stds中 stds[index++].sum++;//名字数++ } } //遍历,找到出现最多次的名字 int max=-1; for(int i=0;i<index;i++) if(stds[i].sum>=max) max=stds[i].sum; for(int i=0;i<index;i++) { if(stds[i].sum==max) { cout<<stds[i].name; break; } } return 0; }
最新回复(0)