Algorithm 数列生成算法
我不知道如何解决这个问题。。。我尝试了很多事情,似乎不应该那么困难,但我没有做到 是否可以创建一个函数“series(_x)”,以生成以下内容:Algorithm 数列生成算法,algorithm,sorting,Algorithm,Sorting,我不知道如何解决这个问题。。。我尝试了很多事情,似乎不应该那么困难,但我没有做到 是否可以创建一个函数“series(_x)”,以生成以下内容: 例如,函数应该是myfunction(11)=>211此提示应该可以帮助您。。。它不是二进制的,但很接近。如果你需要进一步的帮助,请告诉我 0 -> - -> - 1 -> - -> - 10 -> 0 -> 1 11
例如,函数应该是myfunction(11)=>211此提示应该可以帮助您。。。它不是二进制的,但很接近。如果你需要进一步的帮助,请告诉我
0 -> - -> -
1 -> - -> -
10 -> 0 -> 1
11 -> 1 -> 2
100 -> 00 -> 11
101 -> 01 -> 12
110 -> 10 -> 21
111 -> 11 -> 22
1000 -> 000 -> 111
1001 -> 001 -> 112
1010 -> 010 -> 121
1011 -> 011 -> 122
1100 -> 100 -> 211
1101 -> 101 -> 212
1110 -> 110 -> 221
1111 -> 111 -> 222
编辑:我不喜欢我排序列的方式,所以我交换了2和3
Python方法
我们需要做的第一件事是生成二进制字符串在Python中,这可以通过
bin(number)
但是,这将以
0b101
通过告诉python我们不想要前两个字符,但我们想要其余的字符,我们可以很容易地从一开始就去掉0b。代码是:
bin(number)[2::
左边:表示开始两个空格,因为右边是空的,所以转到结尾
现在我们有了二进制数,但我们需要去掉第一个数。幸运的是,我们已经知道如何去掉前导字符,所以将该行改为bin(number)[3:://code>
现在要做的就是在数字中的每个位置加上一个。要做到这一点,让我们创建一个新字符串,并将另一个字符串中的每个字符在递增1后添加到该字符串中
#我们已经有了这个
binary=bin(用户在+1中)[3:]
new=“”
对于二进制字符:
#将字符+1添加到字符串中
new+=str(int(char)+1)
我们完成了。该代码段将从十进制转换为该系统的任何形式。您可能会注意到,这个解决方案将被1抵消(2将是1,3将是2)。我们可以通过在开始之前向用户输入添加一个来解决这个问题
最后的代码有一些方便(while循环和print语句)
为True时:
用户_in=int(输入(“输入号码:”)
binary=bin(用户在+1中)[3:]
new=“”
对于二进制字符:
new+=str(int(char)+1)
打印(用户\u位于“\t->\t”,二进制“\t->\t”,新)
在VB.NET中,以base-3和OEIS公式方式显示计数,无需尝试优化:
模块1
函数OEIS_A007931(n为整数)为整数
“从https://oeis.org/A007931
Dim m=数学楼层(数学日志(n+1)/数学日志(2))
尺寸x=0
对于j=0到m-1
尺寸b=数学楼层((n+1-2^m)/(2^j))
x+=CInt((1+b模式2)*10^j)
下一个
返回x
端函数
函数ToBase3(n为整数)为字符串
Dim s=“”
当n>0时
s=(n Mod 3.ToString()&s
n\=3
结束时
返回s
端函数
函数SkipZeros(n为整数)为字符串
尺寸i=0
Dim num=1
Dim s=“”
而我
s=ToBase3(个)
如果s.IndexOf(“0”c)=-1,则
i+=1
如果结束
num+=1
结束时
返回s
端函数
副标题()
Console.WriteLine(“A007931 Base3迭代”)
对于i=1到22
控制台写入线(OEIS_A007931(i).ToString().PadLeft(7)和SkipZeros(i).PadLeft(7)和i.ToString().PadLeft(11))
下一个
Console.ReadLine()
端接头
端模块
产出:
A007931 Base3 ITERATION
1 1 1
2 2 2
11 11 3
12 12 4
21 21 5
22 22 6
111 111 7
112 112 8
121 121 9
122 122 10
211 211 11
212 212 12
221 221 13
222 222 14
1111 1111 15
1112 1112 16
1121 1121 17
1122 1122 18
1211 1211 19
1212 1212 20
1221 1221 21
1222 1222 22
据
我们应该执行3
步骤:
将值+1
转换为基2
删除第一个1
将1
添加到其余数字
例如,对于11
我们有
将11+1==12
转换为二进制:1100
删除第一个1
:100
将1
添加到其余数字:211
因此11
具有211
表示
C#代码:
private static String MyCode(int value) =>
string.Concat(Convert
.ToString(value + 1, 2) // To Binary
.Skip(1) // Skip (Remove) 1st 1
.Select(c => (char)(c + 1))); // Add 1 to the rest digits
var result = Enumerable
.Range(1, 22)
.Select(value => $"{MyCode(value),4} : {value,2}");
Console.Write(string.Join(Emvironment.NewLine, result));
1 : 1
2 : 2
11 : 3
12 : 4
21 : 5
22 : 6
111 : 7
112 : 8
121 : 9
122 : 10
211 : 11
212 : 12
221 : 13
222 : 14
1111 : 15
1112 : 16
1121 : 17
1122 : 18
1211 : 19
1212 : 20
1221 : 21
1222 : 22
public class Solution {
public static void main(String[] args) {
List<String> ans = solve(10);
for(int i=0;i<ans.size();++i) System.out.println(ans.get(i));
}
private static List<String> solve(int terms){
List<String> ans = new ArrayList<>();
String[] digits = new String[]{"1","2"};
ans.add("1");
if(terms == 1) return ans;
ans.add("2");
if(terms == 2) return ans;
List<String> final_result = new ArrayList<>();
final_result.addAll(ans);
terms -= 2;//since 2 numbers are already added
while(terms > 0){
List<String> temp = new ArrayList<>();
for(String s : digits){
for(int j=0;j<ans.size() && terms > 0;++j){
temp.add(s + ans.get(j));
terms--;
}
}
ans = temp;
final_result.addAll(ans);
}
return final_result;
}
}
演示:
private static String MyCode(int value) =>
string.Concat(Convert
.ToString(value + 1, 2) // To Binary
.Skip(1) // Skip (Remove) 1st 1
.Select(c => (char)(c + 1))); // Add 1 to the rest digits
var result = Enumerable
.Range(1, 22)
.Select(value => $"{MyCode(value),4} : {value,2}");
Console.Write(string.Join(Emvironment.NewLine, result));
1 : 1
2 : 2
11 : 3
12 : 4
21 : 5
22 : 6
111 : 7
112 : 8
121 : 9
122 : 10
211 : 11
212 : 12
221 : 13
222 : 14
1111 : 15
1112 : 16
1121 : 17
1122 : 18
1211 : 19
1212 : 20
1221 : 21
1222 : 22
public class Solution {
public static void main(String[] args) {
List<String> ans = solve(10);
for(int i=0;i<ans.size();++i) System.out.println(ans.get(i));
}
private static List<String> solve(int terms){
List<String> ans = new ArrayList<>();
String[] digits = new String[]{"1","2"};
ans.add("1");
if(terms == 1) return ans;
ans.add("2");
if(terms == 2) return ans;
List<String> final_result = new ArrayList<>();
final_result.addAll(ans);
terms -= 2;//since 2 numbers are already added
while(terms > 0){
List<String> temp = new ArrayList<>();
for(String s : digits){
for(int j=0;j<ans.size() && terms > 0;++j){
temp.add(s + ans.get(j));
terms--;
}
}
ans = temp;
final_result.addAll(ans);
}
return final_result;
}
}
结果:
private static String MyCode(int value) =>
string.Concat(Convert
.ToString(value + 1, 2) // To Binary
.Skip(1) // Skip (Remove) 1st 1
.Select(c => (char)(c + 1))); // Add 1 to the rest digits
var result = Enumerable
.Range(1, 22)
.Select(value => $"{MyCode(value),4} : {value,2}");
Console.Write(string.Join(Emvironment.NewLine, result));
1 : 1
2 : 2
11 : 3
12 : 4
21 : 5
22 : 6
111 : 7
112 : 8
121 : 9
122 : 10
211 : 11
212 : 12
221 : 13
222 : 14
1111 : 15
1112 : 16
1121 : 17
1122 : 18
1211 : 19
1212 : 20
1221 : 21
1222 : 22
public class Solution {
public static void main(String[] args) {
List<String> ans = solve(10);
for(int i=0;i<ans.size();++i) System.out.println(ans.get(i));
}
private static List<String> solve(int terms){
List<String> ans = new ArrayList<>();
String[] digits = new String[]{"1","2"};
ans.add("1");
if(terms == 1) return ans;
ans.add("2");
if(terms == 2) return ans;
List<String> final_result = new ArrayList<>();
final_result.addAll(ans);
terms -= 2;//since 2 numbers are already added
while(terms > 0){
List<String> temp = new ArrayList<>();
for(String s : digits){
for(int j=0;j<ans.size() && terms > 0;++j){
temp.add(s + ans.get(j));
terms--;
}
}
ans = temp;
final_result.addAll(ans);
}
return final_result;
}
}
这些术语成为下一个术语的后缀。请参阅下图以获得更清晰的信息。相同颜色的框会重复出现。因此,我们可以继续为以前的结果预先设置1
和2
代码(java):
private static String MyCode(int value) =>
string.Concat(Convert
.ToString(value + 1, 2) // To Binary
.Skip(1) // Skip (Remove) 1st 1
.Select(c => (char)(c + 1))); // Add 1 to the rest digits
var result = Enumerable
.Range(1, 22)
.Select(value => $"{MyCode(value),4} : {value,2}");
Console.Write(string.Join(Emvironment.NewLine, result));
1 : 1
2 : 2
11 : 3
12 : 4
21 : 5
22 : 6
111 : 7
112 : 8
121 : 9
122 : 10
211 : 11
212 : 12
221 : 13
222 : 14
1111 : 15
1112 : 16
1121 : 17
1122 : 18
1211 : 19
1212 : 20
1221 : 21
1222 : 22
public class Solution {
public static void main(String[] args) {
List<String> ans = solve(10);
for(int i=0;i<ans.size();++i) System.out.println(ans.get(i));
}
private static List<String> solve(int terms){
List<String> ans = new ArrayList<>();
String[] digits = new String[]{"1","2"};
ans.add("1");
if(terms == 1) return ans;
ans.add("2");
if(terms == 2) return ans;
List<String> final_result = new ArrayList<>();
final_result.addAll(ans);
terms -= 2;//since 2 numbers are already added
while(terms > 0){
List<String> temp = new ArrayList<>();
for(String s : digits){
for(int j=0;j<ans.size() && terms > 0;++j){
temp.add(s + ans.get(j));
terms--;
}
}
ans = temp;
final_result.addAll(ans);
}
return final_result;
}
}
公共类解决方案{
公共静态void main(字符串[]args){
列表ans=求解(10);
对于(inti=0;i0){
List temp=new ArrayList();
for(字符串s:数字){
对于(int j=0;j 0;++j){
临时添加(s+ans.get(j));
术语--;
}
}
ans=温度;
最终结果。添加所有(ans);
}
返回最终结果;
}
}
到目前为止你尝试了什么?想想对计算机科学非常重要的一些数字系统。数一数这个数字系统,然后再看看你的问题。你知道一个著名的笑话吗,它的开头是这样的:“世界上有10种人……”?它的答案将引导您找到答案(如果@z5h hint还没有)。请记住,这实际上不是二进制的。有趣的是:查询:我需要了解一下这一点,我甚至不知道如何在没有迭代的情况下生成第一列中的数字。。。我希望它是非递归的,或者使用尽可能少的迭代次数…我将在我的python答案中添加更多信息:)@AndriesHeylen看看我的python实现,看看它是否对您有帮助,python确实使类似的尝试变得非常方便,但也可以用其他语言完成(可能需要更多的工作)是的,但这意味着我将有一个dec-to-bin库供我使用。。。事实并非如此:)@AndriesHeylen有很多这样的例子,请检查以实现其中一种二进制转换方法。然而,您的问题不是关于二进制转换的,您也没有指出一种编程方法