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

思路:每输入一个字符串,跟前面的进行比较,如果有一样的前面的气球数量+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;
	else
		return 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值