C语言经典弱智问题解法整理
<1>交换
//1)输入输出
#include<stdio.h>
int main(void){
int a, b;
scanf("%d %d", &a, &b);
printf("%d %d\n", b, a);
return 0;
}
//2)第三变量
#include<stdio.h>
int main(void){
int a, b, t;
scanf("%d %d", &a, &b);
t = a; a = b; b = t;
printf("%d %d\n", a, b);
return 0;
}
//3)加减法
#include<stdio.h>
int main(void){
int a, b;
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("%d %d\n", a, b);
return 0;
}
//4)乘除法
#include<stdio.h>
int main(void){
int a, b;
scanf("%d %d", &a, &b);
a = a*b;
b = a/b;
a = a/b;
printf("%d %d\n", a, b);
return 0;
}
//5)异或运算
#include<stdio.h>
int main(void){
int a, b;
scanf("%d %d", &a, &b);
a ^= b ^= a ^= b;
printf("%d %d\n", a, b);
return 0;
}
<2>两数相加
//1)加法运算
#include<stdio.h>
int main(){
int a, b;
scanf("%d %d",&a, &b);
printf("%d\n",a+b);
return 0;
}
//2)逻辑运算
#include <stdio.h>
int add(int a ,int b){
if(a==0) return b;
return add((a&b)<<1, (a^b));
}
int main(){
int a ,b;
scanf("%d %d",&a,&b);
printf("%d\n", add(a,b));
return 0;
}
<3>待更新