hdoj 1004 Let the balloon rise (字符串处理问题)

来源:互联网 发布:炫酷网站引导页源码 编辑:程序博客网 时间:2024/05/22 13:53

思路:每输入一个字符串,跟前面的进行比较,如果有一样的前面的气球数量+1,如果没有气球种类加1

代码如下:

#include <stdio.h>#include <stdlib.h>#include <string.h>/* run this program using the console pauser or add your own getch, system("pause") or input loop */struct balloon//定义气球的结构体,存储每种气球的颜色和数量 {char cl[20];int  count;};int judge(char *p1,char *p2)//判断两个字符串是否相等;如果是指针的新手千万记得p1,p2指向的只是字符数组的首地址 {while(*p1==*p2 && *p1){p1++;p2++;} if(!*p1 && !*p2)return 1;elsereturn 0;}int main(int argc, char *argv[]) {int x;int n;int i;int key;int m;int max;char b[20];char maxball[20];struct balloon ball[1200];while(scanf("%d",&n) && n!=0){m=0;max=0;for(i=0;i<1200;i++)//一定要进行初始化,防止影响下一次的测试 {memset(ball[i].cl,'\0',sizeof(char)*20);ball[i].count=0;}for(i=0;i<n;i++)//每输入一次判断一次,如果是有过的气球则数量加1,没有的颜色种类加1 {scanf("%s",b);key=0;for(x=0;x<m;x++){if(judge(ball[x].cl,b)==1){ball[x].count++;key=1;}}if(key==0){strcpy(ball[m].cl,b);m++;}} max=ball[0].count;//找出数量最多的气球 strcpy(maxball,ball[0].cl);for(i=1;i<n;i++){if(ball[i].count>max){max=ball[i].count;strcpy(maxball,ball[i].cl);}}printf("%s\n",maxball);}return 0;}
原创粉丝点击