C programming if statement

2011-09-08 10:24 am

if((i1!=i2)){
if(i1!=i3){
if(i3!=i2){}
}
}
如果有10個variable,我想10個variable都唔等於,咁我就要整45個command +_+
點樣可以將i1 i2 i3 每一個都唔等如既command簡化?

回答 (1)

2011-09-08 1:15 pm
✔ 最佳答案
Put the variables in an array. Write a function to check if any two variables are equal.

Samples codes are in C#. You can apply similar logic in C.

bool checkEqual(int[] A)
{
bool equal = false;
int len = A.Length - 1;
for (int j = 0; j < len; j++)
{
for (int i = j + 1; i < len; i++)
{
if (A[j] == A[i])
return true;
}
}
return equal;
}

void main()
{
int[] A = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
if (!checkEqual(A))
{
// your codes here
}
}


收錄日期: 2021-04-24 09:42:00
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20110908000051KK00085

檢視 Wayback Machine 備份