杭电oj1004java_杭电OJ 1004实现和一些注意的陷阱

1004题其实很简单,就是要你统计最多气球的个数,但是在输出的时候却需要认真仔细,符合题目要求。

在输出是首先注意的是输出顺序:如果有多个数量相同的颜色,先输入的先输出,如,

red

green

red

green

这种情况需要输出

red

green

贴出AC代码:

#include

#include

#include

int main(){

int n = 1;

int i = 0;

int max = 0;

int maxindex = 0;

int j = 0;

char color[1000][16];

int *cnum;

while(n){

scanf("%d",&n);

cnum = (int *)malloc(sizeof(int) * n);

for(i = 0;i < n;i ++)

{

scanf("%s",&color[i]);

}

for(i = n - 1;i >= 0;i --)

{

cnum[i] = 1;

if(i != n - 1){

for(j = n - 1;j > i;j -- )

if(strcmp(color[i],color[j]) == 0){

cnum[i] ++;

}

}

}

max = cnum[0];

maxindex = 0;

for(i = 0;i < n;i ++){

if(max < cnum[i]){

max = cnum[i];

maxindex = i;

}

}

for(i = 0;i < n;i ++)

if(max == cnum[i]){

printf("%s\n",color[i]);

}

}

return 0;

}

2016.11.28更新===============================================

#include#include

intmain(){intn;

scanf("%d",&n);while(n != 0){char color[1000][15];int count[1000] = {0};int currentColor = 0;//有多少种颜色

int i = 0;//开始输入

for(i; i < n; ++i){char s[15];int haveSameColor = 0;int j = 0;

scanf("%s",&s);for(j ; j < currentColor; ++j){if(!strcmp(s,color[j])){

count[j]++;

haveSameColor= 1;break;

}

}if(!haveSameColor)

{

strcpy(color[currentColor++],s);

}

}int maxIndex = 0;int max = count[0];for(i = 1;i < currentColor;++i){if(count[i] >max){

max=count[i];

maxIndex=i;

}

}

printf("%s\n",color[maxIndex]);

scanf("%d",&n);

}return 0;

}

注意:bool型有的时候会出现一些错误,所以这里使用整形来代替

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值