百钱百鸡问题
这个比较简单,直接看代码吧.
using System;
using System.Collections.Generic;
using System.Text;
namespace 百钱百鸡问题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("公鸡五元一只, 母鸡3元一只, 小鸡1元3只, 一百块钱买一百只鸡各买多少只?");
Console.ReadLine();
List<Results> re = new List<Results>();
for (int i = 0; i <= 20; i++)
{
for (int k = 0; k <= 100; k++)
{
if (k % 3 != 0)
continue;//鸡的数目必须是整数.
re.Add(Caculate(i,k));
}
}
foreach(Results r in re)
{
if (r.GetTotal() == 100)
{
Console.WriteLine(string.Format("公: {0}, 母: {1}, 小: {2}, 总数: {3}", r.Man.ToString(),r.Woman.ToString(),r.Children.ToString(),r.GetTotal()));
}
}
Console.ReadLine();
}
public static Results Caculate(int man, int child)
{
Results r = new Results();
r.Man = man;
r.Children = child;
r.Woman = (100 - child / 3 - 5 * man) / 3;
return r;
}
}
public struct Results
{
public int Man
{
get;
set;
}
public int Woman
{
get;
set;
}
public int Children
{
get;
set;
}
public int GetTotal()
{
if (Man <= 0)
return 0;
if (Woman <= 0)
return 0;
if (Children <= 0)
return 0;
return Man + Woman + Children;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace 百钱百鸡问题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("公鸡五元一只, 母鸡3元一只, 小鸡1元3只, 一百块钱买一百只鸡各买多少只?");
Console.ReadLine();
List<Results> re = new List<Results>();
for (int i = 0; i <= 20; i++)
{
for (int k = 0; k <= 100; k++)
{
if (k % 3 != 0)
continue;//鸡的数目必须是整数.
re.Add(Caculate(i,k));
}
}
foreach(Results r in re)
{
if (r.GetTotal() == 100)
{
Console.WriteLine(string.Format("公: {0}, 母: {1}, 小: {2}, 总数: {3}", r.Man.ToString(),r.Woman.ToString(),r.Children.ToString(),r.GetTotal()));
}
}
Console.ReadLine();
}
public static Results Caculate(int man, int child)
{
Results r = new Results();
r.Man = man;
r.Children = child;
r.Woman = (100 - child / 3 - 5 * man) / 3;
return r;
}
}
public struct Results
{
public int Man
{
get;
set;
}
public int Woman
{
get;
set;
}
public int Children
{
get;
set;
}
public int GetTotal()
{
if (Man <= 0)
return 0;
if (Woman <= 0)
return 0;
if (Children <= 0)
return 0;
return Man + Woman + Children;
}
}
}