简体   繁体   中英

Realize equations with C#

How can I realize a equations like this in C# with custom inputs?

Input is amount of Legs, and the amount of animals.

animals: 35
legs: 94

x: Cows
y: Hens

Amount of animals
x + y = 35
y = 35 - x

Amount of legs
Hens: 2 legs
Cows: 4 legs

4x + 2y = 94

->

4x + 2y = 94
4x + 2(35 - x) = 94
4x + 70 - 2x = 94
2x = 24
x = 12

y = 35 - x = 35 - 12 = 23

-> 12 Cows -> 23 Hens

My solution applies if you have only these two types of animals.

int animals = 35;
int legs = 94;
int x = (legs - 2*animals)/2;
int y = animals -x;
Console.WriteLine(x + " Cows and " + y + " hens");

If you want to solve linear equation, then you can follow this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM