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