SELECT
SUM(IF(fruit='Apel', 1,0)) AS apelfruit,
SUM(IF(fruit='Orange', 1,0)) AS orangfruit,
SUM(IF(fruit='Manggo', 1,0)) AS manggofruit
FROM fruit_table WHERE fruit IN ('Apel', 'Orange', 'Manggo')
;
如何在不使用第三个变量的情况下交换它们?输出应该像
*a=10
*b=20
答案 0 :(得分:0)
不确定面试官是否在寻找其他东西的异或,但似乎您可以简单地使用+,-和x。如果a较大或为负也应该起作用。
*a+=*b
*b-=*a
*b=*b x -1
*a-=*b
在您的示例中,这将给我们:
*a+=*b --> *a = 30
*b-=*a --> *b = -10
*b=*b x -1 --> *b = 10
*a-=*b --> *a = 20
答案 1 :(得分:0)
这是执行此操作的简单代码:
#include <stdio.h>
#include <stdlib.h>
void usingXOR(int** x, int** y){
unsigned long long a = (unsigned long long)*x;
unsigned long long b = (unsigned long long)*y;
a = a^b;
b = a^b;
a = a^b;
*x = (int*)a;
*y = (int*)b;
}
void main(){
int x=5;
int y=10;
int* a = &x;
int* b = &y;
//If you only want to swap the values the pointers are pointing to
//Here the addresses the pointers are holding dont get swapped
(*a) = (*a)+(*b);
(*b) = (*a)-(*b);
(*a) = (*a)-(*b);
//If you want to swap addresses in the pointers
//printf("Before swap address a: %p\n", a);
//printf("Before swap address b: %p\n", b);
//usingXOR(&a,&b);
printf("a: %d\n", *a);
printf("b: %d\n", *b);
//printf("After swap address a: %p\n", a);
//printf("After swap address b: %p\n", b);
}