0
我正在試圖找到一種方法來編寫一個程序,該程序將採用「n」個術語並查明它是否上升。如果列表不是升序,找到違反此訂單的第一個號碼並打印索引位置i,然後找到違反它的最後一個號碼。查找第一個和最後一個按升序排列的數字
例如:2 3 4 2 3 2 3 4 1 - 的輸出應爲:元素#4被降& &元素#9是LAST descen
編輯:我已經解決了如何找到的第一個數字只是沒有最後一個號碼
這裏是我的代碼:
#include <iostream>
using namespace std;
int main(){
int num, inputNum, i;
int nextNum=0;
int violator1=0, violator2=0;
int elementNum1=0, flag1=0;
int elementNum2=0, flag2=0;
cout << "Enter how much numbers you want to enter in list: ";
cin >> num;
for (i = 0; i < num; i++){
cout << "Please enter a number: ";
cin >> inputNum;
if (inputNum >= nextNum){
nextNum = inputNum;
}
else {
flag1 = 1; /*The flag is set to '0' when the next number in list has descended*/
violator1 = inputNum;
if (elementNum1 == 0) /*This condition will only run once because elementNum == 0 only once*/
elementNum1 = i+1;
}
}
/*The flag value is determined within the loop to find the descending value*/
if (flag == 0){ /*If the flag is UNchanged ie = 1*/
cout << "These numbers are in ascending order." << endl;
}
else {
cout << "These numbers are NOT in ascending order." << endl;
cout << "Element Number '" << elementNum << "' was the violating number." << endl;
}
return 0;
}
所以它工作?你是否與我們分享了一個好的解決方案,或者你有問題嗎? –
標誌和elementNum在程序中聲明在哪裏? – vishal