閱讀913 返回首頁    go 阿裏雲 go 技術社區[雲棲]


C++實現判斷輸入的數組是否是升序的程序

#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::vector;

//input elements for the vector class object v.
void input(vector<int> &v);

//display the elements.
void output(vector<int> v);

//return the bool value. If the elements are ascending, return 1,otherwise return 0.
bool isAscendingOrder(vector<int> v);

int main()
{

vector<int> v;

input(v);//Input elements.
if (isAscendingOrder(v))
cout << "The elements are ascending!" << endl;
else
cout << "The elements are not ascending!" << endl;
output(v);//output elements.

system("pause");
return 0;
}

void input(vector<int> &v){
int numbers;
int num;

cout << "How many numbers do you want to enter: ";
cin >> numbers;
cout << "Enter "<< numbers << " for a group of integer numbers:" << endl;
for (int i = 0; i < numbers; i++)
{
cin >> num;
v.push_back(num);
}
}//End input

//If it is ascending, return 1, otherwise return 0;
bool isAscendingOrder(vector<int> v)
{
for (int i = 0, j = 1; i < v.size() && j < v.size(); i++, j++)
{
if (v[i] > v[j])
return 0;
}
return 1;
}//End isAscendingOreder

//function output
void output(vector<int> v)
{
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}//End output 

最後更新:2017-04-02 18:14:51

  上一篇:go jQuery控製回車使表單內控件獲得焦點
  下一篇:go C++實現的一個簡單兩個大數相加程序!