C#程序解读
2016-03-21 21:04
337 查看
阅读下面程序,请回答如下问题:
问题1:这个程序要找的是符合什么条件的数?
问题2:这样的数存在么?符合这一条件的最小的数是什么?
问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分钟(电脑:单核CPU 4.0G Hz,内存和硬盘等资源充足)。
问题4:在多核电脑上如何提高这一程序的运行效率?
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:这个程序要找的数符合的条件是只能整除数组rg内任意两个相邻的数,其他数都不能整除。
问题2:肯定存在,不过电脑运行了好久都没出来,但看百度上说是2123581660200。
问题3:运行了好久都没出来,我还以为是哪里死循环了。
问题4:添加多线程同时求解
问题1:这个程序要找的是符合什么条件的数?
问题2:这样的数存在么?符合这一条件的最小的数是什么?
问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分钟(电脑:单核CPU 4.0G Hz,内存和硬盘等资源充足)。
问题4:在多核电脑上如何提高这一程序的运行效率?
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:这个程序要找的数符合的条件是只能整除数组rg内任意两个相邻的数,其他数都不能整除。
问题2:肯定存在,不过电脑运行了好久都没出来,但看百度上说是2123581660200。
问题3:运行了好久都没出来,我还以为是哪里死循环了。
问题4:添加多线程同时求解
相关文章推荐
- 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#]
- 《CLR Via C#》使用CSC.exe将module组合成assembly
- VS2013的安装与C#进行简单单元测试(英文版教程)