Induction algorithm--coin flipping problem __ algorithm

Source: Internet
Author: User

Q: There are any n coins facing up, each flipping n coins know to flip all n coins to the opposite side. (* indicates positive, 0 means reverse)


The source code is as follows:

/* * * Flip coin problem, set a total of n coins, once allowed to flip n<n, need to flip x step, each coin flipped y times, * * n*y==n*x, that is, n/n==y/x, because to flip, y must be odd, Judge n/n if the simplest score, such as N is not an odd number can not flip success * *
If N is an odd number, then x takes the minimum value for n=y,n=x, if n/n is not the simplest fraction, the simplest fraction n1/n1, according to the above steps * * To determine, if the N1 is odd can be flipped.
*/#include <stdio.h> #include <string.h> char s[100];
int GCB (int a,int b);
int Is_even (int a);


 int turn_over (int n,int n,int x);
      int main () {int n,n,x,y,g;
      Int J;
      printf ("Please enter the number of coins: \ n");
      scanf ("%d", &n);
          Do {printf ("Please enter the number of coins to flip once: \ n");
          scanf ("%d", &n);
          g = GCB (N,N);
          y = n/g; if (Is_even (y) ==0) printf ("cannot be flipped successfully.")
      \ n ");

      }while (Is_even (y) ==0);
      x = n/g;
      printf ("Initial state: \ n");
          for (j=0;j<n;j++) {s[j]= ' * ';
      printf ("%3c", ' * ');
      } printf ("\ n");

       Turn_over (N,N,X);
  return 0;
    } int GCB (int a,int b)//Two number of greatest common divisor {int c;
        if (b>a) {c = A;
        A = b;
    b = C;
} while (b! = 0) {        c = a%b;
        A = b;
    b = C;
} return A;
    } int Is_even (int a)//to determine if an even number is, then return 0 {while (a > 0) {a = A-2;
} return A;
    } int turn_over (int n,int n,int x)/* Implements the rollover function */{int i,j,k;  for (i = 0;i<x;i++) {j = n/x* (i);
      The starting position of the i+1 step rollover printf ("Step%d: \ n", i+1);
      Getch ();
          for (k=1;k<=n;k++)//k count function, which is to flip n {if (s[j]== ' 0 ') s[j] = ' * ' At each start from s[j];
          else s[j] = ' 0 ';  j = (j+1)%N;
          J reaches the upper limit, but does not flip enough N to flip from the beginning} for (j = 0;j<n;j++) printf ("%3c", S[j]);
    printf ("\ n");
} return 0;
 }
Is relatively simple, there should be no doubt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.