1000 Apples and 10 baskets

Source: Internet
Author: User

There are 1000 apples and 10 baskets, and the basket is numbered 1-10. Now put these 1000 apples into these 10 baskets, asking:

(1) Any 1 digits from 1-1000, which can be made up of apples of some baskets in 10 baskets.

(2) The apple in the basket is a whole, can not take part of the basket of apples, that is, once a basket is selected, the number of the basket must be counted in all.

Code:

#include <iostream> 
using namespace Std;

int Main ()  
{&NBSP;
int a[10]={1,2,4,8,16,32,64,128,256,489};
int x; 
int b=0,c=1,e=1; // B and C are a comparison of the number of input apples to the array, the E-loop

while (x!=0) {

//Compare if there is the same, exit the loop again for output results
if (x==a[b]| | X==a[c] {
cout<< "Number of apples:" <<X<<ENDL;
e=0;
}
if (e==0)
break;

Compare one by one
if (X>=a[b]&&x<=a[c]) {
X=X-A[B];
cout<< "Number of apples:" <<a[b]<<endl;
B=-1;
c=0;
}
++b;
++c;
}
cout <<endl<< "Hello World" <<endl;
return 0;
}

1000 Apples and 10 baskets

Related Article

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.