初始化数组的成员没有循环

时间:2016-12-02 11:10:16

标签: c# algorithm

我有一个.NET C#求职面试,其中一个问题是"初始化整数数组,其值为1,2,3 ... 100没有循环,没有递归,没有初始化,如

int[] arr = new [] {1, 2, 3, ...100};

有可能吗?

3 个答案:

答案 0 :(得分:10)

一衬垫:

Enumerable.Range(1,100).ToArray();

答案 1 :(得分:1)

C ++解决方案(对不起,从来没有和C#一起上床) - 其他一些操作的副作用。

SELECT COUNT(1) AS clicks 
FROM clicks AS c 
LEFT JOIN links AS l ON l.id = c.link_id 
AND (c.date_added >= '2016-11-01 00:00:00' 
AND c.date_added <= '2016-11-16 23:59:59');

答案 2 :(得分:0)

C#解决方案 - 不是循环,没有递归,不是初始化,这是一个很好的休息方法...使用框架/操作系统提供的事件排队 - 当然在实践中永远不会使用这样的东西<强>但是它遵守了对这封信的要求(我稍后会谈到精神问题)。此外,可以移植许多语言,包括javascript(请参阅setinterval)。

现在,请原谅我,我需要卸载Mono(并采取一两个强大的精神来克服创伤):

using System;
using System.Timers;
using System.Threading;

namespace foo
{
  class MainClass
  {
    public static void Main (string[] args)
    {
      int[] a=new int[100];
      a [99] = 0;
      int count = 0;
      System.Timers.Timer tmr = new System.Timers.Timer();
      tmr.Interval = 36000; // so that we can have a beer by the time we have our array
      tmr.Elapsed += async ( sender, e ) => 
        { if(count<a.Length) a[count]=count++; }
      ;
      tmr.Start ();
      while (a [99] < 99) {
        Thread.Sleep (10);
      }
      foreach(int i in a) {
        Console.WriteLine (i);
      }

    }
  }
}