一个整数数组中一半以上数据是相同的,快速找出
1 #include <stdio.h> 2 int main(int argc, char const *argv[]) 3 { 4 int a[argc-1]; 5 for(int i = 1;i<argc;i++){ 6 a[i-1] = atoi(argv[i]); 7 } 8 findMost(a,argc-1); 9 return 0; 10 } 11 12 int findMost(int *a,int size){ 13 int current; 14 int num = 0; 15 if(size<1){ 16 return -1; 17 } 18 current = a[0]; 19 for(int i = 0;i<size; i++){ 20 21 if(a[i]==current){ 22 num++; 23 // printf("c:%d ", num); 24 }else{ 25 // printf("d:%d ", num); 26 num--; 27 } 28 if(num == 0){ 29 current = a[i]; 30 num=1; 31 } 32 33 } 34 printf("number:%d,count:%d ", current,num); 35 }
路径问题:
#include <stdio.h> struct point { int x; int y; } p; int main(int argc, char const *argv[]) { //printf("%s ", argv[1]); int number = atoi(argv[1]); position(number); printf("x:%d y:%d ", p.x,p.y); return 0; } void position(int num){ p.x = 0; p.y = 0; if(num == 1){ return; } int circle = 1; int direct = 0; int step = circle; int tem = 2; for(int i = 1; i<num;i++){ printf("%d ", direct); switch(direct){ case 0:{ p.x++; break; } case 1:{ p.y++; break; } case 2:{ p.x--; break; } case 3:{ p.y--; break; } } step--; if(step==0){ tem --; direct = (direct+1)%4; printf("c:%d ", direct); if(tem==0){ circle++; tem = 2; } step = circle; } } }