Algorithm/goorm

goorm 3과 5의 배수, 시험성적 평균과 등급 구하기, 홀짝 판별

마띠(쥔장) 2020. 8. 12. 03:21

 

 

#include <iostream>
#include <string>
using namespace std;

string fun(int n) {
	if (n % 2 == 0) {
		return "even";
	}
	else {
		return "odd";
	}
}

int main(void) {
	int n;
	cin >> n;
	cout << fun(n);
	return 0;
}

 

#include <iostream>
using namespace std;
int main(void){
	int n; // 1000이하의 자연수
	cin >> n;
	
	int sum = 0; // 3과 5의 배수의 합
	int ex = 0; // 15의 배수의 합
	for(int i = 0; i <= n; i++){
		if(i%3==0||i%5==0){
			sum += i;
		}
		else if(i%15==0){
			ex += i;
		}
	}
	cout<<sum-ex;
}

 

#include <iostream>
using namespace std;
int main(void) {
	int kor = 0;
	int math = 0;
	int eng = 0;
	cin >> kor >> math >> eng;
	double avg = (kor + math + eng)/3.0;
	cout<<fixed;
	cout.precision(2);
	if (avg >= 90) {
		cout<<avg<<" "<< 'A';
	}
	else if (avg >= 80 && avg < 90) {
		cout<<avg<<" " << 'B';
	}
	else if (avg >= 70 && avg < 80) {
		cout<<avg<<" " << 'C';
	}
	else if (avg >= 60 && avg < 70) {
		cout <<avg<<" "<< 'D';
	}
	else
		cout<<avg<<" " << 'F';

}

 

다 레벨 1에 해당하는 문제들이어서 금방 금방 풀었지만, 굳이 따지자면 마지막 문제에서 시간을 썼습니다.

cout<<fixed;
cout.precision(2);

double 형 변수인 avg는 세 과목의 평균을 저장합니다.

첫 번째 test case인 '100 100 98'에서는 잘 출력되었습니다만, '100 100 100'일 때는 '100.00'이 아닌, 그냥 '100'으로 출력되었습니다. 그래서 위의 코드를 통해 어느 상황에서든지 소수점 둘째자리까지 출력될 수 있도록 하였습니다. 더 쉽게 풀 수 있는 법이 분명 있겠지만 'fixed'는 처음 써봐서 ㅎㅎ 

암튼 오늘부터 3개씩 풀기로!

728x90