找到一组字符

时间:2013-09-19 14:21:05

标签: c recursion computer-science

所以你要用整数c和m读。 C是个案数,m是每个案例的行数。在m之后,有m行包含任意数量的字母,没有双打(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)

1 个答案:

答案 0 :(得分:1)

您混合使用passwordT

有时,您将其称为struct passwordT,有时您将其称为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;
}