我是靠谱客的博主 香蕉斑马,这篇文章主要介绍HDOJ_1004这个题做了好久,现在分享给大家,希望可以做个参考。

【1004】

Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the 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 starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

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

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 

Sample Input
  
  
5 green red blue red red 3 pink orange pink 0
 

Sample Output
  
  
red pink

做了好久,在网上找了个答案233如下:

#include <iostream>
#include<string>
using namespace std;
int main()
{
string s[1000];                //定义字符串数组和整型数组
int a[1000];
int n,i,j;
while (cin >> n)                  //输入n表示有n个字符串
{
if (n == 0)                   //当n=0时,表示程序结束
return 0;
for (i = 0; i<n; i++)      //输入n个字符串,并把a[i]设为0
{
cin >> s[i];
a[i] = 0;
}
for (i = 0; i<n; i++)          //双重for循环判断字符串出现次数,并用a[i]来记录
{
for (j = 0; j<n; j++)
{
if (s[i] == s[j])
a[i]++;
}
}
int max = 0, k;
for (i = 0; i<n; i++)         //设最大值max=0,判断a[i]的最大值即出现最多次,把最大值的下标i赋值给k
{
if (max<a[i])
{
max = a[i];
k = i;
}
}                        //本来加了一个if(max==1)的判断 即当所有的字符串只出现一次  return 0,结果是错的
cout << s[k] << endl;
}
return 0;
}


一开始我做这个题是用的char

觉得这个答案没有限定输入字符串为15个以内,对此代码做了以下修改

(代码是直接粘贴过来的,格式啥的。。。就不要介意了吧。)

#include <iostream>
using namespace std;              //使用char型,不需声明string
int main()
{
char s[1000][15];                //使用二维数组,达到限定字符串为15个以内的设定
int a[1000];
int n, i, j;
while (cin >> n)                  //输入n表示有n个字符串
{
if (n == 0)                   //当n=0时,表示程序结束
return 0;
for (i = 0; i<n; i++)      //输入n个字符串,并把a[i]设为0
{
cin >> s[i];
a[i] = 0;
}
for (i = 0; i<n; i++)          //双重for循环判断字符串出现次数,并用a[i]来记录
{
for (j = 0; j<n; j++)
{
if (s[i] == s[j])
a[i]++;
}
}
int max = 0, k;
for (i = 0; i<n; i++)         //设最大值max=0,判断a[i]的最大值即出现最多次,把最大值的下标i赋值给k
{
if (max<a[i])
{
max = a[i];
k = i;
}
}                        
cout << s[k] << endl;
}
return 0;
}

用自己的软件测试是正确的,,,然而,杭电上是wrong answer。。。

大概是多此一举了吧。。。哇,果然人家的正确答案就很正确


【收获】

杭电的水题。。。water  都要做好久,很多时候没有编程意识。

双重否定判断重复,还有输出最多次的。。。学到了。

最近上课略有点跟不上呢,老师讲的好快,很多都是以前没有听过的。

练车好累,徐州好热。已经约到了科二,,加油加油^_^

接着去刷oj了。。。



最后

以上就是香蕉斑马最近收集整理的关于HDOJ_1004这个题做了好久的全部内容,更多相关HDOJ_1004这个题做了好久内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部