4.百元买百鸡问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
for (a = 0; a < 30; a++)
for (b = 0; b < 75; b++)
for (c = 0; c < 150; c++)
if (a + b + c == 150 && (5 * a + 2 * b + c / 5 == 150) && c % 5 == 0)
Console.WriteLine("母鸡{0}只,公鸡{1}只,鸡仔{2}只", a, b, c);
Console.ReadLine();
}
}
}
5.韩信点兵
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int x;
for (x = 1; x <= 105; x++)
{
if (x % 3 == 2 && x % 5 == 3 && x % 7 == 5)
Console.WriteLine("韩信共有{0}个兵", x);
}
Console.ReadLine();
}
}
}
6.鸡兔同笼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int a, b;
for (a = 1; a <= 50; a++)
{
for (b = 1; b <= 25; b++)
{
if (2 * a + 4 * b == 100 && a + b == 30)
Console.WriteLine("鸡有{0}只,兔有{1}只", a, b);
}
}
Console.ReadLine();
}
}
}
7.100以内所有偶数的和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i, s = 0;
for (i = 1; i <= 100; i++)
{
if (i % 2 != 0)
continue;
s += i;
}
Console.WriteLine("100以内的所有偶数之和为{0}", s);
Console.ReadLine();
}
}
}
8.水仙花数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int a, b, c, d = 100;
for (; d < 1000; d++)
{
a = d % 10;
b = d / 10 % 10;
c = d / 100;
if (a * a * a + b * b * b + c * c * c == d)
Console.WriteLine("{0}是水仙花数", d);
}
Console.ReadLine();
}
}
}
9.求1+3+5+......+n>500的最小正整数n
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n = 1, s = 0;
while (n > 0)
{
s += n;
n = n + 2;
if (s >= 500)
break;
}
Console.WriteLine("最小正整数为{0}", n);
Console.ReadLine();
}
}
}