Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;工作_C++_String_Palindrome - Fatal编程技术网

C++ 如何在C++;工作

C++ 如何在C++;工作,c++,string,palindrome,C++,String,Palindrome,我正在努力解决这个问题 我用字符串实现它。这是我的代码片段 string s,ss; // s and ss both contains integer input. while(s <= ss ) //while( s<=ss && s.size() <= ss.size()) { int i = inc, j = dec; // inc and dec are middle values. both equal if odd else diff

我正在努力解决这个问题

我用字符串实现它。这是我的代码片段

string s,ss;
// s and ss both contains integer input.

while(s <= ss )
//while( s<=ss && s.size() <= ss.size())
{
    int i = inc, j = dec;   // inc and dec are middle values. both equal if odd else different

    while((s[j]-'0')==9 && i < len && j>=0){
        // for cases like 999
        s[i] = s[j] = '0';
        i++;
        j--;
    }
    if(j<0){
        s = "1" + s;
        int l = s[len-1] - '0';
        l++;
        //cout<<l<<"\n";
        s[len] = (l + '0');
    }
    else{
        int l = s[j] - '0';
        l++;
        s[i] = s[j] = (l+'0');
    }

    if(s <= ss)
        cout<<"out in wild "<<s<<" and "<<ss<<"\n";
}

cout<<s<<endl;
字符串s,ss;
//s和ss都包含整数输入。

而(s您比较的是字符串,而不是数字,因此“101”小于“99”(因为“1”<“9”),例如


您将字符串与进行比较,而不是与数字进行比较,因此“101”小于“99”(因为“1”小于“9”),例如

虽然s和ss是字符串变量,但它们是逐字符比较的

如果您提到的是:
s=“101”
&
ss=“99”
,它将直接检查每个字符串中的第一个字符,并作为
'1'
以s虽然s和ss是字符串变量,但它们是逐字符比较的


如果您提到的是:
s=“101”
&
ss=“99”
,它将直接检查每个字符串中的第一个字符,并作为
'1'
以s由于s按字典顺序与ss进行比较,我建议您将尾部的一个字符与头部的一个字符进行比较(一个接一个直到你到达中间)来解决这个问题。

由于s是按字母顺序与ss进行比较的,我建议你将尾部的一个字符与头部的一个字符进行比较(一个接一个直到你到达中间)来解决这个问题。

它使用了字母顺序比较,这会告诉你。
“101”
是“少”比
“99”
。字符串比较是字典式的,所以“101”比“99”小,因为“1”在“9”之前。一个建议是,您可以通过使用一些有意义的变量名来改进代码,而不是使用
s
l
ss
。它使用字典式比较,这会告诉您。
“101”
是“更少”字符串比较是字典式的,因此“101”小于“99”,因为“1”在“9”之前。一个建议是,您可以通过使用一些有意义的变量名来改进代码,而不是使用
s
l
ss
。感谢您以如此简单的方式解释。我认为字符串“1”中的变量名是49,“9”中的变量名是(48+9)但是我明白了。请给我一些链接。谢谢你用这么简单的方式解释。我认为字符串“1”是49,而“9”是(48+9)但是我明白了。请给我一些链接。这个问题不能用int、double或任何其他变量来实现,因为输入限制是1000000位。我需要使用数组或字符串。@AnonHost请看我的第二个注释,我添加了一个链接来比较包含数字的字符串。这个问题不能用int、double或任何其他变量b来实现e因为输入限制为1000000位。我需要使用数组或字符串。@AnonHost看到我的第二个注释,我添加了一个链接来比较包含数字的字符串
int main(){
    std::string s = "99";
    std::string ss = "101";
    
    std::cout << std::boolalpha << (s <= ss);  
}
/* Returns 1 if the integer represented by s1 > the integer represented by s2
*  Returns -1 if the integer represented by s1 < the integer represented by s2
*  Return 0 is both are equals
*
*  s1 and s2 must be strings representing positive integers without trailing 0
*/
int compare(const std::string& s1, const std::string& s2)
{
  if(s1.size() > s2.size())
      return 1;
  if(s2.size() > s1.size())
      return -1;
      
  for(std::size_t i = 0 ; i < s1.size() ; ++i)
  {
    if(s1[i] - '0' < s2[i] - '0')
      return 1;
    if(s2[i] - '0' < s1[i] - '0')
      return -1;
  }
          
  return 0;
}