首页 > 解决方案 > 当我使用 fout 而不是 cout 时,我的代码会打印一个不同的数字

问题描述

嘿,所以我正在做这个练习,我发现当你使用 fout 而不是 cout 时,它会打印一个不同的数字,而你必须打印我在代码示例中设置的东西,如果你输入

14 -783 421 693

-153 680 443 741

作为输入

它将计算出 595441

它会输出 0595441

 #include <bits/stdc++.h>
    using namespace std;
    int main() {
      ifstream fin("billboard.in");
      ofstream fout("billboard.out");
    int x1,y1,x2,y2,px1,py1,px2,py2;

    fin>>x1>>y1>>x2>>y2>>px1>>py1>>px2>>py2;

    x1+=1000;
    x2+=1000;
    y1+=1000;
    y2+=1000;
    px1+=1000;
    px2+=1000;
    py1+=1000;
    py2+=1000;

    int a1=(x2-x1)*(y2-y1);

    if(x2<=px2&&x1>=px1&&y2<=py2&&y1>=py1){
    fout<<0;
    }

    else if(x2<=px2&&x1>=px1){
    if(py2>=y2){
      fout<<0;
    }
     if((py2<=y1)|(py1>y1&&py2<y2) ){
    fout<<a1;
    }else{
    int ma=max(py1,y1);
    int mi=min(py2,y2);
    fout<<((x2-x1)*(y2-y1))-((x2-x1)*(mi-ma));
    cout<<((x2-x1)*(y2-y1))-((x2-x1)*(mi-ma)); /********************************************************/
    }
    }


    else if(y2<=py2&&y1>=py1){

     if(px2<=x1|px1>x1){
    fout<<a1;
    }else{
    int ma=max(px1,x1);
    fout<<((y2-y1)*(x2-x1))-((y2-y1)*(px2-ma));
    }
    }


    else{
      fout<<a1;
    }

    return 0;
    }

标签: c++11

解决方案


好的,所以发生的事情是我有一个 if that wold first fout<<0; 所以你必须非常小心

#include <bits/stdc++.h>
using namespace std;
int main() {
  ifstream fin("billboard.in");
  ofstream fout("billboard.out");
int x1,y1,x2,y2,px1,py1,px2,py2;

fin>>x1>>y1>>x2>>y2>>px1>>py1>>px2>>py2;

x1+=1000;
x2+=1000;
y1+=1000;
y2+=1000;
px1+=1000;
px2+=1000;
py1+=1000;
py2+=1000;

int a1=(x2-x1)*(y2-y1);

if(x2<=px2&&x1>=px1&&y2<=py2&&y1>=py1){
fout<<0;
}

else if(x2<=px2&&x1>=px1){

 if((py2<=y1)|(py1>y1&&py2<y2) ){
fout<<a1;
}else{
int ma=max(py1,y1);
int mi=min(py2,y2);
fout<<((x2-x1)*(y2-y1))-((x2-x1)*(mi-ma));

}
}


else if(y2<=py2&&y1>=py1){

 if(px2<=x1|px1>x1){
fout<<a1;
}else{
int ma=max(px1,x1);
fout<<((y2-y1)*(x2-x1))-((y2-y1)*(px2-ma));
}
}


else{
  fout<<a1;
}

return 0;
}

推荐阅读