9¡¢ÊµÏÖstrcmpº¯Êý¡£ 1int strcmp(char* str1, char* str2){ 2 while(*str1 && *str2 && *str1==*str2){ 3 ++str1; 4 ++str2; 5 } 6 return *str1-*str2; 7} 10¡¢ÇóÒ»¸öÕûÐÎÖÐ1µÄλÊý¡£ 1 int f( int x) { 2 int n = 0 ; 3 while (x) { 4 ++ n; 5 x &= x - 1 ; 6 } 7 return n; 8 } 11¡¢ººÅµËþÎÊÌâ¡£ 1void tower(n,x,y,z){ 2 if(n==1) move(x,z); 3 else { 4 tower(n-1, x,z,y); 5 move(x,z); 6 tower(n-1, y,x,z); 7 } 8} 12¡¢ÈýÖùººÅµËþ×îС²½Êý¡£ 1 int f3(n) { 2 if (f3[n]) return f3[n]; 3 else { 4 if (n == 1 ) { 5 f3[n] = 1 ; 6 return 1 ; 7 } 8 f3[n] = 2 * f3(n - 1 ) + 1 ; 9 return f3[n]; 10 } 11 } ËÄÖùººÅµËþ×îС²½Êý¡£ 1int f4(n){ 2 if(f4[n]==0){ 3 if(n==1) { 4 f4[1]==1; 5 return 1; 6 } 7 min=2*f4(1)+f3(n-1); 8 for(int i=2;i<n;++i){ 9 u=2*f4(i)+f3(n-i); 10 if(u<min) min=u; 11 } 12 f4[n]=min; 13 return min; 14 } else return f4[n]; 15} 13¡¢ÔÚÒ»¸öÁ´±íÖÐɾ³ýÁíÒ»¸öÁ´±íÖеÄÔªËØ¡£ 1void delete(List m, List n) { 2 if(!m || !n) return; 3 List pre = new List(); 4 pre.next=m; 5 List a=m, b=n,head=pre; 6 while(a && b){ 7 if(a.value < b.value) { 8 a=a.next; 9 pre=pre.next; 10 }else if(a.value > b.value){ 11 b=b.next; 12 }else{ 13 a=a.next; 14 pre.next=a; 15 } 16 } 17 m=head.next; 18} 14¡¢Ò»¸öÊý×飬ϱê´Ó0µ½n£¬ÔªËØΪ´Ó0µ½nµÄÕûÊý¡£ÅжÏÆäÖÐÊÇ·ñÓÐÖظ´ÔªËØ¡£ 1int hasDuplicate(int[] a, int n){ 2 for(int i=0;i<n;++i){ 3 while(a[i]!=i && a[i]!=-1){ 4 if(a[a[i]]==-1) return 1; 5 a[i]=a[a[i]]; 6 a[a[i]]=-1; 7 } 8 if(a[i]==i) {a[i]=-1;} 9 } 10 return 0; 11} 15¡¢ÅжÏÒ»¿Å¶þ²æÊ÷ÊÇ·ñƽºâ¡£ 1int isB(Tree t){ 2 if(!t) return 0; 3 int left=isB(t.left); 4 int right=isB(t.right); 5 if( left >=0 && right >=0 && left - right <= 1 || left -right >=-1) 6 return (left<right) (right +1) : (left + 1); 7 else return -1; 8} 9 16¡¢·µ»ØÒ»¿Å¶þ²æÊ÷µÄÉî¶È¡£ 1int depth(Tree t){ 2 if(!t) return 0; 3 else { 4 int a=depth(t.right); 5 int b=depth(t.left); 6 return (a>b) (a+1):(b+1); 7 } 8} 17¡¢Á½¸öÁ´±í£¬Ò»ÉýÒ»½µ¡£ºÏ²¢ÎªÒ»¸öÉýÐòÁ´±í¡£ 1 List merge(List a, List d) { 2 List a1 = reverse(d); 3 List p = q = new List(); 4 while ( a && a1 ) { 5 if (a.value < a1.value) { 6 p.next = a; 7 a = a.next; 8 } else { 9 p.next = a1; 10 a1 = a1.next; 11 } 12 p = p.next; 13 } 14 if (a) p.next = a; 15 elseif(a1) p.next = a1; 16 return q.next; 17 } 18¡¢½«³¤ÐÍת»»Îª×Ö·û´®¡£ 1char* ltoa(long l){ 2 char[N] str; 3 int i=1,n=1; 4 while(!(l/i<10)){i*=10;++n} 5 char* str=(char*)malloc(n*sizeof(char)); 6 int j=0; 7 while(l){ 8 str[j++]=l/i; 9 l=l%i; 10 i/=10; 11 } 12 return str; 13} 19¡¢ÓÃÒ»¸öÊý¾Ý½á¹¹ÊµÏÖ 1 if (x == 0) y = a; 2 else y = b; 1 j[] = {a,b}; 2 y=j[x];
|