c# 经典521例(4)
一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。编程序找出1000之内的所有完数,并按下面格式输出其因子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int s;
for (int i = 1; i < 1000; i++)
{
s = i;
for (int j = 1; j < i; j++)
{
if (i % j == 0)
{ s -= j; }
}
if (s == 0)
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
}
}
打印出以下杨辉三角形(要求打印出10行)。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[10, 10];
for (int i = 0; i < 10; i++)
{
a[i, 0] = 1;
a[i, i] = 1;
}
for (int i = 2; i < 10; i++)
{
for (int j = 1; j < i; j++)
{
a[i, j] = a[i - 1, j - 1] + a[i - 1, j];
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j <= i; j++)
{
Console.Write(a[i,j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
有 1 、2 、3 、4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 5; i++)
{
for (int j = 1; j < 5; j++)
{
for (int k = 1; k < 5; k++)
{
if (i != k && i != j && j != k) //确保 i 、 j 、 k 三位互不相同
Console.WriteLine("{0}{1}{2}", i, j, k);
}
}
}
Console.ReadLine();
}
}
}
一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
double x, y;
for (int i = 1; i < 100000; i++)
{
x = Math.Sqrt(i + 100); //x 为加上 100 后开方后的结果
y = Math.Sqrt(i + 268); //y 为再加上 168 后开方后的结果
if (x * x == i + 100 && y * y == i + 268)// 如果一个数的平方根的平方等于该数,这说明此数是完全平方数
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
输入某年某月某日,判断这一天是这一年的第几天?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int day, month, year, sum = 0, leap;
Console.WriteLine("请输入年");
year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月");
month = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入日");
day = Convert.ToInt32(Console.ReadLine());
switch (month)
{
case 1: sum = 0; break;
case 2: sum = 31; break;
case 3: sum = 59; break;
case 4: sum = 90; break;
case 5: sum = 120; break;
case 6: sum = 151; break;
case 7: sum = 181; break;
case 8: sum = 212; break;
case 9: sum = 243; break;
case 10: sum = 273; break;
case 11: sum = 304; break;
case 12: sum = 334; break;
default: Console.WriteLine("数据错误"); break;
}
sum = sum + day; /*再加上某天的天数*/
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))/*判断是不是闰年*/
leap = 1;
else
leap = 0;
if (leap == 1 && month > 2)/*如果是闰年且月份大于2,总天数应该加一天*/
{
sum++;
Console.WriteLine("一共有{0}天", sum);
}
else
{ Console.WriteLine("一共有{0}天", sum); }
Console.ReadLine();
}
}
}
输入三个整数x,y,z,请把这三个数由小到大输出。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int x, y, z, t;
Console.WriteLine("请输入x的值");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入y的值");
y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入z的值");
z = Convert.ToInt32(Console.ReadLine());
if (x > y)
{ t = x; x = y; y = t; } /*交换x,y的值*/
if (x > z)
{ t = z; z = x; x = t; }/*交换x,z的值*/
if (y > z)
{ t = y; y = z; z = t; }/*交换z,y的值*/
Console.WriteLine("由小到大输出为{0} {1} {2}", x, y, z);
Console.ReadLine();
}
}
}
输出9*9口诀。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i, j, result;
for (i = 1; i < 10; i++)
{
for (j = 1; j < 10; j++)
{
result = i * j;
Console.WriteLine("{0}*{1}={2} ", i, j, result);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
要求输出国际象棋棋盘。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
if ((i + j) % 2 == 0)
Console.Write("黑 ");
else
Console.Write("白 ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
long f1, f2;
f1 = f2 = 1;
for (int i = 1; i <= 20; i++)
{
Console.WriteLine("{0} {1}",f1,f2);
f1 = f1 + f2; /*前两个月加起来赋值给第三个月*/
f2 = f1 + f2; /*前两个月加起来赋值给第三个月*/
}
Console.WriteLine();
Console.ReadLine();
}
}
}