读C#程序
2016-03-21 21:45
441 查看
阅读下面程序,请回答如下问题:
问题1:这个程序要找的是符合什么条件的数?
问题2:这样的数存在么?符合这一条件的最小的数是什么?
问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分钟(电脑:单核CPU 4.0G Hz,内存和硬盘等资源充足)。
问题4:在多核电脑上如何提高这一程序的运行效率?
(注:该程序、用C#语言编写,但是只要有C语言基础完全没有阅读压力,如果对部分语句不懂请自行查询)
要求:将上述问题结果写到博客上。
using System;
using System.Collections.Generic;
using System.Text;
namespace FindTheNumber
{
class Program
{
static void Main(string[] args)
{
int [] rg =
{2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,28,29,30,31};
for (Int64 i = 1; i < Int64.MaxValue; i++)
{
int hit = 0;
int hit1 = -1;
int hit2 = -1;
for (int j = 0; (j < rg.Length) && (hit <=2) ; j++)
{
if ((i % rg[j]) != 0)
{
hit++;
if (hit == 1)
{
hit1 = j;
}
else if (hit == 2)
{
hit2 = j;
}
else
break;
}
}
if ((hit == 2)&& (hit1+1==hit2))
{
Console.WriteLine("found {0}", i);
}
}
}
}
}
问题1:这个程序是要找1到Int64.MaxValue即9223372036854775807内, 不能被rg[]数组中相邻的两个数整除,但可以被其它28个数整除的数;
问题2:存在。我按照对问题1的理解,用java语言写了一个程序,从这30个数中取出连续两个数a,b。剩下的数求最小公倍数num_min,然后num_min对a、b分别取余不得0则输出;程序如下:
输出结果为
所以,最小的数为2123581660200。
问题3、4:不太会.正在查找资料学习中....
问题1:这个程序要找的是符合什么条件的数?
问题2:这样的数存在么?符合这一条件的最小的数是什么?
问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分钟(电脑:单核CPU 4.0G Hz,内存和硬盘等资源充足)。
问题4:在多核电脑上如何提高这一程序的运行效率?
(注:该程序、用C#语言编写,但是只要有C语言基础完全没有阅读压力,如果对部分语句不懂请自行查询)
要求:将上述问题结果写到博客上。
using System;
using System.Collections.Generic;
using System.Text;
namespace FindTheNumber
{
class Program
{
static void Main(string[] args)
{
int [] rg =
{2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,28,29,30,31};
for (Int64 i = 1; i < Int64.MaxValue; i++)
{
int hit = 0;
int hit1 = -1;
int hit2 = -1;
for (int j = 0; (j < rg.Length) && (hit <=2) ; j++)
{
if ((i % rg[j]) != 0)
{
hit++;
if (hit == 1)
{
hit1 = j;
}
else if (hit == 2)
{
hit2 = j;
}
else
break;
}
}
if ((hit == 2)&& (hit1+1==hit2))
{
Console.WriteLine("found {0}", i);
}
}
}
}
}
问题1:这个程序是要找1到Int64.MaxValue即9223372036854775807内, 不能被rg[]数组中相邻的两个数整除,但可以被其它28个数整除的数;
问题2:存在。我按照对问题1的理解,用java语言写了一个程序,从这30个数中取出连续两个数a,b。剩下的数求最小公倍数num_min,然后num_min对a、b分别取余不得0则输出;程序如下:
package test; public class ccj { public static void main(String[] args) { int a[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, 20,21,22,23,24,25,26,27,28,29,30,31}; int b[] = new int[28]; /* * 从头至尾一次从a数组中取两个连续的数后,将a数组保存到b数组 */ for(int m =2;m<31;m++){ int n=m+1; int j = 0; for(int i =0; i<a.length;i++){ if(a[i] == m || a[i] == n){ i++; } else { b[j] = a[i]; j++; } } long num_min = b[0];//记录b数组中所有数的最小公倍数 for(int p = 0;p<b.length-1;p++){ num_min = minmultiple(num_min, b[p+1]); } /* * 判断最小公倍数能否整除所取的两个数,若不能则输出 */ if(num_min%m != 0&& num_min%n != 0){ System.out.println("-->"+num_min); } } } /** * 求两个数最大公约数方法 */ public static long maxdivisor(long a,long b){ if(a>=b){ a= a%b; if(a != 0){ return maxdivisor(a,b); } else return b; } else { b = b%a; if(b != 0){ return maxdivisor(a,b); } else return a; } } /** * 求两个数最小公倍数方法 */ public static long minmultiple(long a, long b){ long num = maxdivisor(a, b); return a*b/num; } }
输出结果为
所以,最小的数为2123581660200。
问题3、4:不太会.正在查找资料学习中....
相关文章推荐
- c#程序阅读分析
- C#程序解读
- c#开发中遇到System.AccessViolationException
- C# 面向对象:封装、继承、多态
- C# 版本 支付
- C# 事件与委托详解
- C#解leetcode 11. Container With Most Water
- 读C#程序
- 2013 C#单元测试
- 机房收费系统遇到的问题(四) 几点一定要注意的
- C#代码分析--阅读程序,回答问题
- C#中打印出当前堆栈
- c#
- c# 实现IComparable、IComparer接口、Comparer类的详解
- C#:Socket通信
- C# firebird嵌入式断开连接无法删除文件
- C# 异步编程
- 阅读c#程序——回答问题
- 如何利用MFC编写的.exe调用C#编写的dll动态库
- 如何对DataTable进行动态分组和动态统计[C#]