我得到了這個非常簡單的項目,但是我陷入了一個簡單的問題。計算數組中的不同元素
用戶輸入一個數不大於99且小於1 000 000
我應該得到的該號碼數字多少次,每次一個發生
如號碼: 112233 = 6位數字和3個不同的數字
我能夠做第一部分,但是我的第二部分是不工作的。
這裏是不工作的一部分:
int getDiff(int CLP, int Num)
{
int counter = 0, i = 0, j = 0;
for (int x = CLP, i = 0; x >= 1; x /= 10, i++) {
PlateNumberArray[i] = x % 10;
cout << endl
<< "Test===>" << PlateNumberArray[i] << endl;
} // end of "x = CLP" for loop
while (i < Num) {
j = 0;
while (j <= Num) {
if (PlateNumberArray[i] == PlateNumberArray[j])
NumberCounter[i]++;
j++;
} //end of while(j <= i)
i++;
} //end of while(i < Num)
for (int i = 0; i < Num; i++)
counter += NumberCounter[i];
return counter;
}
舉例來說,如果我輸入的是112233的返回值應該是3,但是我得到12
如果輸入1122的返回值應該是2,但我得到8
這裏是整個程序到目前爲止,我已經寫了:
/*====================================================================================
Headers and namespace
======================================================================================*/
#include <iostream>
using namespace std;
/*====================================================================================
Prototypes list
======================================================================================*/
int getNum(int); //This function checks how many digits there are in a plate number
int getDiff(int, int); //This function checks how many different numbers are there in the plate number
/*====================================================================================
Global variables list
======================================================================================*/
int PlateNumberArray[6];
int NumberCounter[6];
/*====================================================================================
main Function
======================================================================================*/
int main()
{
//Declaring variables
int CLP;
//End of Vriables Declration
cout << endl
<< "=============================" << endl;
cout << "Enter your vehicle's plate number" << endl;
do {
cin >> CLP;
if (CLP >= 1000000)
cout << "Plate number can be no longer than 6 digits, please re-enter" << endl;
else if (CLP < 100 && CLP >= 0)
cout << "Plate number can not be less than 3 digits, please re-eneter" << endl;
else if (CLP < 0)
cout << "Plate number can not be a negative, please re-eneter" << endl;
} while (CLP >= 1000000 || CLP < 100);
int Num = getNum(CLP);
cout << getDiff(CLP, Num);
return 0;
}
/*====================================================================================
getNum Function
======================================================================================*/
int getNum(int CLP)
{
//Declaring variables
int Num;
//End of Vriables Declration
if (CLP > 99999)
Num = 6;
else if (CLP > 9999)
Num = 5;
else if (CLP > 999)
Num = 4;
else if (CLP > 99)
Num = 3;
return Num;
}
/*====================================================================================
getDiff Function
======================================================================================*/
int getDiff(int CLP, int Num)
{
int counter = 0, i = 0, j = 0;
for (int x = CLP, i = 0; x >= 1; x /= 10, i++) {
PlateNumberArray[i] = x % 10;
cout << endl
<< "Test===>" << PlateNumberArray[i] << endl;
} // end of "x = CLP" for loop
while (i < Num) {
j = 0;
while (j <= Num) {
if (PlateNumberArray[i] == PlateNumberArray[j])
NumberCounter[i]++;
j++;
} //end of while(j <= i)
i++;
} //end of while(i < Num)
for (int i = 0; i < Num; i++)
counter += NumberCounter[i];
return counter;
}
你有一些UB當j == 6如果(PlateNumberArray [i] == PlateNumberArray [j])'有效索引是0 .. 5. – drescherjm