Advertisement
apl-mhd

modifie gap index

Aug 3rd, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int solution(int N);
  4.  
  5.  
  6. int main(){
  7.  
  8.  
  9.     printf(" %d ", solution(10));
  10.  
  11.  
  12.  
  13.     return 0;
  14. }
  15.  
  16.  
  17. int solution(int N){
  18.  
  19.  
  20.  
  21.  
  22.  
  23.     int number =74901729, count = 0, i, j, temp, gapIndex = 0, gapCount = 0;
  24.  
  25.    int  binaryConvertArray[100], binaryGap[100];
  26.  
  27.    /*decimal to binary convert section*/
  28.  
  29.     while(number !=0 ){
  30.  
  31.         binaryConvertArray[count] = number % 2;
  32.  
  33.         number /=2;
  34.         count++;
  35.  
  36.  
  37.     }
  38.  
  39.  
  40.     /* binary array reverse section*/
  41.  
  42.     j = count -1, i = 0;
  43.  
  44.     while(i < j){
  45.  
  46.         temp = binaryConvertArray[i];
  47.  
  48.         binaryConvertArray[i] = binaryConvertArray[j];
  49.         binaryConvertArray[j] = temp;
  50.         i++;
  51.         j--;
  52.  
  53.  
  54.     }
  55.  
  56.  
  57.     for(i = 0; i < count; i++){  /*print binary to decimal number */
  58.  
  59.      printf(" %d ", binaryConvertArray[i]);
  60.  
  61.     }
  62.  
  63.     printf("\n");
  64.  
  65.  
  66.     /*searching gap index*/
  67.  
  68.     for(i = 1; i < count; i++){
  69.  
  70.         if(binaryConvertArray[i] == 0){
  71.  
  72.              gapCount++;
  73.  
  74.  
  75.         }
  76.  
  77.         if(binaryConvertArray[i] == 1){
  78.  
  79.            // printf("gapcount =  %d \n", gapCount);
  80.  
  81.             binaryGap[gapIndex] = gapCount;
  82.             gapCount = 0;
  83.             gapIndex++;
  84.  
  85.         }
  86.  
  87.     }
  88.  
  89.     printf("again bggap  %d %d\n", gapIndex, binaryGap[0]);
  90.  
  91.  
  92.     for(i = 0; i < gapIndex; i++){  /*print binary to decimal number */
  93.  
  94.      printf("binary gap %d \n", binaryGap[i]);
  95.  
  96.     }
  97.  
  98.     printf("gapindex %d \n", gapIndex);
  99.  
  100.  
  101.     /*bouble sort find largest binary gap lol, eto pera kena???*/
  102.     temp = 0;
  103.  
  104.     for(i = 0; i < gapIndex; i++){
  105.  
  106.         for(j = 0; j < gapIndex - 1; j++){
  107.  
  108.             if( binaryGap[j] < binaryGap[j+1]){
  109.  
  110.                 temp = binaryGap[j];
  111.  
  112.                binaryGap[j] = binaryGap[j+1];
  113.  
  114.                binaryGap[j+1] = temp;
  115.             }
  116.  
  117.         }
  118.     }
  119.  
  120.     printf("\nhigest to low gap\n");
  121.  
  122.     for(i = 0; i< gapIndex; i++){
  123.  
  124.         printf(" %d ", binaryGap[i]);
  125.  
  126.     }
  127.     printf("\n");
  128.  
  129.  
  130.     printf("bgapindex %d %d\n", binaryGap[0], gapIndex);
  131.  
  132.     if(gapIndex == 0 || gapIndex == 0 && binaryGap[0] == 1 ){
  133.  
  134.         printf("haha");
  135.         return 0;
  136.     }
  137.  
  138.     else{
  139.  
  140.         printf("Alhamdu-lillha");
  141.        return binaryGap[0];
  142.  
  143.     }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement