1988博客园

无需羡慕,不需狗血,耐得住寂寞,经得起推敲,我们自会拥有最有安全感的人生。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 static void Main(string[] args)
 2         {
 3             int n;//问题规模
 4             int count = 0;//记录循环的次数
 5             int res=0;//记录寻找到的结果又几种
 6             Console.WriteLine("请输入问题规模n的值:");
 7             n = Convert.ToInt32(Console.ReadLine());
 8             TestOne(n, ref count,ref res);
 9             Console.WriteLine("方法一运行次数:" + count + "次;"+"结果有"+res+"种。");
10 
11             count = 0;res=0;
12             TestTwo(n,ref count,ref res);
13 
14             Console.WriteLine("方法二运行次数:" + count +  "次;"+"结果有"+res+"种。");
15             Console.ReadKey();
16         }
 1         static void TestTwo(int n, ref int count,ref int res)
 2         {
 3             Console.WriteLine("方法二:");
 4             for (int i = 0; i < n; i = i + 3)
 5             {
 6                 count++;
 7 
 8                 if ((((4 * i / 3.0) - n) == Convert.ToInt32((4 * i / 3.0) - n)) && (Convert.ToInt32((4 * i / 3.0) - n) > 0) && ((2 * n - (7 * i / 3.0)) == Convert.ToInt32(2 * n - (7 * i / 3.0))) && (Convert.ToInt32(2 * n - (7 * i / 3.0)) > 0))
 9                     {
10                         if (i > 766)
11                         { }
12                         Console.Write("公鸡有:" + ((4 * i / 3.0) - n) + "只;");
13                         Console.Write("母鸡有:" + (2 * n - (7 * i / 3.0)) + "只;");
14                         Console.Write("小鸡有:" + i + "只。\n");
15                         res++;
16                     }
17                 
18 
19             }
20         }
 1        static void TestOne(int n,ref int count,ref int res)
 2         {
 3             for (int i = 1; i < (n / 5); i++)
 4                 for (int j = 1; j < (n / 3); j++)
 5                 {
 6                     count++;
 7                     if (((n - i - j) % 3 == 0) && (7*i+4*j == n))
 8                     {
 9                         Console.Write("公鸡有:" + i + "只;");
10                         Console.Write("母鸡有:" + j + "只;");
11                         Console.Write("小鸡有:" + (n - i - j) + "只。\n");
12                         res++;
13                     }
14                 }
15         }

 

posted on 2016-03-10 15:20  1988kxj  阅读(668)  评论(0编辑  收藏  举报