gpt4 book ai didi

c - 使用递归回文

转载 作者:行者123 更新时间:2023-11-30 18:41:08 24 4
gpt4 key购买 nike

以下代码需要检查回文并使用递归向后写入用户输入,然后使用相同的方法找到最大公分母。找到 gcd 并判断它是否是回文的代码部分可以工作,但每次它获取反转代码的部分时都会崩溃。为什么每次都崩溃。

#include <stdio.h>
#include <stdlib.h>
/*Delare the prototypes*/
char palindromes(char[], int);
char backwards(char[], int, int);
int gcdfunc(int, int);
int findgcd(int, int);

int main ()
{
char userinput[10];/*Declares the character array for the input*/
int index= 0;/*Index for the counting loop*/
int counter= 0;/*Counts number of elements entered in the array*/
int printindex= 0;/*Index to print the values on the screen*/
int gcd= 0;/*Sets a value for the GCD*/
int palcheck = 0;
int value1= 0;/*User value 1*/
int value2= 0;/*User value 2*/
int flipindex=0;/*Sets an index for the gcd function*/

printf("Please enter a series of nine or less characters to test for a palindrome.\n");
scanf(" %9s%n", &userinput, &counter);
printf("\n");

palcheck = palindromes(userinput, counter-1);

if(palcheck == 0)
{
printf("Your input was not a palindrome \n");
}/*End of if statement*/
else
{
printf("Your input was a palindrome \n");
}/*End of else statement*/

backwards(userinput, counter-1, flipindex);

printf("Your input backwards is: ");
for(printindex; printindex <= counter; printindex++)
{
printf("%c", userinput[printindex]);
}/*End of printing backwards loop*/
printf("\n");

printf("\nEnter two numbers: ");
scanf("%d %d",&value1,&value2);

gcd=gcdfunc(value1, value2);

printf("The GCD of %d and %d is: %d",value1,value2,gcd);

system("pause");

}/*End of main function*/

char palindromes(char userinput[], int counter)
{
int palindex= 0;/*Declares the index to check for a palindrome*/
int palendinx= counter;
int modulus = counter%2;
if(modulus = 0)
{
if(userinput[palindex]==userinput[palendinx])
{
palindex++;
palendinx--;
if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx])
return 1;
palindromes(userinput, counter);
}
else
{
return 0;
}
}
else
{
if(userinput[palindex]==userinput[palendinx])
{
palindex++;
palendinx--;
if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx])
return 1;
palindromes(userinput, counter);
}
else
{
return 0;
}
}
}/*End of palidrome function*/

char backwards(char userinput[], int counter, int flipindex)
{
while(flipindex<(counter/2))
{
char temp;/*Sets a temporary value to swap the two values*/
temp = userinput[flipindex];
userinput[flipindex] = userinput[counter-flipindex];
userinput[counter-flipindex] = temp;
backwards(userinput, counter, flipindex++);
}

}/*End of reverse function*/

int gcdfunc(int value1, int value2)
{
int gcd;
gcd=findgcd(value1,value2);
return gcd;
}

int findgcd(int value1,int value2)
{
while(value1!=value2)
{
if(value1>value2)
return findgcd(value1-value2,value2);
else
return findgcd(value1,value2-value1);
}
return value1;
}

最佳答案

在你的函数中,没有变量的传递,它一遍又一遍地保持在同一个循环中,并且在递归中,同一个函数不共享相同的变量,它会创建新的变量,如果你修改一个变量,它不会影响其他变量变量。

char palindromes(char userinput[], int counter)
{
int palindex= 0;/*Declares the index to check for a palindrome*/
int palendinx= counter;
int modulus = counter%2;
if(modulus = 0)
{
if(userinput[palindex]==userinput[palendinx])
{
palindex++;
palendinx--;
if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx])
return 1;
palindromes(userinput, counter);
}
else
{
return 0;
}
}
else
{
if(userinput[palindex]==userinput[palendinx])
{
palindex++;
palendinx--;
if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx])
return 1;
palindromes(userinput, counter);
}
else
{
return 0;
}
}
}/*End of palidrome function*/

这是我所做的代码

int palindromes(char userinput[], int counter,int index , int palendex)
{

if(userinput[palendex] == userinput[index])
{

if(counter%2 == 0 && ( index - palendex) == 1)
return 0;
if( index == palendex )
return 0;
else
return palindromes(userinput, counter , index-1 ,palendex+1 );


}
else
return 1;
return 1;
}

在你的后向函数中,它永远不会离开循环,并且会执行很多可用内存耗尽的实例。所以我将其更改为 if 语句,它看起来像这样(以及一些更改)

int backwards(char userinput[], int counter, int flipindex)
{
if(flipindex<(counter/2))
{
char temp;/*Sets a temporary value to swap the two values*/
temp = userinput[flipindex];
userinput[flipindex] = userinput[counter-flipindex-1];
userinput[counter-flipindex-1] = temp;
backwards(userinput, counter, flipindex+1);
}
return 1;
}

关于c - 使用递归回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22949064/

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