MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.
int LookForOne (int n , int x[]) {
int i;
for (i = 0; i < n; i++) {
if (x[i] == 0) {
return (TRUE);
}
}
return (FALSE);
}
int LookForOneMaybe(int n , int x[]) {
int i;
int res = -1;
for (i = 0; i < n; i++) {
res &= x[i];
}
if (res != 0) {
return (FALSE);
}
return (MAYBE);
}
int CheckForOne(int x[], int n) {
long int test = 1;
int i;
int temp = 1;
int retVal = 1;
for (i = 0; i < n; i++) {
temp = test;
test = test * x[i];
while (test > temp) {
test--;
}
printf("test = %d\n",test);
}
if ( test != 0 ) {
retVal = 0;
}
return retVal;
}
int LookForOne(int n , int x[]) {
int i;
unsigned int res = -1;
for (i = 0; i < n; i++) {
unsigned int temp = x[i];
temp |= temp>>1;
temp |= temp>>2;
temp |= temp>>4;
temp |= temp>>8;
temp |= temp>>16;
temp |= temp>>32; // depending on sizeof(int)*CHAR_BIT;
// Now, the LSB of temp is set iff x[i] <> 0
// We use a "full" AND for res,
// it's one machine instruction anyway,
// but we check the LSB after the loop
res &= temp;
}
if ( (res & 1) ) {
return (FALSE);
}
return (TRUE);
}
int LookForOneUnrolled(int n , int x[]) {
int i;
unsigned int res = -1;
for (i = 0; i < n; i += 6) {
unsigned int temp1 = x[i];
unsigned int temp2 = x[i+1];
unsigned int temp3 = x[i+2];
unsigned int temp4 = x[i+3];
unsigned int temp5 = x[i+4];
unsigned int temp6 = x[i+5];
temp1 |= temp1>>1;
temp2 |= temp2>>1;
temp3 |= temp3>>1;
temp4 |= temp4>>1;
temp5 |= temp5>>1;
temp6 |= temp6>>1;
temp1 |= temp1>>2;
temp2 |= temp2>>2;
temp3 |= temp3>>2;
temp4 |= temp4>>2;
temp5 |= temp5>>2;
temp6 |= temp6>>2;
temp1 |= temp1>>4;
temp2 |= temp2>>4;
temp3 |= temp3>>4;
temp4 |= temp4>>4;
temp5 |= temp5>>4;
temp6 |= temp6>>4;
temp1 |= temp1>>8;
temp2 |= temp2>>8;
temp3 |= temp3>>8;
temp4 |= temp4>>8;
temp5 |= temp5>>8;
temp6 |= temp6>>8;
temp1 |= temp1>>16;
temp2 |= temp2>>16;
temp3 |= temp3>>16;
temp4 |= temp4>>16;
temp5 |= temp5>>16;
temp6 |= temp6>>16;
temp1 |= temp1>>32;
temp2 |= temp2>>32;
temp3 |= temp3>>32;
temp4 |= temp4>>32;
temp5 |= temp5>>32;
temp6 |= temp6>>32;
temp1 &= temp2;
temp3 &= temp4;
temp5 &= temp6;
res &= temp1;
temp3 &= temp5;
res &= temp3;
}
if ( (res & 1) ) {
return (FALSE);
}
return (TRUE);
}
if (n == 0) return FALSE;
if (x[n-1] == 0) return TRUE;
int save = x[n-1];
x[n-1] = 0;
// ...
x[n-1] = save;
return RETVAL;
sl n, 2 ; n <<= 2
add n, x ; n += x
lab1: cmp x, n ;
jge lab2 ; if (x >= n) goto lab2
cmp 0, (x) ;
je lab3 ; if (0 == *x) goto lab3
add x, 4 ; x++
jmp lab1 ; goto lab1
lab2: ret 0 ; return 0
lab3: ret 1 ; return 1
int LookForOneWatchdog(int n, int x[]) {
int i;
if (n == 0) return (FALSE);
int watchdog = x[n-1];
int ret = FALSE;
for (i = 0; ; i++) {
if (x[i] == 0) {
ret = TRUE;
break;
}
}
x[n-1] = watchdog;
return ret;
}
int LookForOneWatchdog(int n, int x[]) {
int i;
if (n == 0) return (FALSE);
int watchdog = x[n-1];
x[n-1] = 0; // forgot this line
int ret = FALSE;
for (i = 0; ; i++) {
if (x[i] == 0) {
ret = TRUE;
break;
}
}
x[n-1] = watchdog;
return ret;
}
int LookForOneWatchdogOptimized(int n, int x[]) {
int i;
if (n == 0) return (FALSE);
if (x[n-1] == 0) return (TRUE);
int watchdog = x[n-1];
x[n-1] = 0;
int ret = FALSE;
for (i = 0; ; i++) {
if (x[i] == 0) {
break;
}
}
x[n-1] = watchdog;
return (i < (n - 1)) ? TRUE : FALSE;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
scoreUp challenge | 14 | 64 | |
Specific format | 21 | 207 | |
wordcount challenge | 11 | 134 | |
Problem to page | 4 | 93 |
Join the community of 500,000 technology professionals and ask your questions.