R - Let the Balloon Rise

subject

Contest time again! How excited it is to see balloonsfloating around. But to tell you a secret, the judges' favorite time isguessing the most popular problem. When the contest is over, they will countthe balloons of each color and find the result. 

This year, they decide to leave this lovely job to you. 

********************************************************************************************************

Input

Input contains multiple test cases. Each test case startswith a number N (0 < N <= 1000) -- the total number of balloonsdistributed. The next N lines contain one color each. The color of a balloon isa string of up to 15 lower-case letters. 

A test case with N = 0 terminates the input and this test case is not to beprocessed.

********************************************************************************************************

Output

For each case, print the color of balloon for the mostpopular problem on a single line. It is guaranteed that there is a uniquesolution for each test case. 

********************************************************************************************************

Sample Input

5

green

red

blue

red

red

3

pink

orange

pink

0

********************************************************************************************************

Sample Output

red

pink

********************************************************************************************************

题目来自于,https://cn.vjudge.net/contest/169626#problem/R,这个网站上面的测试用例比较少,容易过。

这种代码,就是采用了一种思想,就是答案是唯一的,也就是总是会有一个颜色最多。


第一次提交的代码就是下面的。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

struct node
{
    char x[25];
    int sum;
}a[1005];

bool cmp(node n,node m)
{
    return n.sum>m.sum;
}

int main()
{
    int n;  
    int i,j;
   while(scanf("%d",&n),n)
   {
         for(int i=0;i<n;i++)
               cin>>a[i].x;
         for( i=0;i<n;i++)
         {
             for(j=0;j<n;j++)
             {
                 if(strcmp(a[i].x,a[j].x)==0)
                     a[i].sum++;
             }
         }
         sort(a,a+n,cmp);
         printf("%s\n",a[0].x);
   } 
   return 0;
} 
提交的结果也是下面的。


********************************************************************************************************

之后想了好久,改了好多,发现只是没有初始化的问题,这也就是题目的用例过多,我们一定要记得初始化,

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

struct node
{
	char x[25];
	int sum;
}a[1005];

bool cmp(node n,node m)
{
	return n.sum>m.sum;
}

int main()
{
	int n;  
	int i,j;
   while(scanf("%d",&n),n)
   {
   	  //初始化 结构体的sum值 
   	  memset(a,0,sizeof(a));
   	  for(i=0;i<n;i++)
   	  	cin>>a[i].x;
   	  for( i=0;i<n;i++)
   	  {
   	  	for(j=0;j<n;j++)
   	  	{
   	  		if(strcmp(a[i].x,a[j].x)==0)
   	  		{
   	  			a[i].sum++;
   	  		}
   	  	}
   	  }
   	  /* 这种也可以,搞定 
		 sort(a,a+n,cmp);
   	  printf("%s\n",a[0].x);*/
   	  int t;
   	  int max =0;
   	  for(i=0;i<n;i++)
   	  {
   	  	if(max<a[i].sum)
   	  	{
   	  		max = a[i].sum;
   	  		t = i;
   	  	}
   	  }
   	  printf("%s\n",a[t].x);
   } 
   return 0;
}
********************************************************************************************************

下面还有种代码,就是之后改进的代码,如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>

using namespace std;

struct node
{
	char x[25];
	int sum;
}a[1005];

bool cmp(node n,node m)
{
	return n.sum>m.sum;
}

int main()
{
	int n; 
	int i,j;
   while(scanf("%d",&n),n)
   {
   	  memset(a,0,sizeof(a));
   	 for(int i=0;i<n;i++)
   	  {
   	  	  cin>>a[i].x;
   	  }
   	  int q=0;
   	  for( i=0;i<n && q==0;i++)
   	  {
   	  	int k =i;
   	  	for( j=0;j<n && q==0; j++)
   	  	{
   	  		if(strcmp(a[i].x,a[j].x)==0)
   	  		{
   	  			a[i].sum++;
   	  			if(a[i].sum>=n){
   	  			  printf("%s\n",a[i].x); 
					q=1;
   	  			}
   	  		}
   	  	}
   	  }
   	  if(q==0){
   	  	sort(a,a+n,cmp);
   	  	puts(a[0].x);
   	  }
   } 
   return 0;
}
添了一个判断,这种就是判断是不是全是一种颜色的,也就是相同颜色是不是达到了n个,举一个例子让大家理解一下。

例如:

 ,4

pink

pink

pink

pink

他刚开始进去的是pink,它的一次循环,sum次数变成了4,也就是达到了n次数,那么最多的就是他了,因为颜色的大小范围就是1<=sum<=n,所以就是就直接退出,

这种if(a[i].sum>=n)的判断就是判断是不是都是一种颜色的,如果是那么退出输出,不是就在下面排序,在输出。

它的循环次数最少就是一层for循环,之后就没有了。最多的话,也比上面的代码少几个n次循环,所以他的时间复杂度很低。

*********************************************************************************************************

posted @ 2017-07-11 09:17  让你一生残梦  阅读(110)  评论(0编辑  收藏  举报