首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线程间的互通

线程间的互通
EN

Code Golf用户
提问于 2019-05-02 11:08:32
回答 3查看 496关注 0票数 3

很高兴我找到了这个很酷的地方。想试着分享一个朋友给我的编码挑战,一个面试问题。它通常是用java编写的,但是你可以使用任何你想要的语言,只是玩得开心而已!

挑战:高效和安全地编写一个程序,该程序创建一个n个线程数组,这些线程彼此通信,为了简单起见,每个线程代表一个介于1,...,100之间的数字。它必须被随机初始化。

下面是有趣的部分:每个线程都查看它的邻居(i-1,i+1),并且:

  • 如果它的数目小于他的两个邻居的数目(I-1,i+1),它就会自己增加。
  • 如果它的数目大于他的两个邻居的数目,它就会自己减少。
  • 如果它的数目相等或它的邻居之间的数目相等,它就保持这个数字不变。

到目前为止还不好玩,对吧?所以,让我们也把它变成一个循环数组!(数组中的第一个元素和数组中的最后一个元素是相邻的)棘手的部分是:每个线程在其邻居完成检查之前不应该更改其编号。

该程序执行多轮检查。

输入:对于程序逻辑,您可以从用户那里获取输入,或者定义它,这并不重要。

输出:第一行应该打印数组的起始状态。第二行应在每个线程进行一次检查后打印数组中的所有数字。第三行应该在每个线程进行第二次测试(第二轮)之后,在所有数组中打印数字,等等。

一开始可能会很棘手,但要玩得开心!获胜者将是谁谁可以写最短的代码,如果一个平手,冷静的算法技巧或函数使用的java语言将被授予额外的分数!

第一篇帖子在这里,将尝试贴出更酷的东西给你欣赏!真的很喜欢这个网站。

我在一段时间前发布了它,但在国外时忘记了我的证书,所以我无法登录到该网站并修复上一篇文章。希望这一个是根据网站的标准和更多,我真的希望你会喜欢这个酷的编码游戏练习。

祝大家好运,最重要的是-好好享受吧!

正如凯文说得好:不要让代码-高尔夫语言阻止你用非合作语言发布答案。试着为“任意”编程语言提供一个尽可能短的答案。

EN

回答 3

Code Golf用户

发布于 2019-05-02 12:05:09

05AB1E,35 字节数

代码语言:javascript
复制
FтLΩˆ}>µ¯©=´vy®N<N>‚èy.SDPiн+ë\}ˆ}¼

n作为第一个输入,m作为第二个输入。不使用任何线程,因为05AB1E没有线程。

在网上试试。

解释:

代码语言:javascript
复制
F    }               # Loop the (implicit) input `n` amount of times:
 тL                  #  Push a list in the range [1,100]
   Ω                 #  Pop and push a random value from this list
    ˆ                #  Pop and add it to the global array
>                    # Increase the second (implicit) input `m` by 1
 µ                   # Loop until the counter_variable is equal to that:
  ¯                  #  Push the global array
   ©                 #  Store it in the register (without popping)
    =                #  Print it with trailing newline (without popping)
     ´               #  And clear the global array
   v                 #  Loop over each item of the array that is still on the stack:
      N<             #   Get the index-1
        N>           #   And the index+1
          ‚          #   Pair them together
     ®     è         #   Index both into the previous array we stored in the register
                     #   (NOTE: 05AB1E has automatic wrap-around when indexing)
            y.S      #   Compare both of them to the current integer `y`
                     #   (-1 if `y` is larger; 1 if `y` is smaller; 0 if equal)
               D     #   Duplicate this pair
                Pi   #   If the product is 1 (only true for [1,1] and [-1,-1]):
                  н  #    Only leave the first element of the duplicated pair
    y              + #    And add it to the current integer `y`
                 ë   #   Else:
                  \  #    Discard the duplicated pair, so `y` is at the top of the stack
                 }ˆ  #   After the if-else: add it to the global array
   }¼                #  After the inner loop: increase the counter_variable by 1
票数 4
EN

Code Golf用户

发布于 2019-05-02 13:19:14

Javascript,209个字节

代码语言:javascript
复制
for(a=[],l=n=prompt();n;)a[--n]=(M=Math).floor(M.random()*100+1);for(console.log(a),m=prompt();--m;)b=a.slice(),b.forEach((e,i)=>b[i]+=e<M.min(x=b[(i-1+l)%l],y=b[(i+1)%l])?1:e>M.min(x,y)?-1:0),console.log(a=b)

此程序在运行时将提示用户两次。第一个用于n(线程数量,而是使用数组),第二个用于m(检查数量)。

票数 2
EN

Code Golf用户

发布于 2019-05-02 16:52:59

C# (可视化C#交互式编译器),198个字节

代码语言:javascript
复制
n=>m=>{int[]z=new int[n],g;int i=n,a;for(var k=new Random();i>0;z[--i]=k.Next(1,101));for(;m-->0;z=g){g=z.ToArray();i=0;for(Print(z);i<n;g[i]+=(a=z[(n+~i)%n]-z[i])*(z[-~i%n]-z[i++])>0?a<0?-1:1:0);}}

在网上试试!

票数 2
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/185056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档