简体   繁体   English

用 C# 实现方程

[英]Realize equations with C#

How can I realize a equations like this in C# with custom inputs?如何使用自定义输入在 C# 中实现这样的方程?

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 -> 12 头奶牛 -> 23 只母鸡

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如果你想解线性方程,那么你可以按照这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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