zarif_2002's blog

By zarif_2002, history, 5 years ago, In English

include<stdio.h>

include<math.h>

int main(){

int a,t,i;

double y,z;

scanf("%d",&t);

for(i = 0; i < t; i++){

    scanf("%d",&a);

    if(a == 1){

        printf("N\n");
    }
    else{
        y = ((double)a + sqrt((double)a*(double)a - 4*(double)a))/2;

        z = ((double)a - sqrt((double)a*(double)a - 4*(double)a))/2;

        printf("Y %0.9lf %0.9lf\n",y,z);
    }
}
return 0;

why this code shows wrong answer(1076 C)

  • Vote: I like it
  • -27
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by zarif_2002 (previous revision, new revision, compare).

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

The problem solution has three cases:

  1. if d = 0, then the answer is Yes, and a = b = 0.

  2. if d = 1, 2, or 3, then the answer is No.

  3. if d > 3, then the answer is Yes, a = c (1+e) and b = c (1-e), where c = d/2 and e = sqrt(1-2/c).

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It is because you do not check if discriminant is lower then zero, but check if a==1 instead. And if D is equal to zero, you should output only one root (actually there will be two same roots).

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In this problem you literally have to solve a + b = d, a * b = d when d is given. The resulting equation after substituting ( for example b = d — a ) into the second equation will be -a^2 + ad — d = 0 ( a quadratic equation ), it has no solutions for real numbers when a * a — 4 * a < 0.