输出 Befor 给出字符串的输入以查找回文
Output Befor giving the input of string for finding palindrome
此代码在输入测试用例的值后立即给出输出 YES。 代码:用于字母数字回文
int main() {
int t;
cin>>t;
while(t--){
string s;
int count = 0,size = 0;
getline(cin,s);
cout<<s<<endl;
s.erase(remove_if(s.begin(),s.end(),not1(ptr_fun((int(*)(int))isalnum))), s.end());
for(int i=0;i<=s.size()/2;i++){
size++;
if(tolower(s[i])==tolower(s[s.size()-i-1])){
count++;
}
else
break;
}
if (count==size)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
我得到的输出是 YES,没有给出任何字符串输入
For Input:
2
I am :IronnorI Ma, i
Ab?/Ba
Your Output is:
YES
I am :IronnorI Ma, i
YES
此代码在输入测试值后给出输出 YES 箱。我得到的输出是 YES,没有给出任何字符串输入
您的问题就在这里:
/* code */
cin>>t; -----------> std::cin
while(t--)
{
string s;
int count = 0,size = 0;
getline(cin,s); ------------> std::getline()
/* remaining code */
使用类似std::cin
的内容进行读取会在输入流中保留换行符。当控制流达到std::getline()
时,换行符将被丢弃,但输入将立即停止。这会导致std::getline()
尝试读取新行并跳过输入。
FIX:从空格分隔的输入切换到换行分隔的输入时,您希望通过执行std::cin.ignore()
来清除输入流中的所有换行
符固定代码应为:https://www.ideone.com/ucDa7i#stdin
#include <iostream>
#include <string>
#include <limits>
#include <algorithm>
int main()
{
int t;
std::cin >> t;
// fix
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
while(t--)
{
std::string s;
int count = 0,size = 0;
getline(std::cin,s);
/* remaining code */
}
相关文章:
- 查找大输入的位数总和时出错?
- 查找匹配的输入,并返回匹配的输入和不匹配的输入
- C++程序来查找输入数字的最小/最大值
- 从C++的用户输入中查找最小值
- 拆分字符串输入并查找字符是否有效
- 为什么我的阶乘数查找器返回以C++输入的数字?(已编辑)
- 从输入 1 查找输入 2 中字符的位置
- 查找两个二进制输入 C++ 的 xNOR 位
- 这个查找查询是否出错,因为我输入的数字太大
- 如何从C++的输入中查找字符串并将其替换为另一个字符串
- 无法打开输入文件 Poco.obj 或查找 PocoFoundationd.lib
- 在用户输入数组中查找元音
- 如何从数字输入文件中查找最大数字
- 如何查找输入的双精度是否为0.05的倍数
- 如何创建一个以键为输入并在C++std:map中查找值的模板函数
- 在C++中使用 for 循环从用户输入中查找最大和最小整数
- 如何查找当前输入语言
- 如何从输入文件中查找未知数量的行和列
- 通过用户输入查找c++中的最大值和最小值
- 根据用户输入查找质数的位置