gpt4 book ai didi

不重复的字符串组合 C

转载 作者:行者123 更新时间:2023-11-30 20:32:23 24 4
gpt4 key购买 nike

`#include <stdio.h>
void convert(int num){
char *word[10];

switch(num){
case 0: word [0] = "ZERO";
break;
case 1: word [1] = "ONE";
break;
case 2: word [2] = "TWO";
break;
case 3: word [3] = "THREE";
break;
case 4: word [4] = "FOUR";
break;
case 5: word [5] = "FIVE";
break;
case 6: word [6] = "SIX";
break;
case 7: word [7] = "SEVEN";
break;
case 8: word [8] = "EIGHT";
break;
case 9: word [9] = "NINE";
break;
}
printf("%s", word[num]);


}



int main(){
int a=0;
int b=0;
int c=0;



for(a=0;a<10;a=a+1){
for(b=0;b<10;b=b+1){
for(c=0;c<10;c=c+1){
convert(a);convert(b);convert(c);
printf("\n");
}
}

我想用字符串“零”“一”“二”等重复进行所有可能的组合,3×3,并将它们中的每一个存储在一个字符串数组中(从“zerozerozero”到“nineninenine”) ”)。现在这就是我所拥有的,我只打印每个组合,并且在将它们存储在数组中时遇到困难。我将不胜感激任何帮助或建议。 :D

最佳答案

我们可以声明一个足够大小的字符数组。对于三个单词的串联,我们使用 p 来查找下一个插入单词的位置。

#include <stdio.h>
#include <string.h>
int convert(int num, char* full_word, int p){
const char* word;
switch(num){
case 0: word = "ZERO";
break;
case 1: word = "ONE";
break;
case 2: word = "TWO";
break;
case 3: word = "THREE";
break;
case 4: word = "FOUR";
break;
case 5: word = "FIVE";
break;
case 6: word = "SIX";
break;
case 7: word = "SEVEN";
break;
case 8: word = "EIGHT";
break;
case 9: word = "NINE";
break;
}
strcpy(full_word + p, word);
p += strlen(word);
return p;
}



int main(){
int a=0;
int b=0;
int c=0;
int ind = 0;

char arr[1000][20];
for(a=0;a<10;a=a+1){
for(b=0;b<10;b=b+1){
for(c=0;c<10;c=c+1, ++ind){
char word[100];
int p = convert(a, word, 0);
p = convert(b, word, p);
convert(c, word, p);
strcpy(arr[ind], word);
}
}
}
for (int i = 0; i < 1000; ++i) {
printf("%s\n", arr[i]);
}
return 0;
}

关于不重复的字符串组合 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47624684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com