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了。。。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值