这是我面试的问题。我的答案是否正确? [英] This Is A Interview Question For Me. Am I Correct With My Answer?

查看:81
本文介绍了这是我面试的问题。我的答案是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(考虑函数

f(X)= 1 / XD

,其中newton-Raphson迭代给出了

Xi + 1 = Xi-(f(Xi)/ f'(Xi))= Xi - ((1 / Xi-D)/( - 1 / Xi ^ 2))= Xi + Xi(1-DXi)= Xi(2-DXi) )

哪个可以用Xi计算,只使用乘法和减法,或者使用两个融合乘法加法。你能想到它的作用以及你在实现它时需要注意的事情吗?写一个C代码来计算它。)



我的ans:

#include< stdio.h>

main()

{

浮动X [15],X1 [15]; //我为Xi取X1,为Xi + 1取X1;

int D;

printf(输入X [i]的值:);

scanf(%f,& X [15]);

printf(\输入D的值:);

scanf(%d,& D);

X1 [15] = X [15] *(2-(D * X [15]));

printf(X [i + 1] =%f \ n,X1 [15]);

}



我是c编程的初学者。谢谢。

( Consider the function
f(X)=1/X-D
for which the newton-Raphson iteration gives
Xi+1=Xi-(f(Xi)/f'(Xi))=Xi-((1/Xi-D)/(-1/Xi ^2))=Xi+Xi(1-DXi)=Xi(2-DXi)
Which can be calculated from Xi using only multiplication ans subtraction , or using two fused multiply-addition. Can you expect what it does and what all care you are required to take while implementing it and can you write a C code to compute it. )

my ans:
#include<stdio.h>
main()
{
float X[15],X1[15]; //i took X for Xi and X1 for Xi+1;
int D;
printf("enter the value for X[i]:");
scanf("%f",&X[15]);
printf("\nenter the value of D:");
scanf("%d",&D);
X1[15]=X[15]*(2-(D*X[15]));
printf("X[i+1]=%f \n",X1[15]);
}

Am some what beginner at c programming.Thank you.

推荐答案

做我们编写代码时我们都要做的事情:测试并看看。

编译代码,运行咖啡,找出应该得到的结果 - Wolfram Alpha [ ^ ]可能有所帮助 - 然后看看你的代码产生了什么。如果您尝试的所有值都相同,那么它可能会有效。



如果不是......
Do what we all have to do when we write code: test it and see.
Compile the code, run the coffee, and work out what results you should get - Wolfram Alpha[^] may help - and then see what your code produces. If it's the same for all values you try, then it probably works.

If it doesn't...


这篇关于这是我面试的问题。我的答案是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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