首页 > ACM题库 > HDU-杭电 > HDU 3876-A pupil’s problem-数学相关-[解题报告]HOJ
2015
04-13

HDU 3876-A pupil’s problem-数学相关-[解题报告]HOJ

A pupil’s problem

问题描述 :

Wei wei is a pupil. He is tired of calculating the quadratic equation. He wants you to help him to get the result of the quadratic equation. The quadratic equation’ format is as follows: ax^2+bx+c=0.

输入:

The first line contains a single positive integer N, indicating the number of datasets. The next N lines are n datasets. Every line contains three integers indicating integer numbers a,b,c (a≠0).

输出:

The first line contains a single positive integer N, indicating the number of datasets. The next N lines are n datasets. Every line contains three integers indicating integer numbers a,b,c (a≠0).

样例输入:

3
1 2 1
1 2 3
1 -9 6

样例输出:

-1.00
NO
0.73 8.27

点击打开链接

ps:解二次方程,wrong了一次,x1和x2木有判断大小!!!
   
    (1)若b^2-4ac<0,无实数根,有两个复数根:x1=[-b+i√(4ac-b^2)]/(2a) , x2=[-b-i√(4ac-b^2)]/(2a);
    (2)若b^2-4ac=0,有两个相等实根: x1=x2=-b/(2a);
    (3)若b^2-4ac>0,有两个不等实根: x1=[-b+√(b^2-4ac)]/(2a) ,x2=[-b-√(b^2-4ac)]/(2a) 。

#include<stdio.h>
#include<math.h>
int main()
{
	   int t;
	   int a,b,c;
	   double x1,x2,x,temp;
	   scanf("%d",&t);
	   while(t--)
	   {
		   scanf("%d%d%d",&a,&b,&c);
		   x=b*b-4*a*c;
		   if(x<0)
			   printf("NO\n");
		   else if(x==0)
		   {
			   x1=-1.0*b/(2*a);
			   printf("%.2f\n",x1);
		   }
		   else if(x>0)
		   {
			   x1=(-1.0*b-sqrt(x))/(2*a);
			   x2=(-1.0*b+sqrt(x))/(2*a);
			   if(x1>x2)//
			   {
				   temp=x1;x1=x2;x2=temp;
			   }
			   printf("%.2f %.2f\n",x1,x2);
		   }
	   }
	   return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/yangyafeiac/article/details/7801883


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)

  3. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。