C++ 我如何检查大量数据是否具有峰值外观?
如果从左侧开始的数字按升序排序,则数字具有峰值外观 按数字顺序排列,从该数字开始,按降序排列 这是我天真的做法,但我知道这不是100%正确。我的函数不返回任何内容,而且效率也不高C++ 我如何检查大量数据是否具有峰值外观?,c++,C++,如果从左侧开始的数字按升序排序,则数字具有峰值外观 按数字顺序排列,从该数字开始,按降序排列 这是我天真的做法,但我知道这不是100%正确。我的函数不返回任何内容,而且效率也不高 int peak_appearance(int n) { /** trivial case */ if (n<100) return false; int current = n%10; int previous = (n/10)%10; while
int peak_appearance(int n)
{
/** trivial case */
if (n<100)
return false;
int current = n%10;
int previous = (n/10)%10;
while (n!=0 and current < previous) // starting from the right, checking if we have ascending order
{
n = n/10;
current = n%10;
previous = (n/10)%10;
}
if (n==0) // checking if we have digits on the right side of the peak digit
return false;
while (n!=0 and current > previous) // checking descending order
{
n = n/10;
current = n%10;
previous = (n/10)%10;
}
if (n!=0)
return false;
else
return true;
}
int-peak\u外观(int-n)
{
/**琐事*/
if(n previous)//检查降序
{
n=n/10;
电流=n%10;
先前=(n/10)%10;
}
如果(n!=0)
返回false;
其他的
返回true;
}
你所说的“只是不返回任何东西”是什么意思?看起来它在每个分支中都返回了一些东西(即使在返回int
的函数中returnfalse
并不明显)。你的问题是什么?关于你的小案例,似乎一个1或2位数的数字可以被认为具有峰值外观。函数返回类型为int
,但返回的是bool
。很好,您的true
和false
将分别转换为1
和0
。为什么不使用数字的字符串表示形式呢?更简单,请参见。实际上,我们不知道您是如何使用此函数的。我们不知道您的输入是什么,也不知道您的预期输出是什么。我们不知道你的实际产出是多少。调用此函数的main()
函数将非常有用。@NikosM。这会容易得多,但我不太熟悉数字的字符串表示。