繁体   English   中英

如何生成这种数字模式?

[英]How can I generate this pattern of numbers?

给定输入1-32如何生成以下输出?

进出

  1. 1
  2. 1
  3. 1
  4. 1
  5. 2
  6. 2
  7. 2
  8. 2
  9. 1
  10. 1
  11. 1
  12. 1
  13. 2
  14. 2
  15. 2
  16. 2 ......

编辑不是家庭作业..只是缺乏睡眠。

我在C#工作,但我一直在寻找一种与语言无关的算法。

编辑2提供更多背景...我有一个包含32个项目的数组,代表一个二维棋盘。 我需要此算法的最后一部分在矢量和图形之间进行转换,其中索引在棋盘上的黑色方块上对齐。

最终守则:

 --Index;
 int row = Index >> 2;
 int col = 2 * Index - (((Index & 0x04) >> 2 == 1) ? 2 : 1);

假设您可以使用按位运算符,您可以检查具有相同输出的数字的共同点,在这种情况下,我更喜欢使用输入0-31,因为它更简单(您只需将1减去实际值)

你有什么?

0x0000 -> 1
0x0001 -> 1
0x0010 -> 1
0x0011 -> 1
0x0100 -> 2
0x0101 -> 2
0x0110 -> 2
0x0111 -> 2
0x1000 -> 1
0x1001 -> 1
0x1010 -> 1
0x1011 -> 1
0x1100 -> 2
...

如果你注意到当输出应该是1时第三位总是0 ,那么很容易,反之,当输出应该是2时,它总是1

所以:

char codify(char input)
{
     return ((((input-1)&0x04)>>2 == 1)?(2):(1));
}

编辑

正如评论所建议的,它也应该起作用

char codify(char input)
{
     return ((input-1 & 0x04)?(2):(1));
}

因为在某些语言(如C)中, 0将评估为false ,其他任何值都为true 我不确定它是否也适用于C#,因为我从未用该语言编程。 当然,这不是一个与语言无关的答案,但它更优雅!

在C:

char output = "11112222"[input-1 & 7];

要么

char output = (input-1 >> 2 & 1) + '1';

或者在了解FogleBird之后:

char output = input - 1 & 4 ? '2' : '1';

或者在Steve Jessop的想法之后:

char output = '2' - (0x1e1e1e1e >> input & 1);

要么

char output = "12"[input-1>>2&1];

C运算符优先级是邪恶的。 使用我的代码作为坏例子:-)

你可以使用整数除法和模2(偶数奇数)的组合:有四个块,第一个,第三个,第五个块等依次产生1,第2个,第4个,第6个,依此类推2 。

s := ((n-1) div 4) mod 2;
return s + 1;

div应该是整数除法。

编辑:当然,将第一个mod转换为div

只是为了笑,这是一种技术,可以在编译时以任意方式将输入1..32映射到两个可能的输出:

// binary 1111 0000 1111 0000 1111 0000 1111 0000
const uint32_t lu_table = 0xF0F0F0F0;

// select 1 bit out of the table
if (((1 << (input-1)) & lu_table) == 0) {
    return 1;
} else {
    return 2;
}

通过更改常量,您可以处理所需的任何输出模式。 显然在你的情况下有一种模式,这意味着它可以更快地完成(因为不需要转换),但其他人已经做到了。 此外,查找表更常见的是数组,但这不是必需的。

蟒蛇

def f(x):
    return int((x - 1) % 8 > 3) + 1

要么:

def f(x):
    return 2 if (x - 1) & 4 else 1

要么:

def f(x):
    return (((x - 1) & 4) >> 2) + 1

接受的答案return ((((input-1)&0x04)>>2 == 1)?(2):(1)); 在我刚写完的时候使用了一个分支:

return 1 + ((input-1) & 0x04 ) >> 2;

在Perl中:

#!/usr/bin/perl

use strict; use warnings;

sub it {
    return sub {
        my ($n) = @_;
        return 1 if 4 > ($n - 1) % 8;
        return 2;
    }
}

my $it = it();

for my $x (1 .. 32) {
    printf "%2d:%d\n", $x, $it->($x);
}

要么:

sub it {
    return sub {
        my ($n) = @_;
        use integer;
        return 1 + ( (($n - 1) / 4) % 2 );
    }
}

在Haskell:

vec2graph :: Int -> Char
vec2graph n = (cycle "11112222") !! (n-1)

这取决于您使用的语言。

在VB.NET中,你可以这样做:

for i as integer = 1 to 32
   dim intAnswer as integer = 1 + (Math.Floor((i-1) / 4) mod 2)
   ' Do whatever you need to do with it
next

这可能听起来很复杂,但这只是因为我把它放到一个单一的线上。

这很简单:

if (input == "1") {Console.WriteLine(1)};
if (input == "2") {Console.WriteLine(1)};
if (input == "3") {Console.WriteLine(1)};
if (input == "4") {Console.WriteLine(1)};
if (input == "5") {Console.WriteLine(2)};
if (input == "6") {Console.WriteLine(2)};
if (input == "7") {Console.WriteLine(2)};
if (input == "8") {Console.WriteLine(2)};

等等...

HTH

在Groovy中:

def codify = { i  ->
    return  (((((i-1)/4).intValue()) %2 ) + 1)
}

然后:

def list = 1..16
list.each {
    println "${it}: ${codify(it)}"
}
char codify(char input)
{
     return  (((input-1) & 0x04)>>2) + 1;
}

使用Python:

output = 1
for i in range(1, 32+1):
  print "%d. %d" % (i, output)
  if i % 4 == 0:
    output = output == 1 and 2 or 1

JavaScript的

我的第一个想法是

output = ((input - 1 & 4) >> 2) + 1;

但drhirsch的代码在JavaScript中运行良好:

output = input - 1 & 4 ? 2 : 1;

和荒谬的(与FogleBird的答案有关):

output = -~((input - 1) % 8 > 3);

Java,使用模运算('%')给出循环行为(0,1,2 ... 7),然后根据返回值将三元组“循环”为1(?)或2(:)。 ...

 public static void main(String[] args) {
        for (int i=1;i<=32;i++) {
             System.out.println(i+"="+ (i%8<4?1:2) );
    }

生产:

1 = 1 2 = 1 3 = 1 4 = 2 5 = 2 6 = 2 7 = 2 8 = 1 9 = 1 10 = 1 11 = 1 12 = 2 13 = 2 14 = 2 15 = 2 16 = 1 17 = 1 18 = 1 19 = 1 20 = 2 21 = 2 22 = 2 23 = 2 24 = 1 25 = 1 26 = 1 27 = 1 28 = 2 29 = 2 30 = 2 31 = 2 32 = 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM