오브의 빛나는 별

Code Up(코드업) 1056번~1058번 (C언어) 본문

코드업(C언어)

Code Up(코드업) 1056번~1058번 (C언어)

오브의 별 2022. 1. 20. 17:41
반응형

[1056] 참/거짓이 서로 다를 때에만 참 출력하기

<문제>

두 가지의 참(1) 또는 거짓(0)이 입력될 때,
참/거짓이 서로 다를 때에만 참을 출력하는 프로그램을 작성해보자.

<정답>

#include <stdio.h>

int main(void)
{
	int a, b;
	
	scanf("%d %d", &a, &b);
	if((a&&!b)||(!a&&b))
		printf("1");
	else
		printf("0");
	
	return 0;
}
#include <stdio.h>
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", a^b);
    return 0;
}

XOR(exclusive or, 배타적 논리합)연산: ^

 

[1057] 참/거짓이 서로 같은 때에만 참 출력하기

<문제>

두 개의 참(1) 또는 거짓(0)이 입력될 때,
참/거짓이 서로 같을 때에만 참이 계산되는 프로그램을 작성해보자.

<정답>

#include <stdio.h>

int main(void)
{
	int a, b;
	
	scanf("%d %d", &a, &b);
	if((!a==!b)&&(a==b))
		printf("1");
	else
		printf("0");
	
	return 0;
}
#include <stdio.h>
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", !(a^b));
    return 0;
}

 

[1058] 둘 다 거짓일 경우만 참 출력하기

<문제>

두 개의 참(1) 또는 거짓(0)이 입력될 때,
모두 거짓일 때에만 참이 계산되는 프로그램을 작성해보자.

<정답>

#include <stdio.h>

int main(void)
{
	int a, b;
	
	scanf("%d %d", &a, &b);
	if(!(a||b))
		printf("1");
	else
		printf("0");
	
	return 0;
}
#include <stdio.h>
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", !(a||b));
    return 0;
}
반응형