[英]Extra line is coming as output in c++
int main() {
string n;
int number_of_test_cases;
cin>>number_of_test_cases;
for (int i = 0; i <= number_of_test_cases; i++) {
getline(cin,n);
for (int p = 0; p < n.size(); p++)
{
if (p%2==0){
cout<<n[p];
}
}cout<<" ";
for (int p = 0; p < n.size(); p++)
{
if (p%2!=0){
cout<<n[p];
}
}cout<<endl;
}return 0;}
This code first takes number of test cases as input number_of_test_cases,then it runs loop n times in which it first takes input string then it runs a loop to print even indexed characters then space then again runs a loop to print odd indexed characters then it prints a endline.此代码首先将测试用例的数量作为输入 number_of_test_cases,然后运行循环 n 次,其中首先获取输入字符串,然后运行循环以打印偶数索引字符,然后是空格,然后再次运行循环以打印奇数索引字符,然后打印一个端线。
Now the problem is that after giving number_of_test_cases as input it is printing a unwanted line.现在的问题是,在给出 number_of_test_cases 作为输入之后,它正在打印不需要的行。 So now i want to get rid of that extra line.所以现在我想摆脱那条额外的线。
This question is from hackerrank: https://www.hackerrank.com/challenges/30-review-loop/problem这个问题来自hackerrank: https://www.hackerrank.com/challenges/30-review-loop/problem
for input 2 Hacker Rank its giving output对于输入 2 Hacker Rank 其给出 output
Hce akr
Rn ak
instead of代替
Hce akr
Rn ak
Use cin.ignore()
after scanning the integer before using getline function.在使用 getline function 之前扫描 integer 后使用cin.ignore()
。 Also the condition in your for loop should be i < j
.你的 for 循环中的条件也应该是i < j
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.