C++求升序数个数

来源:百度知道 编辑:UC知道 时间:2024/05/15 05:50:25
调用随机函数rand(),产生10个取值小于1000的整数,统计其中升序数、降序数的个数输出到屏幕。
哪位帮忙,谢谢了,急用,在线等
升序数指高位数字不大于低位数字的数,例如3,456,446,22;降序数指高位数字大于低位数字的数,例如54,831。而263,834之类则即非升序数也非降序数。
谢谢大侠帮忙,我初学

具体判断的代码就是一下的了,
置随机数种子:
srand( unsigned( time( NULL ) ) );
随机数函数rand()返回一个int型整数。
框架楼主自己写吧。

bool
ifUpNumber( int x )
{
. while( x > 0 )
. {
. int t = x % 10;
. x /= 10;
. if( t >= x % 10 )
. return false;
. }
. return true;
}
bool
ifDownNumber( int x )
{
. while( x > 0 )
. {
. int t = x % 10;
. x /= 10;
. if( t < x % 10 )
. return false;
. }
. return true;
}

如下;
#include<iostream>
#include<time.h>
using namespace std;

int main()
{
int a[10];
srand( (int)time(0) );

for( int i = 0; i < 10; i++ )
a[i] = rand( )%1000;

cout << "10个数分别为:";
for( int i = 0; i < 10; i++ )
cout << a[i] << " ";

int Snum = 0, Jnu