#14736: 想請問為甚麼測試給過 送出就變NA 是哪裡出錯了嗎


h411016 (hantsai)


#include <iostream>
#include <cmath>
using namespace std;

void judge(int a,int b,int c){
  int D=b*b-4*a*c;
  int A=-b/2*a;
  if(D<0){
    cout<<"No real root"<<endl;
  }
  else if(D==0){
    cout<<"Two same roots x="<<A<<endl;
  }
  else{
    int big=(-b+sqrt(D))/(2*a);
    int small=(-b-sqrt(D))/(2*a);
    cout<<"Two different roots x1="<<big<<" , x2="<<small<<endl;
  }
}

int main(){
  int a,b,c;
  while(cin>>a>>b>>c){
    judge(a,b,c);
  }
  return 0;
}
#14758: Re:想請問為甚麼測試給過 送出就變NA 是哪裡出錯了嗎


kev8067@gmail.com (卓承緯)


#include 
#include 
using namespace std;

void judge(int a,int b,int c){
  int D=b*b-4*a*c;
  int A=-b/2*a;
  if(D<0){
    cout<<"No real root"<<endl;
  }
  else if(D==0){
    cout<<"Two same roots x="<<A<<endl;
  }
  else{
    int big=(-b+sqrt(D))/(2*a);
    int small=(-b-sqrt(D))/(2*a);
    cout<<"Two different roots x1="<<big<<" , x2="<<small<<endl;
  }
}

int main(){
  int a,b,c;
  while(cin>>a>>b>>c){
    judge(a,b,c);
  }
  return 0;
}

根的公式解是 -B/(2A) 你的code int A=-b/2*a; 看看哪裡怪怪的吧

測資是0所以沒驗出來

 

#16916: Re:想請問為甚麼測試給過 送出就變NA 是哪裡出錯了嗎


410421236 (努力)


#include 
#include 
using namespace std;

void judge(int a,int b,int c){
  int D=b*b-4*a*c;
  int A=-b/2*a;
  if(D<0){
    cout<<"No real root"<<endl;
  }
  else if(D==0){
    cout<<"Two same roots x="<<A<<endl;
  }
  else{
    int big=(-b+sqrt(D))/(2*a);
    int small=(-b-sqrt(D))/(2*a);
    cout<<"Two different roots x1="<<big<<" , x2="<<small<<endl;
  }
}

int main(){
  int a,b,c;
  while(cin>>a>>b>>c){
    judge(a,b,c);
  }
  return 0;
}



你的int A=-b/2*a; 這行電腦再讀的時候因為沒有括號所以就直接從左到右做起,所以要改成-b/(2*a);