Shell programming, 100 money to buy 100 chickens, simple implementation

Source: Internet
Author: User

There is an interesting question today:

A group of friends wrote with a shell:

#!/bin/bash# hundred million chicken, each chicken at least buy one all= -# Cocks per how many yuan read-P'Rooster How many text one:'gongji# hen per how many yuan read-P'How many hens a hen:'muji# can buy a few chickens per dollar read-P'chick How many only one article:'xiaoji# hundred dollars to buy a few cocks gongji_count=`Expr${all}/${gongji} 'Echo "======== hundred yuan to buy hundred chicken ========"Echo "Rooster ${gongji} text Money one, hen ${muji} text money A, chicken a penny ${xiaoji} only, with 100 text money buy 100 chickens, rooster, hen, chicken must have, ask rooster, hen, chicken how many only? "Echo "Male and female small" forAinch`seq${gongji_count} ' DoF=`Expr${all}-${a} \*${gongji} ' G=`Expr${F}/${muji} ' forBinch`seq${g} ' DoD=`Expr${all}-$a-$b ' [['Expr${D}% ${xiaoji} ' = =0]] && c= 'Expr${D}/${xiaoji} ' | |continue [['Expr${a} \* ${gongji} + ${b} \* ${muji} + ${c} ' = = ${all}] &&Echo${a} ${b} 'Expr${C} \*${xiaoji} ' Done Done

But I didn't read it, and it was slow.

I wrote a simple implementation myself:

#!/bin/bash# #公鸡5块一只, hen 3 pieces of a, chicken 1 Yuan 3 only # for 100 dollars to buy 100 chickens, there are several ways to buy the law # # #version0.2Read-P"define the price of the rooster (default is 5):"Gjpread-P"define the price of the hen (default is 3):"Mjpread-P"define the price of a chick to buy for a piece of money? (default = 3)": xjp[-Z $GJP] && gjp=5[ -Z $MJP] && mjp=3[ -Z $XJP] && xjp=3[$[$gjp+ $MJP + $XJP]-eq0] &&Echo "wrong input!"&& exit1; Gjmax=$[ -/$GJP]mjmax=$[ -/$MJP] for((i=1; i<= $gjmax; i++)); Do#公鸡的循环 for((j=1; j<= $mjmax; j + +)); Do#母鸡的循环 XJ=$[( --$i * $GJP-$j * $MJP) *$XJP] #公鸡加母鸡的总价算出可购买的小鸡的总数 [$[$xj+ $i + $j]-eq -] && [$[$xj/$xjp + $i * $gjp + $j * $MJP]-eq -] &&Echo "Rooster: $i, hen: $j, chick: $xj"||continue; # Total quantity is 100 total price is 100 yuan Done Done

We simply analyzed the algorithm and found that the for loop is not the same

For example: for A in 'seq ${gongji_count} '

All results need to be put into list, written to memory, and then read

This may be one of the reasons for the impact of the operation, welcome big guy Analysis Message

Thanks for the selfless supply of the source of the wild God Shrimp

Shell programming, 100 money to buy 100 chickens, simple implementation

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.