HDU 1004 Let the Balloon Rise(AC代码)
#include <stdio.h>
#include <string.h>
char a[][];
int b[]={};
int main()
{
int n,i,j,k,max,loc;
while(scanf("%d",&n)!=EOF&&n!=){
max=-;
k=;
loc=;
for(i=;i<n;i++){
scanf("%s",a[k]);
for(j=;j<k;j++){
if(strcmp(a[j],a[k])==){
b[j]++;
k--;
break;
}
}
k++;
}
for(i=;i<k;i++){
if(b[i]>max){
max=b[i];
loc=i;
}
b[i]=;
}
puts(a[loc]);
}
return ;
}
gets遇到回车才结束,并把回车符改为'\0'再存到字符串。注:如果输入的数字过长,这个函数会出问题,因为他不判断输入的长度的。
puts只要遇到第一个'\0'就会输出,并自动输出一个换行符。
格式:这个格式没很大问题,遇到0就结束,而不用输出换行再结束。
思路:
1、题中明确说答案只会有一个,也就是不会出现比如:2 red green的情况。
2、一个二维的字符数组,保存输入的颜色。
3、一个int型数组记录每个颜色出现的次数。(我把他初始化为0,是因为次数不重要,重要的是谁是最多的,实际上应该是该数字+1)
技巧:
1、每输入一个颜色,就对比前面输入的颜色中有没有出现过,若有,在该颜色位置对应的int数组上+1。
2、若输入的已经在之前的颜色中存在,在1中已经记录过出现的次数,那么颜色数组中刚输入的位置就可以重复利用了。
3、若输入的没有在之前的颜色中存在(即新颜色),该位置就不能被覆盖了。
4、输完之后,只需要对比一下int数组中的前k个就行啦(看代码),不用扫整个int数组了,这对于“大部分都是重复出现的颜色”情况就好多了,要是只有两个颜色,一次对比就搞定。
HDU 1004 Let the Balloon Rise(AC代码)的更多相关文章
- hdu 1004 Let the Balloon Rise(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- HDU 1004 Let the Balloon Rise(map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- HDU 1004 Let the Balloon Rise【STL<map>】
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1004 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU 1004 Let the Balloon Rise map
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1004 Let the Balloon Rise strcmp、map、trie树
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1004 Let the Balloon Rise 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 用STL 中的 Map 写的 #include <iostream> #includ ...
- HDU 1004 Let the Balloon Rise(STL初体验之map)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- HDU 1004 - Let the Balloon Rise(map 用法样例)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
随机推荐
- SecureCRT中的ftp文件上传
原文地址:http://www.blogbus.com/jjuan-flake-logs/59745331.html SecureCRT与SshClient不同的就是,SecureCRT没有图形化的文 ...
- Your content must have a ListView whose id attribute is 'android.R.id.list'
修改ListView的ID为 ' @android:id/list ' <ListView android:id="@android:id/list" android:lay ...
- Linux学习之CentOS--CentOS6.4下Mysql数据库的安装与配置【转】
如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲解了Linux学习之CentOS(七)--C ...
- JBoss像tomcat那样创建部署文件,JBoss创建虚拟目录
jboss可以像tomcat那样,写一个配置文件,指向应用所在的路径,而不用将应用直接复制到deploy下的某一个以.war结尾的文件夹下吗? 答:好像是不能直接操作,但是可以通过变通的方式来搞定.在 ...
- HtmlHelper—DropDownList:SelectList、SelectListItem
前言 在项目中经常使用到DropDownList来显示数据库中的数据,典型的例子为为某书籍选择所属类型. 使用SelectList来实现: 实现一: Controller 代码 SelectList ...
- 使用SMSManager短信管理器发送短信
import android.os.Bundle;import android.app.Activity;import android.app.PendingIntent;import android ...
- [Js]基础知识
一.JavaScript组成 1.ECMAScript 解释器.翻译(提供功能有限,如加减乘除,定义变量.函数等) 几乎没有兼容性问题 2.DOM 有一些兼容性问题 3.BOM(用的少,交互 ...
- bzoj 1951: [Sdoi2010]古代猪文
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #defin ...
- c# 配置文件之configSections配置
对于小型项目来说,配置信息可以通过appSettings进行配置,而如果配置信息太多,appSettings显得有些乱,而且在开发人员调用时,也不够友好,节点名称很容易写错,这时,我们有几种解决方案 ...
- 根据窗体自动调整控件及文本框记住上次填写内容Demo
第一次写文章,组词难免没有不通之处... 最近常用到Winform根据窗体大小自动调整空间大小及字体.文本框记住上次填写内容待下次输入某一段时候自动跳出上次输入内容.于是就随便把两个问题放到同一个de ...