c# 经典521例(5)

将一个正整数分解质因数

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.Write(
"请输入一个数字");
int n= Convert.ToInt32(Console.ReadLine());
for (int i = 2; i <= n; i++)
{

while (n != 1)
{
if (n % i == 0)
{
Console.WriteLine(i);
n
= n / i;
}
else
{
break; }
}
}
Console.ReadLine();
}

}
}

输入两个正整数m和n,(利用辗除法)求其最大公约数和最小公倍数。

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.Write(
"请输入第一个数字");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write(
"请输入第二个数字");
int num2 = Convert.ToInt32(Console.ReadLine());
int a, b, temp, r;
if (num1 < num2)
{
temp
= num1;
num1
= num2;
num2
= temp;
}
a
= num1; b = num2;
while (b != 0)//利用辗除法,直到b为0为止
{
r
= a % b;
a
= b;
b
= r;
}
Console.WriteLine(
"最大公约数为{0}",a);
Console.WriteLine(
"最小公倍数为{0}", num1 * num2 / a);
Console.ReadLine();
}
}
}


打印出如下图案(菱形)

        *

      *** 

   ******

  ********

   ****** 

     *** 

       *

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i, j, k;
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 2 - i; j++)
Console.Write(
" ");
for (k = 0; k <= 2 * i; k++)
Console.Write(
"*");
Console.Write(
"\n");
}
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= i; j++)
Console.Write(
" ");
for (k = 0; k <= 4 - 2 * i; k++)
Console.Write(
"*");
Console.Write(
"\n");
}
Console.ReadLine();
}
}
}


一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int ge, shi, qian, wan;
Console.Write(
"请输入一个五位数字");
int x = Convert.ToInt32(Console.ReadLine());
wan
= x / 10000;
qian
= x % 10000 / 1000;
shi
= x % 100 / 10;
ge
= x % 10;
if (ge == wan && shi == qian)/*个位等于万位并且十位等于千位*/
Console.WriteLine(
"是回文数");
else
Console.WriteLine(
"不是回文数");
Console.ReadLine();
}
}
}

求X的N次乘方 

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int t = 1;
Console.WriteLine(
"请输入一个五位数字 ");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(
"请输入次方数 ");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
t
= t * x;
}
Console.WriteLine(
"{0}的{1}次是:{2}", x, n, t);
Console.ReadLine();
}
}
}

posted @ 2011-06-22 07:53  伟伟LOVE齐齐  阅读(227)  评论(0编辑  收藏  举报