【1004】
This year, they decide to leave this lovely job to you.
A test case with N = 0 terminates the input and this test case is not to be processed.
5 green red blue red red 3 pink orange pink 0
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这个题做了好久内容请搜索靠谱客的其他文章。
发表评论 取消回复