gpt4 book ai didi

c++ - bool 函数返回意外值

转载 作者:行者123 更新时间:2023-11-30 02:14:33 26 4
gpt4 key购买 nike

isValid("23:15") 在应该返回 1 时返回 0

bool isValid(string s){
int pos = s.find(":");
if(s.length() < 4 || s.length() > 5)
return false;
else if(s.length() == 5)
{
if(s[0] > 2)
return false;
}
if(s[pos + 1] > 5 )
{
return false;
}
return true;
}

实际输出= 0

最佳答案

我猜你把数字和数字搞混了。

if(s[0] > 2)

应该是

if (s[0] > '2')

if(s[pos + 1] > 5 )

应该是

if (s[pos + 1] > '5')

您还需要考虑如果 s 不包含冒号会发生什么情况。

关于c++ - bool 函数返回意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com