寻找过半数的字符

#include <stdio.h>

find(char* str, int n){
    int i = 1;
    char temp = str[0];
    int num = 1;
    while(i < n){
        if(str[i] == temp){
            num++;
        }
        else if(num == 0){
            temp = str[i];
            num = 0;
        }
        else{
            num--;
        }
        i++;
    }
    printf("%c", temp);
}

main(){
    char str[] = {'a', 'b', 'c', 'a', 'd', 'a', 'd', 'a', 'a', 'd'};
    find(str, 10);
}

 

posted @ 2014-03-03 09:01  yutoulck  阅读(121)  评论(0编辑  收藏  举报