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


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

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

    red

    green

    red

    green

    这种情况需要输出

    red

    green

    贴出AC代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    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
    ",color[i]);
    			}
    		
    
    	}
    	
    	return 0;
    }
    

      

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

    #include<stdio.h>
    #include<string.h>
    
    int main(){
        int n;
        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
    ",color[maxIndex]);
            scanf("%d",&n);
        }
        return 0;
    }

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

    THISSKY出品,原文链接:http://www.cnblogs.com/zhuhongjongy/p/4959522.html 

  • 相关阅读:
    ADO.Net对Oracle数据库的操作(转)
    代码反思(1)
    继承与多态
    存储过程
    linux学习流程及内容概括
    Linux下终端快捷键
    查找算法
    epoll解读
    TCP/udp编程
    如何学习嵌入式
  • 原文地址:https://www.cnblogs.com/zhuhongjongy/p/4959522.html
Copyright © 2020-2023  润新知