https://www.acmicpc.net/problem/9498
9498번: 시험 성적
시험 점수를 입력받아 90 ~ 100점은 A, 80 ~ 89점은 B, 70 ~ 79점은 C, 60 ~ 69점은 D, 나머지 점수는 F를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
1. if 문사용해서 푼코드
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int score = int.Parse(ReadLine());
if (score >= 90)
WriteLine("A");
else if (score >= 80)
WriteLine("B");
else if (score >= 70)
WriteLine("C");
else if (score >= 60)
WriteLine("D");
else
WriteLine("F");
}
}
}
2. switch문 사용 + Math.Truncate() 로 소수점 날리는 방법 사용
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=monkeychoi&logNo=220517384249
C# Math.Floor 와 Math.Truncate 의 차이
Math.Truncate 소수점 부분을 잘라낸다 그냥 날려버리는 것이다 그래서 양수나 음수 모두 Ma...
blog.naver.com
Math.Truncate
decimal Math.Truncate(decimal d)
double Math.Truncate(double d) //참고로 이렇게 뜰 때도 있다
visual스튜디오에 이렇게 뜸
그러면 매개변수로 당연히 소수점 decimal형식을 넣어줘야한다
이 코드에서 만약 input/10.0을 안하고 정수 input을 매개변수로 넣어주면 오류
그리고 decimal형식을 int score 에 넣기 때문에 (int)형변환 필요
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int input = Convert.ToInt32(ReadLine());
int score = (int)(Math.Truncate(input / 10.0) * 10);
//Truncate- 소수점 날리기
//Truncate는 declimal형식이므로 매개변수에 나누기 10을해서 decimal 형식으로 만들어야하고
//decimal이기 때문에 (int) - int로 형변환
string grade = "";
switch (score)
{
case 100: //
case 90: //조건 두개에 같은 출력
grade = "A";
break;
case 80:
grade = "B";
break;
case 70:
grade = "C";
break;
case 60:
grade = "D";
break;
default:
grade = "F";
break;
}
WriteLine(grade);
}
}
}
3. switch 식 사용
[C#] 반복문(if, switch식, do while, foreach)
if문 if 문에서 사용하는 조건식은 bool형식 if -> else if -> else if-> .... ... ... -> else switch문 switch (조건식) { case 상수 1: -> 조건식 == 상수1일 case (==이 생략됐다고 보자) //실행할 코드 break; -> break : 조건
zizh.tistory.com
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int score = (int)(Math.Truncate(int.Parse(ReadLine()) / 10.0) * 10);
string grade = score switch
{
100 => "A",
90 => "A",
80 => "B",
70 => "C",
60 => "D",
_ => "F"
};
WriteLine(grade);
}
}
}
https://www.acmicpc.net/problem/2753
2753번: 윤년
연도가 주어졌을 때, 윤년이면 1, 아니면 0을 출력하는 프로그램을 작성하시오. 윤년은 연도가 4의 배수이면서, 100의 배수가 아닐 때 또는 400의 배수일 때이다. 예를 들어, 2012년은 4의 배수이면서
www.acmicpc.net
1.
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int year = Convert.ToInt32(ReadLine());
if (year % 4 == 0)
{
if ((year % 100 != 0) || (year % 400 == 0))
{
WriteLine(1);
}
else //이거 안써서 틀림
WriteLine(0);
}
else
WriteLine(0);
}
}
}
2.
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int year = Convert.ToInt32(ReadLine());
// 문제조건 어긋날 경우 return으로 현재 메소드 종결
if (year < 1 || year > 4000) return;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
WriteLine(1);
else
WriteLine(0);
}
}
}
https://www.acmicpc.net/problem/2884
2884번: 알람 시계
상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다. 상근이는 모든 방법을 동원해보았지만,
www.acmicpc.net
시간계산의원리
1.
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
string[] s = ReadLine().Split();
int h = int.Parse(s[0]);
int m = int.Parse(s[1]);
if (h < 0 || h > 23 || m < 0 || m > 59) return;
m -= 45;
if (m < 0)
{
h -= 1;
//h -- 로 썼으면 더 좋았을텐데
m += 60;
}
if (h < 0)
{
h += 24;
}
WriteLine($"{h} {m}");
}
}
}
2. m -= 45 를 미리계산하지 않고 조건식에 합친느낌
using System;
public class Program {
public static void Main()
{
string[] a = Console.ReadLine().Split(' ');
int h = int.Parse(a[0]);
int m = int.Parse(a[1]);
if (h < 0 || h > 24 || m < 0 || m > 59) return;
if (m < 45)
{
h--;
m += 15;
if(h < 0)
{
h = 23;
}
}
else
{
m -= 45;
}
Console.WriteLine("{0} {1}", h, m);
}
}
https://www.acmicpc.net/problem/2525
2525번: 오븐 시계
첫째 줄에 종료되는 시각의 시와 분을 공백을 사이에 두고 출력한다. (단, 시는 0부터 23까지의 정수, 분은 0부터 59까지의 정수이다. 디지털 시계는 23시 59분에서 1분이 지나면 0시 0분이 된다.)
www.acmicpc.net
1. 내가푼거
추가되는 분을 굳이 h, m으로 나눠서
너무 지저분..
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
string[] input = ReadLine().Split();
int h = int.Parse(input[0]);
int m = int.Parse(input[1]);
int time = int.Parse(ReadLine());
int[] time_h_m = new int[2];
time_h_m[0] = time / 60;
time_h_m[1] = time % 60;
h += time_h_m[0];
m += time_h_m[1];
if (m >= 60)
{
m -= 60;
h++;
}
if (h >= 24)
{
h -= 24;
}
WriteLine($"{h} {m}");
}
}
}
2. 다른사람코드
while문으로
추가되는분을 기존 입력받은 분B에 추가하고
B가 60을넘길때마다 ..
만약 130분이 추가되어서
B-=60을해도 B>=60이기때문에
60미만이 나올때까지 반복
using System;
using System.Numerics;
namespace boj
{
class Program
{
static void Main(string[] args)
{
string [] str = Console.ReadLine().Split(' ');
int A = int.Parse(str[0]);
int B = int.Parse(str[1]);
int C = int.Parse(Console.ReadLine());
B += C;
while (B >= 60)
{
A++;
B -= 60;
}
if(A >= 24)
{
A -= 24;
}
Console.WriteLine(A + " " + B);
}
}
}