코딩/BOJ & 알고스팟

[BOJ 14614] Calculate!

hoon222y 2017. 5. 30. 14:27

https://www.acmicpc.net/problem/14614


XOR연산에 관련된 문제이다.


핵심은 비트연산은 && ,|| 가 아니라 &,|을 쓴다는 것이다.!!!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
 
using namespace std;
 
int main(){
    int a,b;
    string c;
    scanf("%d%d" , &a,&b);
    cin >>c;
    int len = c.length();
    
    
    int ans = ((~a) & b) | (a & (~b));
    
    if((c[len-1]-'0')%2 == 0){
        printf("%d\n" , a);
    }else{
        printf("%d\n" , ans);
    }
    
    return 0;
}
cs


와!!!!!!!!!!!!!!!!! XOR연산은 그냥 a^b 로 하면된다니!!!!!!!


미쳐!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
 
using namespace std;
 
int main(){
    int a,b;
    string c;
    scanf("%d%d" , &a,&b);
    cin >>c;
    int len = c.length();
    
    int ans = a^b;
    //int ans = ((~a) & b) | (a & (~b));
    
    if((c[len-1]-'0')%2 == 0){
        printf("%d\n" , a);
    }else{
        printf("%d\n" , ans);
    }
    return 0;
}
cs