Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 打印并存储给定数字的不同长度排列_Algorithm_Numbers_Design Patterns_Permutation_Placement - Fatal编程技术网

Algorithm 打印并存储给定数字的不同长度排列

Algorithm 打印并存储给定数字的不同长度排列,algorithm,numbers,design-patterns,permutation,placement,Algorithm,Numbers,Design Patterns,Permutation,Placement,如果我有一个号码2345,那么它会如何打印: 2. 3. 4. 5. 23 24 25 34 35 45 234 235 345 i、 e打印所有可能的数字,直到长度为-1 使用深度优先搜索(dfs)您可以实现这一点,下面是一个示例: #include <cstdio> #include <cstring> char s[10]; char a[10]; void doSomething(char* s){ printf("%s\n", s); } void

如果我有一个号码2345,那么它会如何打印: 2. 3. 4. 5. 23 24 25 34 35 45 234 235 345

i、 e打印所有可能的数字,直到长度为-1

使用深度优先搜索(dfs)您可以实现这一点,下面是一个示例:

#include <cstdio>
#include <cstring>

char s[10];
char a[10];

void doSomething(char* s){
    printf("%s\n", s);
}

void dfs(int x, int y){
    if (!s[x]){
        a[y] = 0;
        doSomething(a);
        return;
    }
    dfs(x + 1, y);
    a[y] = s[x];
    dfs(x + 1, y + 1);
}

int main(){
    scanf("%s", s);
    dfs(0, 0);
    return 0;
}
#包括
#包括
chars[10];
chara[10];
无效剂量测定法(字符*s){
printf(“%s\n”,s);
}
无效dfs(整数x,整数y){
如果(!s[x]){
a[y]=0;
剂量测定法(a);
返回;
}
dfs(x+1,y);
a[y]=s[x];
dfs(x+1,y+1);
}
int main(){
scanf(“%s”,s);
dfs(0,0);
返回0;
}
#包括
#包括
#包括
整数位计数(长位){
位=(位和0x5555)+(位>>1和0x5555);
位=(位&0x33333333)+(位>>2&0x33333333);
位=(位和0x0F0F)+(位>>4和0x0F0F);
位=(位和0x00ff00ff)+(位>>8和0x00ff00ff);
返回(位和0x0000ffff)+(位>>16和0x0000ffff);
}
void select(常量字符*src,字符*select,无符号整数模式){
对于(;模式;++src,模式>>=1)
如果(模式1)
*选择+++=*src;
*选择='\0';
}
int main(){
常量字符*selectList=“2345”;
字符buff[sizeof(selectList)/sizeof(char)+1];
int len=strlen(选择列表);
int max=pow(2,len)-1;
int i,j;

对于(i=1;i的可能副本,我需要找出给定的数字是否是彩色的。彩色的意思是,如果我们对一个数字进行不同长度的排列,排列中的数字的乘积应该是不同的。例如:263是彩色的,而236不是因为(2*3=6),这是排列(2,3,6,23,36)之一在你的问题中,你能链接到“彩色数字”的完整定义吗?排列中的数字的乘积(我不认为这些是排列)是否要求因子的数量至少是两个?我假设是这样,否则2=2,在236的“排列”集合中。
#include <stdio.h>
#include <string.h>
#include <math.h>

int bitcount(long bits){
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}

void select(const char *src, char *select, unsigned int pattern){
    for(;pattern;++src, pattern >>= 1)
        if(pattern & 1)
            *select++ = *src;
    *select ='\0';
}

int main(){
    const char* selectList = "2345";
    char buff[sizeof(selectList)/sizeof(char)+1];
    int len = strlen(selectList);
    int max = pow(2, len) -1;
    int i, j;
    for(i = 1; i<len;++i){
        //j:bit pattern
        for(j = 1; j<max;++j){
            if(i==bitcount(j)){
                select(selectList, buff, j);
                printf("%s\n", buff);
            }
        }
    }
    return 0;
}