算法入门题
来源:互联网 发布:windows 行为监控软件 编辑:程序博客网 时间:2024/05/16 06:18
1。在网上看到了一个关于最小公倍数和最大公约数的题目,而且网友讨论的挺热烈的,
自己也把它做了一遍,和其他人的代码不一样,按照惯例,正确调试过了。
题目:
求两个自然数,其和是667,最小公倍数与最大公约数之比是120:1。
我的代码:
class Class1
{
//求两个数的最小公倍数
public int beisu(int m, int n)
{
int t = m * n/yuesu (m,n );;
return t;
}
//求两个数最大公约数
public int yuesu(int m, int n)
{
int s = m % n;
if (s == 0)
{
return n;
}
else
return yuesu(n, s);
}
}
//求两个自然数,其和是667,最小公倍数与最大公约数之比是120:1。(例如:115,552)
static void Main(string[] args)
{
Class1 ss = new Class1();
for (int i = 666; i >0; i--)
{
if (i > 667 - i)
{
if (ss.beisu(i, 667 - i) / ss.yuesu(i, 667 - i) == 120)
Console.Write("{0},{1},", i, 667 - i);
}
}
2。 对由从界面输入的任意整数进行因式分解:
static void Main(string[] args)
{
int n;
n =Convert .ToInt32( Console.ReadLine());
Double nsq = (double)Math .Sqrt (n);
while (n % 2 == 0)
{
Console.WriteLine("{0}",2);
n /= 2;
}
for (int t = 3; t <= nsq ; )
{
if (n % t == 0)
{
Console.WriteLine("{0}", t);
n /= t;
}
else t += 2;
}
}
整数分解为连续整数之和
static void Main(string[] args)
{
int n =Convert .ToInt32 ( Console.ReadLine());
for(int n1=1;n1 <n ;n1 ++)
{
for (int n2=2;n2 <=n ;n2 ++)
{
if ((n1 +n2 )*(n2 -n1 +1)==n*2)
{
for (int x=n1 ;x <=n2 ;x++)
{
Console .Write ("{0},",x );}
}
}
}
}
}
3。对由从界面输入的任意整数进行因式分解:
static void Main(string[] args)
{
int n;
n =Convert .ToInt32( Console.ReadLine());
Double nsq = (double)Math .Sqrt (n);
while (n % 2 == 0)
{
Console.WriteLine("{0}",2);
n /= 2;
}
for (int t = 3; t <= nsq ; )
{
if (n % t == 0)
{
Console.WriteLine("{0}", t);
n /= t;
}
else t += 2;
}
}
整数分解为连续整数之和
static void Main(string[] args)
{
int n =Convert .ToInt32 ( Console.ReadLine());
for(int n1=1;n1 <n ;n1 ++)
{
for (int n2=2;n2 <=n ;n2 ++)
{
if ((n1 +n2 )*(n2 -n1 +1)==n*2)
{
for (int x=n1 ;x <=n2 ;x++)
{
Console .Write ("{0},",x );}
}
}
}
}
}
}
自己也把它做了一遍,和其他人的代码不一样,按照惯例,正确调试过了。
题目:
求两个自然数,其和是667,最小公倍数与最大公约数之比是120:1。
我的代码:
class Class1
{
//求两个数的最小公倍数
public int beisu(int m, int n)
{
int t = m * n/yuesu (m,n );;
return t;
}
//求两个数最大公约数
public int yuesu(int m, int n)
{
int s = m % n;
if (s == 0)
{
return n;
}
else
return yuesu(n, s);
}
}
//求两个自然数,其和是667,最小公倍数与最大公约数之比是120:1。(例如:115,552)
static void Main(string[] args)
{
Class1 ss = new Class1();
for (int i = 666; i >0; i--)
{
if (i > 667 - i)
{
if (ss.beisu(i, 667 - i) / ss.yuesu(i, 667 - i) == 120)
Console.Write("{0},{1},", i, 667 - i);
}
}
2。 对由从界面输入的任意整数进行因式分解:
static void Main(string[] args)
{
int n;
n =Convert .ToInt32( Console.ReadLine());
Double nsq = (double)Math .Sqrt (n);
while (n % 2 == 0)
{
Console.WriteLine("{0}",2);
n /= 2;
}
for (int t = 3; t <= nsq ; )
{
if (n % t == 0)
{
Console.WriteLine("{0}", t);
n /= t;
}
else t += 2;
}
}
整数分解为连续整数之和
static void Main(string[] args)
{
int n =Convert .ToInt32 ( Console.ReadLine());
for(int n1=1;n1 <n ;n1 ++)
{
for (int n2=2;n2 <=n ;n2 ++)
{
if ((n1 +n2 )*(n2 -n1 +1)==n*2)
{
for (int x=n1 ;x <=n2 ;x++)
{
Console .Write ("{0},",x );}
}
}
}
}
}
3。对由从界面输入的任意整数进行因式分解:
static void Main(string[] args)
{
int n;
n =Convert .ToInt32( Console.ReadLine());
Double nsq = (double)Math .Sqrt (n);
while (n % 2 == 0)
{
Console.WriteLine("{0}",2);
n /= 2;
}
for (int t = 3; t <= nsq ; )
{
if (n % t == 0)
{
Console.WriteLine("{0}", t);
n /= t;
}
else t += 2;
}
}
整数分解为连续整数之和
static void Main(string[] args)
{
int n =Convert .ToInt32 ( Console.ReadLine());
for(int n1=1;n1 <n ;n1 ++)
{
for (int n2=2;n2 <=n ;n2 ++)
{
if ((n1 +n2 )*(n2 -n1 +1)==n*2)
{
for (int x=n1 ;x <=n2 ;x++)
{
Console .Write ("{0},",x );}
}
}
}
}
}
}
- 算法入门题
- 算法入门题
- 算法入门
- 算法入门
- 算法入门
- 算法入门。。?
- 算法入门
- 算法入门
- 算法入门
- 算法入门
- 算法入门-程序设计入门
- 算法入门5:贪心算法
- 算法入门8:随机算法
- 遗传算法入门
- 遗传算法入门
- 第二章 算法入门
- 算法入门的唠叨
- 算法竞赛入门经典
- input text have to be number or character
- move cursor by key board
- X window 中实现资料的共享和交换
- 最新三维立体画·换个角度看世界
- 约定
- 算法入门题
- TChart控件的BUG?
- 页面(PAGE)标记(TAGS)
- j2me 减少三次握手的时间(减少连结网络的时间)
- 《大规模C++程序设计》读书笔记20
- 提高增量搜索速度-制作可学习词库
- transaction sql
- 基于Visual Studio 2003/2005的Office插件开发FAQ
- IConnectionPoint接口