(hundred cases programming) 17. Divide Seven baskets of fish (deep)

Source: Internet
Author: User

Title: A, B, c three fish to sea fishing, they with the ship with 21 baskets. When returning home that night, they found seven baskets full of fish, and seven baskets of half a basket of fish, the other seven baskets are empty, because they do not have a scale, had to visually consider that seven full-basket fish weight is equal, 7 half-basket fish weight is equal. How to divide the fish and baskets into three parts without pouring the fish out.

Tips:

According to test instructions: Everyone should be divided into seven baskets, of which there are 3.5 basket of fish. Use an 3*3 array A to represent what three people are divided into. Each of them corresponds to a row of array A, the No. 0 column of the array is divided into the number of baskets of the fish, the 1th column of the array is divided into half baskets, and the number of empty baskets is divided into the 2nd column of the array. By topic can be launched:
。 The sum of the elements of each row or column of an array is 7;
。 For the rows of an array, the number of full baskets plus half baskets = 3.5;
。 The number of baskets for each person cannot exceed 3 baskets;
。 Everyone must have at least 1 and a half baskets, and the half-basket number is definitely odd
For some of the fish scheme found, three people who take which share is the same, in order to avoid duplication of the allocation scheme, can be stipulated: the second person's full basket number equals the first person's full basket number; the second person's half basket number is greater than the first person's half basket number.

 #include <iostream> using namespace std; int main (void) {int i,j,k,m,n,flag,a[3][3];
   for (i=0;i<=3;i++)//First the full basket is judged {a[0][0]=i;
    for (j=i;j<7-i&&j<=3;j++) {a[1][0]=j;
    if ((a[2][0]=7-a[0][0]-j) >3) continue;
    if (a[2][0]<a[1][0]) break;
     for (k=1;k<=5;k+=2)//half-basket judgment {a[0][1]=k;
      for (m=1;m<7-k;m+=2) {a[1][1]=m;
      A[2][1]=7-m-k;
       
        for (flag=1,n=0;flag&&n<3;n++)//judgment on Empty baskets if (a[n][0]+a[n][1]<7&&a[n][0]*2+a[n][1]==7)
       
       A[N][2]=7-A[N][0]-A[N][1];
       else flag=0;
         if (flag) {for (Int. q=0;q<3;q++) for (int t=0;t<3;t++) {if (t==0)
         "cout<<" <<q+1<< "person may be divided into:";
         cout<<a[q][t]<< "";
        if (t==2) cout<<endl; 
}}}}}} return 0; }


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.