Dry Wine If you sell the recursive algorithm

Source: Internet
Author: User

Dry Wine If you sell the recursive algorithm

Remember that old song, if the wine is not sold, it contains a story of how bitter. Life is not easy, thrift resources recovery and reuse it!
Children's winter vacation homework has not been done, there must be so that adults are blindfolded, the computer's liquor that is seconds to kill millions of bottles!

Topic:
    • Beer 2 yuan 1 bottles, 4 caps for a bottle, 2 empty bottles for 1 bottles
    • Q: How many bottles can I drink for 100 bucks?
Recursive algorithm implementation:
#define N   50int total = 0;int round = 0;void beer_bottle_lid(int beer, int bottle, int lid){    round++;    printf ("第%d轮:喝了%d瓶酒,剩下%d个空瓶子,剩下%d个瓶盖子\n", round, beer, bottle, lid);    int beer1 = (bottle)/2;    int beer2 = (lid)/4;    int bot = (bottle)%2;    int li = (lid)%4;    int new_beer = (beer1+beer2);    int new_bottle = (new_beer+bot);    int new_lid = (new_beer+li);    total += new_beer;      if (new_beer == 0){        return;    }    beer_bottle_lid(new_beer, new_bottle, new_lid);}//---------------------------int main(void){    total += N;    beer_bottle_lid(N, N, N);    printf ("\n\r总共喝了瓶酒,也是醉了!\n", total);    return 0;}
Test results
第1轮:喝了50瓶酒,剩下50个空瓶子,剩下50个瓶盖子第2轮:喝了37瓶酒,剩下37个空瓶子,剩下39个瓶盖子第3轮:喝了27瓶酒,剩下28个空瓶子,剩下30个瓶盖子第4轮:喝了21瓶酒,剩下21个空瓶子,剩下23个瓶盖子第5轮:喝了15瓶酒,剩下16个空瓶子,剩下18个瓶盖子第6轮:喝了12瓶酒,剩下12个空瓶子,剩下14个瓶盖子第7轮:喝了9瓶酒,剩下9个空瓶子,剩下11个瓶盖子第8轮:喝了6瓶酒,剩下7个空瓶子,剩下9个瓶盖子第9轮:喝了5瓶酒,剩下6个空瓶子,剩下6个瓶盖子第10轮:喝了4瓶酒,剩下4个空瓶子,剩下6个瓶盖子第11轮:喝了3瓶酒,剩下3个空瓶子,剩下5个瓶盖子第12轮:喝了2瓶酒,剩下3个空瓶子,剩下3个瓶盖子第13轮:喝了1瓶酒,剩下2个空瓶子,剩下4个瓶盖子第14轮:喝了2瓶酒,剩下2个空瓶子,剩下2个瓶盖子第15轮:喝了1瓶酒,剩下1个空瓶子,剩下3个瓶盖子总共喝了195瓶酒,也是醉了

Dry Wine If you sell the recursive algorithm

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.