You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++输出问题:无法在同一行输出空格分隔的两个结果,求助

Fixing Your C++ Output Formatting Issue

Hey there! Since you're already comfortable with C, this is a quick fix related to how C++'s cout handles output compared to C's printf. Let's break down what's going wrong and how to fix it.

What's Causing the "56" Output Instead of "5 6"?

Your current code has two small, easy-to-fix issues:

  1. No explicit space between output values: When you write cout<< N << R <<endl;, cout streams the values back-to-back without any separation—this is exactly like doing printf("%d%d", N, R); in C, which would also spit out "56" instead of "5 6".
  2. Output is placed outside the loop: While this doesn't break your test input (since s=1), if you ever have multiple test cases, only the last pair of N and R would get printed. It's cleaner and safer to handle each test case's output inside the loop.

Corrected Code

Here's the adjusted version that will match your expected output:

#include<iostream> 
using namespace std; 

int main() { 
    int s;
    long long N,R; // Removed unused variables count1 and max1 to clean up code
    cin>>s; 
    for (int i=0; i<s; i++) { 
        cin>> N>>R; 
        // Add a space between N and R in the output stream
        cout<< N << " " << R <<endl; 
    } 
    return 0; 
}

Key Notes (Tailored to Your C Knowledge)

  • In C, you'd use printf("%lld %lld\n", N, R); to explicitly add the space between values. C++'s cout works the same way—you need to include the space as a separate element in the stream (either as a string " " or a single character ' ').
  • I stripped out the unused count1 and max1 variables to keep the code tidy, but feel free to add them back if you need them for future logic.

Testing this with your input 1 5 6 will now output:

5 6

内容的提问来源于stack exchange,提问作者anil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:51:49