[英]How can I generate this pattern of numbers?
給定輸入1-32如何生成以下輸出?
進出
編輯不是家庭作業..只是缺乏睡眠。
我在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.