int elements[5] = {1,2,5,1,2}
for(int i=0; i< 5 -1 ; i++)
{
if(elements[i] ==elements[i+1] ){
cout << "equal" << endl;
}else{
cout << "not equal" << endl;
}
}
如何改善这个?我无法将索引0与索引5进行比较;
答案 0 :(得分:1)
尝试以下
int elements[5] = {1,2,5,1,2}
for(int i=0; i<5; i++)
{
for(int j=i+1; j<5; j++)
{
if(elements[i] ==elements[j] ){
cout << "equal" << endl;
}else{
cout << "not equal" << endl;
}
}
}