怎样用c++写这道题

他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)用C++语言怎么写出这个程序_百度知道
用C++语言怎么写出这个程序
LT出了几道题,但小z觉得题目太难了,叫LT换掉一些题目。
每个题目都有一个难度值,难度值越高,表示题目越难。现在LT想知道,在换了题目之后,最难的那道题的难度值是多少。
第一行一个整数n,表示题目总数。
第二行n个整数 a1,a2...an ,表示...
我有更好的答案
for(int i=0;i++){scanf(&%d&;i&n;i++)scanf(&%d&;n&;=b[i]#include&cstdio&#include&cstring&gt,a[123],b[123],&b[i]);if(b[i]&gt,ma);return 0;}望采纳;int ma=-100000;scanf(&%d&;0) a[i]&#47,&n);n,&a[i]);for(int i=0;i&if(b[i]&0) a[i]+=b[i];ma=max(a[i],ma);}printf(&%d\#include&algorithm&#include&iostream&int main(){int n
互联网运营专家
为您推荐:
其他类似问题
您可能关注的内容
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。176被浏览24,440分享邀请回答#include &complex&
#include &tuple&
#include &iostream&
using std::complex;
complex&double& sqrt1pxm1(complex&double& x)
return x / (std::sqrt(x + 1.0) + 1.0);
// 返回根的个数和两个复根
std::tuple&int, complex&double&, complex&double&&
quadratic_roots(complex&double& a, complex&double& b, complex&double& c)
complex&double& x1 = 0.0, x2 = 0.0;
if (a == 0.0) { // 退化为 bx + c = 0
if (b != 0.0) {
x1 = -c / b;
return std::make_tuple(1, x1, x2);
} else if (c == 0.0) { // 0 = 0
return std::make_tuple(-1, x1, x2);
} else { // 1 = 0
return std::make_tuple(0, x1, x2);
} else if (b == 0.0) { // ax^2 + c = 0
x1 = std::sqrt(-c / a);
return std::make_tuple(2, x1, x2);
x1 = 0.5*b/a * sqrt1pxm1(- 4.0*a*c/b/b);
x2 = c / a / x1;
return std::make_tuple(2, x1, x2);
void test_quadratic_roots(complex&double& a, complex&double& b, complex&double& c)
int roots;
complex&double& x1, x2;
std::tie(roots, x1, x2) = quadratic_roots(a, b, c);
&& a && "*x^2 + " && b && "*x + " && c && " = 0\n"
&& "有" && roots && "个根\n";
if (roots & 0) {
std::cout && "x1 = " && x1 && "\n";
if (roots & 1) {
std::cout && "x2 = " && x2 && "\n";
std::cout && std::endl;
int main()
test_quadratic_roots(1.0, - (1e20+1.0), 1e20);
test_quadratic_roots(0, 0, 0);
test_quadratic_roots(0, 0, 1);
test_quadratic_roots(0, 1, 2);
test_quadratic_roots(1, 0, -2);
test_quadratic_roots(1, 3, 2);
test_quadratic_roots({0,1}, {2,3}, {4,5});
[1] 91 条评论分享收藏感谢收起#include &cassert&
#include &cmath&
#include &iostream&
#include &iomanip&
template &typename T&
std::pair&T, T& naive(T a, T b, T c) {
T dis = b * b - 4 * a * c;
assert(dis &= 0);
// not handling complex root
T sqrtdis = std::sqrt(dis);
T x1 = (-b - sqrtdis) / (a + a);
T x2 = (-b + sqrtdis) / (a + a);
return std::make_pair(x1, x2);
template &typename T&
void output(const char* fname, const std::pair&T, T&& p) {
std::cout && fname && "&" && typeid(T).name() && "&: " && p.first && ", " && p.second && std::endl;
int main() {
std::cout && std::fixed && std::setprecision(0);
output("naive", naive(1.0f, -f, f));
以下的都是我在 x86_64-apple-darwin16.0.0/clang-700.0.72 的运行结果(不同编译器/平台可能有差异):naive&f&: -inf, inf
使用单精度浮点数的话,因为
超出了单精度浮点数的范围,所以其结果是 inf,inf 一直传递下去,最终结果是正负 inf。那么我们试试 double:
output("naive", naive(1.0 , - ,
双精度浮点数没有上述问题,但 dis 因为有误差,这些误差也传递下去:dis
-b - sqrtdis 0
-b + sqrtdis
正确的中间值应为:dis
-b - sqrtdis 2
-b + sqrtdis
看看 double 的结果:naive&d&: 0,
第二个根正确了,第一个根不正确。给力点,升级 long double 看看:
output("naive",
naive (1.0L, -L, L));
输出:naive&e&: 4,
还是不行啊,我们看看中间的计算值:dis
-b - sqrtdis 8
-b + sqrtdis
较小的根的正确计算过程应该是:(-b - sqrtdis) / 2 = ( - ) / 2 = 1
而现时是:(-b - sqrtdis) / 2 = ( - ) / 2 → 4
所以,即使我们使用 long double,也未能解决另一个根不正确的问题。在 x86/x86-64 上 clang 使用 80位浮点数,如果在一些平台上 long double 使用 128 位的话,也许会更精确一些。然而,我们可以分析一下精度问题的来源。在这个求解过种中,我们发现 -b + sqrtdis 会得到正确的答案,而 -b - sqrtdis 会得到误差大的答案。对于两个接近的浮点小数 a, b,a - b 会比 a + b 造成更大的误差,这称为 catastrophic cancellation。在这个问题上,-b 和 sqrtdis 都是正数,我们可以先求出 -b + sqrtdis 带来的根,然后利用一元二次方程两根之积来求另一个根 :当然,对于任意的 b,我们也要考虑 b 是正数时,应先求 -b - sqrtdis 的根 [1]。再做实验:template &typename T&
T sgn(T x) {
(x & 0) return
else if (x & 0) return -1;
// https://en.wikipedia.org/wiki/Loss_of_significance#A_better_algorithm
template &typename T&
std::pair&T, T& better(T a, T b, T c) {
if (b == 0)
return naive(a, b, c);
T dis = b * b - 4 * a * c;
assert(dis &= 0);
// not handling complex root
T sqrtdis = std::sqrt(dis);
T x1 = (-b - sgn(b) * sqrtdis) / (a + a);
T x2 = c / (a * x1);
return std::make_pair(x1, x2);
int main() {
output("better", better(1.0f, -f, f));
output("better", better(1.0 , - ,
output("better", better(1.0L, -L, L));
所有结果:naive&f&: -inf, inf
naive&d&: 0,
naive&e&: 4,
better&f&: inf, 0
better&d&: , 1
better&e&: , 1
可以看到,double 版本也能获得正确的结果。当然,这种做法其实只是减少精度损失,而在这个例子中刚好获得完全正确的答案。要对所有系数都获得更精确的答案,需要使用任意精度运算。本答案的完整源代码位于 。----更新:在维基百科条目[2]下的 Vieta's formulas 小节,谈及如果两个根的数量级相差很大,,那么我们可以使用
去计算根的近似值:那么,template &typename T&
std::pair&T, T& vieta(T a, T b, T c) {
T x1 = -b / a;
T x2 = -c / b;
return std::make_pair(x1, x2);
可以令 float 版本也得到接近答案:vieta&f&: , 1
vieta&d&: , 1
vieta&e&: , 1
但要使用这个方式,必须先知道两个根有这样的特性。我想到的判断方法是计算两根之比直接计算 仍然会有溢出问题,或者理想地是用大约的对数(可取自浮点数的幂)去计算这个比的数量级。当然,用牛顿法或二分法也是能求出答案。以这个例子来说,如果取初值
,刚好一个牛顿迭代就已能得到 ,然后用根的积算出另一个根。但如果根的数量级较大,在迭代中计算 也是很容易溢出。[1] [2] 26215 条评论分享收藏感谢收起2被浏览419分享邀请回答11 条评论分享收藏感谢收起写回答C++函数编程20道习题汇总含其详细程序解答_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
C++函数编程20道习题汇总含其详细程序解答
&&C++函数编程20道习题汇总含其详细程序解答
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,同时保存到云知识,更方便管理
还剩8页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢}

我要回帖

更多关于 人物描写作用答题方法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信