Num 12: HDOJ: 题目1004 : Let the Balloon Rise( 字符串问题 )
2015-07-24 13:38
495 查看
原题链接
题目:
Total Submission(s): 89088 Accepted Submission(s): 33718
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
需要对字符串进行处理,并记录各个字符串出现的次数,和最大数所在的字符串的位置 ( flag );
AC代码:
题目:
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 89088 Accepted Submission(s): 33718
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
5 green red blue red red 3 pink orange pink 0
[align=left]Sample Output[/align]
red pink
需要对字符串进行处理,并记录各个字符串出现的次数,和最大数所在的字符串的位置 ( flag );
AC代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int n,max[1001]; char str[1001][100]; while(scanf("%d",&n)&&n) { memset(max,0,sizeof(max)); int in,out; for(in=0; in<n; in++)//输入 n 个字符串; { scanf("%s",&str[in]); } for(in=0; in<n; in++)//从第一个字符串开始到最后一个字符串比较是否相同; for(out=0; out<n; out++)//每次都扫描所有的字符串; { if(strcmp((str[out]),(str[in]))==0) max[in]++;//记录每个字符串数目 } int big=-1,flag;//big用于找出最大的出现次数;flag记录位置; for(in=0; in<n; in++)//寻找最大次数; { if(big<max[in]) { big=max[in]; flag=in; } } printf("%s\n",str[flag]); } return 0; }
相关文章推荐
- Struts2返回JSON对象的方法总结
- nopCommerce的源代码结构和架构
- #笔记#圣思园 JavaWeb 第57讲——JS继承:对象冒充、call方法、apply方法、原型链方式、混合方式
- wxSqlite给sqlite数据库添加口令
- I2C总线和SPI总线
- java面向对象--05
- python MySQL数据库连接
- 方法内部lambda表达式中若用到变量,若在方法内lambda外,则只能为final,或者定义为全局变量
- 快速排序算法
- 发生系统错误 1067
- MYSQL :小案例金山词霸
- [LeetCode]Search a 2D Matrix II
- 二级域名绑定ECS
- 多了一层或者多层响应者 如何实现跳转
- POJ题目1947 Rebuilding Roads(树形dp)
- 操作系统 内存管理(二)
- Ubuntu 14.04 64位字体美化(使用黑文泉驿)
- linux服务器做路由,映射同网段机器
- 02SkypeForBusiness2015--拓扑发布以及DNS更新
- Spring整合 RMI