【发布时间】:2011-02-16 01:47:35
【问题描述】:
速度算法解决以下问题最有效的方法是什么?
给定 6 个数组,D1、D2、D3、D4、D5 和 D6,每个数组包含 6 个数字,例如:
D1[0] = number D2[0] = number ...... D6[0] = number
D1[1] = another number D2[1] = another number ....
..... .... ...... ....
D1[5] = yet another number .... ...... ....
给定第二个数组 ST1,包含 1 个数字:
ST1[0] = 6
给定第三个数组 ans,包含 6 个数字:
ans[0] = 3, ans[1] = 4, ans[2] = 5, ......ans[5] = 8
使用数组 D1,D2,D3,D4,D5 和 D6 的索引,从 0 到存储在 ST1[0] 中的数字减一,在本例中为 6,因此从 0 到 6 -1,将 ans 数组与每个 D 数组进行比较。如果在同一索引的任何 D 中都没有找到一个或多个 ans 数字,则结果应为 0,如果在同一索引的某个 D 中找到所有 ans 数字,则结果应为 1。也就是说,如果某个 ans[i] 不等于任何 DN[i] 则返回 0,如果每个 ans[i] 等于某个 DN[i 则返回 1 ]。
到目前为止我的算法是:
我试图让所有内容尽可能不循环。
EML := ST1[0] //number contained in ST1[0]
EML1 := 0 //start index for the arrays D
While EML1 < EML
if D1[ELM1] = ans[0]
goto two
if D2[ELM1] = ans[0]
goto two
if D3[ELM1] = ans[0]
goto two
if D4[ELM1] = ans[0]
goto two
if D5[ELM1] = ans[0]
goto two
if D6[ELM1] = ans[0]
goto two
ELM1 = ELM1 + 1
return 0 //If the ans[0] number is not found in either D1[0-6], D2[0-6].... D6[0-6] return 0 which will then exclude ans[0-6] numbers
two:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[1]
goto three
if D2[ELM1] = ans[1]
goto three
if D3[ELM1] = ans[1]
goto three
if D4[ELM1] = ans[1]
goto three
if D5[ELM1] = ans[1]
goto three
if D6[ELM1] = ans[1]
goto three
ELM1 = ELM1 + 1
return 0 //If the ans[1] number is not found in either D1[0-6], D2[0-6].... D6[0-6] return 0 which will then exclude ans[0-6] numbers
three:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[2]
goto four
if D2[ELM1] = ans[2]
goto four
if D3[ELM1] = ans[2]
goto four
if D4[ELM1] = ans[2]
goto four
if D5[ELM1] = ans[2]
goto four
if D6[ELM1] = ans[2]
goto four
ELM1 = ELM1 + 1
return 0 //If the ans[2] number is not found in either D1[0-6], D2[0-6].... D6[0-6] return 0 which will then exclude ans[0-6] numbers
four:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[3]
goto five
if D2[ELM1] = ans[3]
goto five
if D3[ELM1] = ans[3]
goto five
if D4[ELM1] = ans[3]
goto five
if D5[ELM1] = ans[3]
goto five
if D6[ELM1] = ans[3]
goto five
ELM1 = ELM1 + 1
return 0 //If the ans[3] number is not found in either D1[0-6], D2[0-6].... D6[0-6] return 0 which will then exclude ans[0-6] numbers
five:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[4]
goto six
if D2[ELM1] = ans[4]
goto six
if D3[ELM1] = ans[4]
goto six
if D4[ELM1] = ans[4]
goto six
if D5[ELM1] = ans[4]
goto six
if D6[ELM1] = ans[4]
goto six
ELM1 = ELM1 + 1
return 0 //If the ans[4] number is not found in either D1[0-6], D2[0-6].... D6[0-6] return 0 which will then exclude ans[0-6] numbers
six:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[5]
return 1 ////If the ans[1] number is not found in either D1[0-6].....
if D2[ELM1] = ans[5] return 1 which will then include ans[0-6] numbers
return 1
if D3[ELM1] = ans[5]
return 1
if D4[ELM1] = ans[5]
return 1
if D5[ELM1] = ans[5]
return 1
if D6[ELM1] = ans[5]
return 1
ELM1 = ELM1 + 1
return 0
作为首选语言,纯 c
【问题讨论】:
-
我认为您的编程技能非常基础。很可能你想做的事情可以更容易地完成。请详细说明你想用这段代码做什么(数组代表什么以及你想从中提取哪些信息),这可能会澄清事情并带来更多答案。
-
哦,伙计们。对于第一次使用的用户,他显然付出了很多努力来尝试尽可能好地格式化和表达他的问题。 +1
-
同意 Lieven... 即使对于初学者,我们也不希望任何人对提出问题感到不舒服,尤其是一个合法的问题,即使是教育/学习。如果没有与现实世界的开发人员建立联系,其他人如何成为更强大的开发人员。
-
@mark:我想为 Stackoverflow 的警察部门的方式道歉。已经对待你了。
-
您能解释一下您还想要什么吗?在您给出的算法中,我认为只有前两个循环可以运行,因为在所有循环中,循环将结束并且代码将返回,或者循环将达到
goto two并将转到第二个一。另外,当您说“将每个 res 数组与每个 D 数组进行比较”时,程序应该如何处理比较?您是要打印一系列字符串“大于”、“小于”等,还是要在遇到相等的数字时退出,或其他?
标签: c algorithm optimization cuda