[英]Find a two digit number with recursion
I'm trying to code something in C by using recursion. 我正在尝试使用递归在C语言中编写代码。
The user writes two positive numbers of same length and the program gives him a new number, which is composed like this : 用户编写两个相同长度的正数,程序给他一个新的数字,其组成如下:
new number unity digit = the smallest digit in the second positive number that the user wrote. 新数字单位数字=用户写的第二个正数中的最小数字。
new number ten digit = the biggest digit in the first positive number that the user wrote. 新数字十位数=用户写的第一个正数中的最大位数。
Very simple in fact, here is an example : 实际上非常简单,这是一个示例:
5642
and 2371
5642
和2371
will give us : 61
. 会给我们: 61
。
I tried something like this : 我尝试过这样的事情:
#include <stdio.h>
int calcPair(int a, int b){
int number = calcPair(a/10, b/10);
int digit1 = (number/10);
int digit2 = number%10;
if(digit1 < a%10){
digit1 = a%10;
}
if(digit2 > b%10){
digit2 = b%10;
}
return(number);
}
int main()
{
int a, b, number=0;
printf("Please enter two positive number of same length:\n");
scanf("%d", &a);
scanf("%d", &b);
calcPair(a, b);
printf("The two-digit number composed from %d, %d is: %d", a, b, number);
return 0;
}
BUT the program doesn't run at all.. and closes. 但是程序根本没有运行..并关闭。
Maybe someone can correct me ? 也许有人可以纠正我? Or helping me finding the mistake . 或者帮助我发现错误 。
Thanks by advance. 预先感谢。
Your recursion can never end. 您的递归永远不会结束。 Consider the following line in calcPair
: 考虑calcPair
的以下行:
int number = calcPair(a/10, b/10);
This statement will always be executed unless you make it conditional, such as: 除非您有条件,否则该语句将始终执行,例如:
int number;
if((a != 0) || (b != 0))
number = calcPair(a/10, b/10);
Eventually, because you're dividing both numbers by 10, this condition will prove FALSE
. 最终,因为您将两个数字都除以10,所以此条件将证明为FALSE
。
Something like this: 像这样:
int calcPair(int a, int b){
int number;
if (a < 10 && b < 10) {
number = a*10 + b;
} else {
int digita = a%10;
int digitb = b%10;
number = calcPair(a/10, b/10);
if(digita > number/10){
number = digita*10 + number%10;
}
if(digitb < number%10){
number = (number/10)*10 + digitb;
}
}
return number;
}
Also, a small fix to the main: 此外,主要解决方案:
int main()
{
int a, b, number=0;
printf("Please enter two positive number of same length:\n");
scanf("%d", &a);
scanf("%d", &b);
number = calcPair(a, b);
printf("The two-digit number composed from %d, %d is: %d", a, b, number);
return 0;
}
Whether or not you are allowed to (you did not specify in OP), 是否允许 (您未在OP中指定),
here is a recursive search method using strings : 这是使用字符串的递归搜索方法 :
Strings are just an array of char
. 字符串只是char
的数组。 because you are interested in distinguishing the individual digits within a larger integer, the char
data type will be a sufficient size container to facilitate the comparison. 因为您有兴趣区分较大整数内的各个数字,所以char
数据类型将是一个足够大的容器,便于进行比较。
Using arrays of char
(strings) within a recursive function with exit criteria of strlen()
> 0 will allow you to walk through each integer , and select the appropriate value (min or max). 在退出条件为strlen()
> 0的递归函数中使用char
(字符串)数组将使您可以遍历每个整数 ,并选择适当的值(最小值或最大值)。
This approach uses two recursive functions : getMinDigit()
and getMaxDigit()
, both returning a char representing the maximum value digit, or minimum value digit of their respective original multi-digit integer. 此方法使用两个递归函数 : getMinDigit()
和getMaxDigit()
,都返回一个char,它们表示各自原始多位数整数的最大值或最小值。 These results are then concatenated, and converted back into a two digit integer. 然后将这些结果连接起来,并转换回两位数的整数。
Here is the example code that given: 这是给出的示例代码 :
5642 and 2371 5642和2371
will give us : 61. 会给我们:61。
char getMinDigit(char *digit)
{
static char val='9';//largest single digit base 10
int len=0;
if(strlen(digit) > 0)
{
len = strlen(digit);
if(digit[len-1] < val) //test for smallest char in string
{
val = digit[len-1];
digit[len-1] = 0;
getMinDigit(digit);
}
else
{
digit[len-1] = 0;
getMinDigit(digit);
}
}
return val;
}
char getMaxDigit(char *digit)
{
static char val='0'; //smallest single digit base 10
int len=0;
if(strlen(digit) > 0)
{
len = strlen(digit);
if(digit[len-1] > val) //search for largest char in string
{
val = digit[len-1];
digit[len-1] = 0;
getMaxDigit(digit);
}
else
{
digit[len-1] = 0;
getMaxDigit(digit);
}
}
return val;
}
int calcPair(int a, int b)
{
char big[10]={""}, small[10]={""};
char Big, Small;
char result[3]={""};
sprintf(big, "%d", a);
sprintf(small, "%d", b);
Big = getMaxDigit(big); //recursive function
Small = getMinDigit(small); //recursive function
sprintf(result, "%c%c", Big, Small);
return atoi(result);
}
int main(void)
{
int result = calcPair(5642, 2371);
printf("%d", result);
return 0;
//for illustration, hard coded to OP values
//int a, b, number=0;
//printf("Please enter two positive number of same length:\n");
//scanf("%d", &a);
//scanf("%d", &b);
//calcPair(a, b);
//printf("The two-digit number composed from %d, %d is: %d", a, b, number);
//return 0;
}
I think you can refactor your code to be more expressive of your requirement, with a few helper functions. 我认为您可以通过一些辅助函数来重构代码,使其更能表达您的要求。
int greater(int a, int b)
{
return (a>b);
}
int less(int a, int b)
{
return (a<b);
}
int pickDigit(int n, int (*func)(int, int))
{
int ret = n%10;
n /= 10;
while ( n > 0 )
{
if ( fun(n%10, ret) )
{
ret = n%10;
}
n /= 10;
}
return ret;
}
int getBiggestDigit(int n)
{
return pickDigit(n, greater);
}
int gteSmallestDigit(int n)
{
return pickDigit(n, less);
}
int numDigits(int n)
{
int ret = 0;
while (n > 0 )
{
++ret;
n /= 10;
}
return ret;
}
int calcPair(int a, int b)
{
if ( numDigits(a) != numDigits(b) )
{
// Deal with error.
}
return betBiggestDigit(a)*10+getSmallestDigit(b);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.