gpt4 book ai didi

c - 寻找一组字符

转载 作者:行者123 更新时间:2023-11-30 20:27:33 25 4
gpt4 key购买 nike

所以你要读入整数c和m。 C 是案例数,m 是每个案例的行数。 m 之后,有 m 行包含任意数量的字母,没有 double (a-z)。在这些行之后有一个 int r ,如果我们对每行包含一个字母的每个可能结果进行递归,形成一个字符串,这就是我们想要的解决方案编号。

有一种方法可以跳过递归,直接进入我们想要的等级(int r)。我尝试将结构发送到函数中时遇到指针问题。任何帮助都会很棒。这是我的代码示例

#include <stdio.h>
#include <stdlib.h>
#define MAX 26
//Struct to hold all information inside
struct passwordT{
char letters[MAX];
int charCount;
int possibleSkips;
int solutionArray;
};

//Functions. numOfSkips gets number of solutions to skip for each line, and getSol gets the array for the
lines of chars.
passwordT* numOfSkips(passwordT *T, int *total, int m);
int getSol(int ps, int v, int m, int r);



int main(){
int i, j, k;
int c, m, r;
char p;
int total = 0;

//Read in Number of cases
scanf("%d", &c);

//Declaring a pointer to struct passwordT, then making mallocing for the size of the number of cases
passwordT* ptrToPass;
ptrToPass = malloc(c*sizeof(passwordT));

//Cycle through cases
for (i=0; i<c;i++){

//Read in number of lines
scanf("%d", &m);

//Reading in the string on each line
for(j=0;j<m;j++){
scanf("%s", &ptrToPass[j].letters);
}

//Get number of characters for each string
for(j=0; j<m; j++){
ptrToPass[j].charCount = 0;
k=0;
p = ptrToPass[j].letters[0];
while(p!='\0'){
ptrToPass[j].charCount ++;
k++;
p = ptrToPass[j].letters[k];
}
}

//Scan in solution number wanted
scanf("%d", &r);

//Get number of solutions to skip
ptrToPass = numOfSkips(ptrToPass, &total, m);

//Get solution set
for(j=0;j<m;j++)
ptrToPass[j].solutionArray = getSol(ptrToPass[j].possibleSkips, 1, m, r);

//Print results
for(j=0;j<m;j++)
printf("%c", ptrToPass[j].letters[ptrToPass[j].solutionArray]);
}

return 0;
}

//This struct is for an algorithm used to skip to int r without recursion
passwordT* numOfSkips(passwordT *T, int *total, int m){

passwordT *skippers;
int i = 0;

//If i = 0, possible solution skips is 1. Else, possible solution skips for skippers[i] is total. Then total =
total times current lines char count
for(i=0;i<m;i++){

if(i==0){
skippers[i].possibleSkips = 1;
total = skippers[i].charCount;
}

else{
skippers[i].possibleSkips = total;
total = total * skippers[i].charCount;
}
}
}


// Function used to get the array of the solution (int r) would be if we used recursion
int getSol(int ps, int v, int m,int r){
int sa = 0;
int i;
for(i=0; i<m; i++){
while(v <= r - ps){
sa ++;
v += sa;
}
}

return sa;

}

这些是我的错误:

12: syntax error before '*' token (Declaration of first function numOfSkips in header
13: warning, data definition has no type or storage class

IN MAIN

28: passwordT undeclared, first use in this function ( so when i have passwordT* ptrToPass;)
28: ptrtoPass undeclared

IN numOfSkips

73: syntax error before '*'--> error with passwordT* numOfskips(passwordT *T, int *total, int m){
79: m undeclared (first use in this function) --> First time I use m in function numOfSkips
83: total undeclared (same situation as line 79)

最佳答案

您可以混合搭配使用passwordT

有时,您将其称为structpasswordT,有时您将其称为typedef。

我建议选择一个并坚持使用它

您最新的错误消息表明您没有 C-99 编译器。
(哎呀,如果你在我第一次提出这个问题时提到你正在使用哪个编译器,那真的会有帮助......)

相反,尝试这个(旧式)声明:

typedef struct 
{
char letters[MAX];
int charCount;
int possibleSkips;
int solutionArray;
} passwordT;

passwordT* numOfSkips(passwordT *T, int *total, int m);
<小时/>

在这部分代码中,您以不同的方式处理total:

passwordT* numOfSkips(passwordT *T, int *total, int m){
[...]
total = skippers[i].charCount;
}

total 被声明为 pointer-to-int,但您将其视为 int

你明白指针和它们指向的类型之间的区别吗?
total 是什么类型?

我建议为变量名称(尤其是指针)添加前缀,以帮助您跟踪它们的类型。
(现在名称可以帮助您记住它是一个指针)

passwordT* numOfSkips(passwordT *T, int *ptr_total, int m){
[...]
*ptr_total = skippers[i].charCount;
}

关于c - 寻找一组字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18897305/

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