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:
- No explicit space between output values: When you write
cout<< N << R <<endl;,coutstreams the values back-to-back without any separation—this is exactly like doingprintf("%d%d", N, R);in C, which would also spit out "56" instead of "5 6". - 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 ofNandRwould 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++'scoutworks 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
count1andmax1variables 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
相关产品推荐
相关产品推荐

