我该如何解决这个问题 [英] How do I solve this question

查看:85
本文介绍了我该如何解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int  foo( int  a, int  b){
if (a%b == 0 ){
return b;
} 其他 {
return foo(b,a%b);
}
}





•foo(17,3)给出的输出是多少?

•foo(3,9)给出的输出是多少?

•执行一系列测试并记录结果,因此简要说明foo函数的用途。 (示例:找到x和y的最大值)。



我尝试过的事情:



foo(17,3)= 1

foo(3,9)= 3

 a 9 4 100 
b 50 67 45
结果1 5 0





函数foo用于计算两个整数和找到a和b的余数。如果a%b的值为0,则函数返回0.否则,b的新值将用于计算%b,直到余数为0.

解决方案

< blockquote> foo(4,67)的结果不是5!

foo(100,45)的结果不是0!

引用:

因此简要解释了foo函数的用途。 (例如:找到x和y的最大值)。



你必须找到foo执行的数学函数的名称,而不是用伪代码描述。

Quote:

执行一系列测试并记录结果



设置一个参数12并使用第二个参数1..20的所有值执行foo。

使用15作为第一个参数执行相同的操作。

交换参数。

你应该在结果中看到一些东西。


最重要的提示:这是你的功课。它将帮助你提高你的技能



提示1:使用 printf 用于输出。



提示2:您可以使用包装功能,例如:

  void  fooX( int  a, int  b)
{
int r = foo(a,b);
printf( Result ... input,...); // 控制台上的一些有用输出
}

提示3:创建一些测试场景如:

  void  runtest()
{
for int i = 0 :i< < span class =code-digit> 100 ; i ++){
int a = rand()% 10 ; // a范围为0到9
int b = rand()% 100 ; // b范围为0到99

fooX(a,b );
}
}

提示4:使用阵列

  int 数据[ 2 ] [  10 ] = { 0 }; 
data [ 0 ] [ 0 ] = 9 ; // a
data [ 1 ] [ 0 ] = 19 ; // b


您正确回答了问题(1) (2)。

现在你必须执行一系列测试(尝试许多不同的输入数字对)并查看它是否在函数的答案中出现一个模式,以便正确回答问题(3)。< br $> b $ b



顺便提一下,您报告的其他测试,即:

引用:

a 9 4 100

b 50 67 45

结果1 5 0

不正确

我报告我所做的一次运行的输出:

 ab结果
17 3 - > 1
3 9 - > 3
80 15 - > 5
49 8 - > 1
30 12 - > 6
1000 25 - > 25
9 50 - > 1
67 5 - > 1
100 45 - > 5


int foo(int a, int b){
	if (a%b==0){
		return b;
	}else{
		return foo(b,a%b);
	}
}



• What is the output given by foo(17, 3)?
• What is the output given by foo(3, 9) ?
• Perform a series of tests and document your results, hence explain briefly the purpose of the foo function. (Example: it is finding the max of x and y).

What I have tried:

foo(17,3)=1
foo(3,9)=3

a       9	4	100
b       50	67	45
result	1	5	0



The function foo is used to compute two integer and find the remainder of a and b. If the value of a%b is 0, the function returns 0. Else the new value of b will be used to calculate a%b until the remainder is 0.

解决方案

Result of foo(4,67) is not 5 !
Result of foo(100,45) is not 0 !

Quote:

hence explain briefly the purpose of the foo function. (Example: it is finding the max of x and y).


You have to find the name of mathematical function performed by foo, not describe fo in pseudocode.

Quote:

Perform a series of tests and document your results


set one parameter to 12 and perform foo with all values of second parameter 1..20.
Do the same with 15 as first parameter.
swap parameters.
you should see something in the results.


The most important tip: this is your homework. It will help you to enhance your skills.

Tip 1: use printf for output.

Tip 2: you may use a wrapping function like:

void fooX(int a, int b)
{
  int r = foo( a,b );
  printf("Result ... input", ... );//some useful output at the console
}

Tip 3: create some test scenarios like:

void runtest()
{
  for( int i = 0: i < 100; i++ ) {
    int a = rand() % 10;          // a in the range 0 to 9
    int b = rand() % 100;         // b in the range 0 to 99

    fooX( a, b );
  } 
}

Tip 4: use an array

int data[2][10] = {0};
data[0][0] = 9;//a 
data[1][0] = 19;//b


You answered correctly to questions (1) and (2).
Now you have to perform a series of tests (try many different pairs of input numbers) and see if it emerges a pattern in function's answer, in order to answer correctly to question (3) too.


By the way, the additional tests your reported, namely:

Quote:

a 9 4 100
b 50 67 45
result 1 5 0

are incorrect
I report the output of a run I made:

   a   b   result 
  17   3 ->  1
   3   9 ->  3
  80  15 ->  5
  49   8 ->  1
  30  12 ->  6
1000  25 -> 25
   9  50 ->  1
  67   5 ->  1
 100  45 ->  5


这篇关于我该如何解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆