忧默的小男孩 2022-09-02 16:51 采纳率: 94.4%
浏览 68
已结题

c++编译器输出问题?

代码已经通过了,但是不知道为什么要改,比如正确的代码一开始会输入一个n表示要输入n行测试数据,我定义i等于0,i小于n,然后用那个可以通过的代码测试,一直tm的少输出一行,必须把n改为n+1,才能通过

第四张图片标蓝的那一段代码,不写就输出10行,写上就输出9行,答案还都不对,if-else和单独用if为什么出来的结果不一样? 这到底是什么问题啊

img

img

img

img

//可以通过的代码
#include <iostream>
#include <cstdio>

using namespace std;

int main(){
    int n;
    cin >> n;
    for(int i=0;i<n+1;i++){
        char a[100];
        int count=0;
        fgets(a,100,stdin);
        for(int j=0;j<100;j++){
            if(a[j]=='\0') break;
            if(a[j]!=' '){
                count++;
            } 
            if(a[j]==' ') break;
                        
        }
        
        if(a[0]-a[count+1]==0)
            cout << "Tie" << endl;;
        if(a[0]-a[count+1]==1)
            cout << "Player1" << endl;
        if(a[0]-a[count+1]==-1)
            cout << "Player2" << endl;
        if(a[0]-a[count+1]==6)
            cout << "Player2" << endl;
        if(a[0]-a[count+1]==-6)
            cout << "Player1" << endl;
        if(a[0]-a[count+1]==5)
            cout << "Player1" << endl;
        if(a[0]-a[count+1]==-5)
            cout << "Player2" << endl;
    }
 
}
//不能通过的代码
#include <iostream>
#include <cstdio>

using namespace std;

int main(){
    int n;
    cin >> n;
    for(int i=0;i<n+1;i++){
        char a[100];
        int count=0;
        fgets(a,100,stdin);
        for(int j=0;j<100;j++){
            if(a[j]=='\0') break;
            if(a[j]!=' ') count++;
            if(a[j]==' ') break;
        }
        
        if(a[0]-a[count+1]==0){
            cout << "Tie" << endl;;
        }else if(a[0]-a[count+1]==1){
            cout << "Player1" << endl;
        }else if(a[0]-a[count+1]==-1)
            cout << "Player2" << endl;
            else if(a[0]-a[count+1]==6)
            cout << "Player1" << endl;
            else if(a[0]-a[count+1]==-6)
            cout << "Player2" << endl;
            else if(a[0]-a[count+1]==5)
            cout << "Player1" << endl;
            else  if(a[0]-a[count+1]==-5)
            cout << "Player2" << endl;
    }
 
}
  • 写回答

5条回答 默认 最新

  • CSDN专家-sinJack 2022-09-02 17:01
    关注

    如果表达式不变,是没区别的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月4日
  • 已采纳回答 9月2日
  • 创建了问题 9月2日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。