https://www.acmicpc.net/problem/10950
10950번: A+B - 3
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
using System;
using System.Diagnostics;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int input = Convert.ToInt32(ReadLine());
for (int i = 0; i < input; i++)
{
string[] s = ReadLine().Split();
int a = int.Parse(s[0]), b = int.Parse(s[1]);
WriteLine(a + b);
}
}
}
}
https://www.acmicpc.net/problem/8393
8393번: 합
n이 주어졌을 때, 1부터 n까지 합을 구하는 프로그램을 작성하시오.
www.acmicpc.net
static void Main(string[] args)
{
int input = Convert.ToInt32(ReadLine());
int sum = 0;
for (int i =1; i<=input; i++)
{
sum += i;
}
WriteLine(sum);
}
https://www.acmicpc.net/status?user_id=zizh121&problem_id=25304&from_mine=1
채점 현황
www.acmicpc.net
static void Main(string[] args)
{
int totalPrice = int.Parse(ReadLine());
int n = int.Parse(ReadLine());
int comparePrice = 0;
for(int i = 1; i<= n; i++)
{
string[] s = ReadLine().Split();
int a = int.Parse(s[0]);
int b = int.Parse(s[1]);
comparePrice += a * b;
}
if (totalPrice == comparePrice) WriteLine("Yes");
else WriteLine("No");
}
https://www.acmicpc.net/problem/15552
15552번: 빠른 A+B
첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는 각각 두 정수 A와 B가 주어진다. A와 B는 1 이상, 1,000 이하이다.
www.acmicpc.net
StringBuilder를 사용할수 있는가?
=> 반복적으로 Console.WriteLine()을 사용할 경우 고려
=> string을 많이 쓰게 될 경우 고려
Console.WriteLine()만 사용하는 대신 반복되는 출력을 1초안에 할 수 있냐는 성는문제인데
C#은 아래와 같이 힌트가 있고
C#
StreamReader로 읽고, StringBuilder로 출력을 모아 놓았다가 그 String을 Console.WriteLine하는 방법이 있습니다. BufferedStream과 StringWriter로 조금 더 향상시킬 수 있는 것 같으나 자세한 것은 다른 분의 답변을 기다리겠습니다.
더알아봐야겠지만 블로그 풀이 방법은 StringBuilder 사용이다
기존의 방식대로 string을 사용해서 출력을 하게되면
무조건 시간초과가 발생하는 문제이다.
string은 사용하면 할수록 처리 속도가 느려지게 된다.
이유를 설명하려면 다소 복잡한데...
약간 간략하게 압축해서 설명하자면,
int나 float는 값형식인 것에 비해서, string 형식은 참조형식이다.
또한, string 변수를 변형하거나 바꾸는 등의 작업을 수행하면
저장되었던 값이 바뀌는 것이 아니라
새로운 저장공간에 새로운 string이 생성되는 방식이다.
그러다보니 string을 많이쓰면 쓸수록 공간이 낭비되고 속도가 느려진다.
c#에서는 이를 방지하기 위해 StringBuilder라는 것이 존재한다.
먼저, StringBuilder를 사용하려면
using System.Text; 를 적어줘야만 한다.
스트링빌더에 대해서 간단히 설명하면
문자열을 한번에 통합적으로 관리한다고 생각하면 된다.
Append() : 줄바꿈 x (WriteLine)
AppendLine() : 줄바꿈 (WriteLine())
메소드를 사용해서 문자열을 담아둘 수 있다.
출력할 때는 ToString()으로 뽑아낸다.
그리고 아직 배열문제에 진입하지는 않았지만,
효과적으로 문제를 풀기위해서 어쩔 수 없이 배열을 사용했다.
(참고 : https://coding-of-today.tistory.com/81)
using System;
// StringBuilder를 사용하기 위해서.
using System.Text;
namespace for문4
{
class Program
{
static void Main(string[] args)
{
// 스트링빌더 answer 선언
StringBuilder answer = new StringBuilder();
// 테스트케이스 개수 입력 받음
int t = int.Parse(Console.ReadLine());
// 더하기 때 사용할 int를 배열로 생성함
int[] a = new int[t];
int[] b = new int[t];
for (int i = 0; i < t; i++)
{
// 숫자를 입력받고 공백을 기준으로 나눠 담을 수 있게 문자열 배열을 선언한다.
string[] s = Console.ReadLine().Split();
// 입력받은 문자를 int형으로 변환해서 저장한다.
a[i] = int.Parse(s[0]);
b[i] = int.Parse(s[1]);
// 각 숫자를 더한뒤 문자열로 변환하고 answer에 집어 넣음
answer.AppendLine((a[i] + b[i]).ToString());
}
// 출력
Console.WriteLine(answer.ToString());
}
}
}
https://www.acmicpc.net/problem/11021
11021번: A+B - 7
각 테스트 케이스마다 "Case #x: "를 출력한 다음, A+B를 출력한다. 테스트 케이스 번호는 1부터 시작한다.
www.acmicpc.net
static void Main(string[] args)
{
int count = int.Parse(ReadLine());
for (int i = 1; i <= count; i++)
{
string[] s = ReadLine().Split();
int a = Convert.ToInt32(s[0]);
int b = Convert.ToInt32(s[1]);
WriteLine("Case #{0}: {1}", i, a + b);
}
}
https://www.acmicpc.net/problem/2438
2438번: 별 찍기 - 1
첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제
www.acmicpc.net
static void Main(string[] args)
{
int num = int.Parse(ReadLine());
for(int i = 1; i<=num; i++)
{
for(int j =1; j<=i; j++)
{
Write("*");
}
WriteLine();
}
}
https://www.acmicpc.net/problem/2439
2439번: 별 찍기 - 2
첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제 하지만, 오른쪽을 기준으로 정렬한 별(예제 참고)을 출력하시오.
www.acmicpc.net
좀 고민을 함 이상하게 푼줄알았는데 다른 코드들도 비슷하네
for { ............①
for {} .....②
+
for {} .....③
}
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int num = int.Parse(ReadLine());
string s = " ";
for(int i = 1; i<=num; i++)
{
for(int k = num-1; k>=i; k--)
{
Write(" ");
}
for (int j = 1; j <= i; j++)
{
Write("*");
}
WriteLine();
}
}
}
}
https://www.acmicpc.net/problem/10951
10951번: A+B - 4
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
EOF에 대해 알아보는 문제
: End Of File
간단하게 파일의 끝을 확인할 수 있는가를 묻는다
아래 코드처럼 간략하게 if문으로 input에 입력값이 없어 null상태가 되면 무한루프를 빠져나갈 수 있게 한다는데
EOF가정확히뭔지.. 저걸로 eof구현끝?
static void Main()
{
while (true)
{
string input = Console.ReadLine();
if (input == null) break;
string[] AB = input.Split();
int a = int.Parse(AB[0];
int b = int.Parse(AB[1]);
Console.WriteLine(a + b);
}
}
https://www.acmicpc.net/problem/1110
1110번: 더하기 사이클
0보다 크거나 같고, 99보다 작거나 같은 정수가 주어질 때 다음과 같은 연산을 할 수 있다. 먼저 주어진 수가 10보다 작다면 앞에 0을 붙여 두 자리 수로 만들고, 각 자리의 숫자를 더한다. 그 다음,
www.acmicpc.net
이상한 변수 막 집어넣고 while조건도 다르게 하다가 결국은 틀린 문제..
최종적으로 값 비교해야하니까 copyinput에 값 복사하고 input은 그대로,copyinput값 가지고 while문에서 count 1회 증가하면서 연산수행
연산 후 input == copyinput이면 break
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int input = int.Parse(ReadLine());
int copyInput = input;
int count = 0;
while (true)
{
count += 1;
int a = copyInput / 10;
int b = copyInput % 10;
int sum = a + b;
copyInput = b * 10 + sum % 10;
if(input == copyInput)
{
break;
}
}
WriteLine(count);
}
}
}
https://www.acmicpc.net/problem/10950
10950번: A+B - 3
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
using System;
using System.Diagnostics;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int input = Convert.ToInt32(ReadLine());
for (int i = 0; i < input; i++)
{
string[] s = ReadLine().Split();
int a = int.Parse(s[0]), b = int.Parse(s[1]);
WriteLine(a + b);
}
}
}
}
https://www.acmicpc.net/problem/8393
8393번: 합
n이 주어졌을 때, 1부터 n까지 합을 구하는 프로그램을 작성하시오.
www.acmicpc.net
static void Main(string[] args)
{
int input = Convert.ToInt32(ReadLine());
int sum = 0;
for (int i =1; i<=input; i++)
{
sum += i;
}
WriteLine(sum);
}
https://www.acmicpc.net/status?user_id=zizh121&problem_id=25304&from_mine=1
채점 현황
www.acmicpc.net
static void Main(string[] args)
{
int totalPrice = int.Parse(ReadLine());
int n = int.Parse(ReadLine());
int comparePrice = 0;
for(int i = 1; i<= n; i++)
{
string[] s = ReadLine().Split();
int a = int.Parse(s[0]);
int b = int.Parse(s[1]);
comparePrice += a * b;
}
if (totalPrice == comparePrice) WriteLine("Yes");
else WriteLine("No");
}
https://www.acmicpc.net/problem/15552
15552번: 빠른 A+B
첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는 각각 두 정수 A와 B가 주어진다. A와 B는 1 이상, 1,000 이하이다.
www.acmicpc.net
StringBuilder를 사용할수 있는가?
=> 반복적으로 Console.WriteLine()을 사용할 경우 고려
=> string을 많이 쓰게 될 경우 고려
Console.WriteLine()만 사용하는 대신 반복되는 출력을 1초안에 할 수 있냐는 성는문제인데
C#은 아래와 같이 힌트가 있고
C#
StreamReader로 읽고, StringBuilder로 출력을 모아 놓았다가 그 String을 Console.WriteLine하는 방법이 있습니다. BufferedStream과 StringWriter로 조금 더 향상시킬 수 있는 것 같으나 자세한 것은 다른 분의 답변을 기다리겠습니다.
더알아봐야겠지만 블로그 풀이 방법은 StringBuilder 사용이다
기존의 방식대로 string을 사용해서 출력을 하게되면
무조건 시간초과가 발생하는 문제이다.
string은 사용하면 할수록 처리 속도가 느려지게 된다.
이유를 설명하려면 다소 복잡한데...
약간 간략하게 압축해서 설명하자면,
int나 float는 값형식인 것에 비해서, string 형식은 참조형식이다.
또한, string 변수를 변형하거나 바꾸는 등의 작업을 수행하면
저장되었던 값이 바뀌는 것이 아니라
새로운 저장공간에 새로운 string이 생성되는 방식이다.
그러다보니 string을 많이쓰면 쓸수록 공간이 낭비되고 속도가 느려진다.
c#에서는 이를 방지하기 위해 StringBuilder라는 것이 존재한다.
먼저, StringBuilder를 사용하려면
using System.Text; 를 적어줘야만 한다.
스트링빌더에 대해서 간단히 설명하면
문자열을 한번에 통합적으로 관리한다고 생각하면 된다.
Append() : 줄바꿈 x (WriteLine)
AppendLine() : 줄바꿈 (WriteLine())
메소드를 사용해서 문자열을 담아둘 수 있다.
출력할 때는 ToString()으로 뽑아낸다.
그리고 아직 배열문제에 진입하지는 않았지만,
효과적으로 문제를 풀기위해서 어쩔 수 없이 배열을 사용했다.
(참고 : https://coding-of-today.tistory.com/81)
using System;
// StringBuilder를 사용하기 위해서.
using System.Text;
namespace for문4
{
class Program
{
static void Main(string[] args)
{
// 스트링빌더 answer 선언
StringBuilder answer = new StringBuilder();
// 테스트케이스 개수 입력 받음
int t = int.Parse(Console.ReadLine());
// 더하기 때 사용할 int를 배열로 생성함
int[] a = new int[t];
int[] b = new int[t];
for (int i = 0; i < t; i++)
{
// 숫자를 입력받고 공백을 기준으로 나눠 담을 수 있게 문자열 배열을 선언한다.
string[] s = Console.ReadLine().Split();
// 입력받은 문자를 int형으로 변환해서 저장한다.
a[i] = int.Parse(s[0]);
b[i] = int.Parse(s[1]);
// 각 숫자를 더한뒤 문자열로 변환하고 answer에 집어 넣음
answer.AppendLine((a[i] + b[i]).ToString());
}
// 출력
Console.WriteLine(answer.ToString());
}
}
}
https://www.acmicpc.net/problem/11021
11021번: A+B - 7
각 테스트 케이스마다 "Case #x: "를 출력한 다음, A+B를 출력한다. 테스트 케이스 번호는 1부터 시작한다.
www.acmicpc.net
static void Main(string[] args)
{
int count = int.Parse(ReadLine());
for (int i = 1; i <= count; i++)
{
string[] s = ReadLine().Split();
int a = Convert.ToInt32(s[0]);
int b = Convert.ToInt32(s[1]);
WriteLine("Case #{0}: {1}", i, a + b);
}
}
https://www.acmicpc.net/problem/2438
2438번: 별 찍기 - 1
첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제
www.acmicpc.net
static void Main(string[] args)
{
int num = int.Parse(ReadLine());
for(int i = 1; i<=num; i++)
{
for(int j =1; j<=i; j++)
{
Write("*");
}
WriteLine();
}
}
https://www.acmicpc.net/problem/2439
2439번: 별 찍기 - 2
첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제 하지만, 오른쪽을 기준으로 정렬한 별(예제 참고)을 출력하시오.
www.acmicpc.net
좀 고민을 함 이상하게 푼줄알았는데 다른 코드들도 비슷하네
for { ............①
for {} .....②
+
for {} .....③
}
using System;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int num = int.Parse(ReadLine());
string s = " ";
for(int i = 1; i<=num; i++)
{
for(int k = num-1; k>=i; k--)
{
Write(" ");
}
for (int j = 1; j <= i; j++)
{
Write("*");
}
WriteLine();
}
}
}
}
https://www.acmicpc.net/problem/10951
10951번: A+B - 4
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
EOF에 대해 알아보는 문제
: End Of File
간단하게 파일의 끝을 확인할 수 있는가를 묻는다
아래 코드처럼 간략하게 if문으로 input에 입력값이 없어 null상태가 되면 무한루프를 빠져나갈 수 있게 한다는데
EOF가정확히뭔지.. 저걸로 eof구현끝?
static void Main()
{
while (true)
{
string input = Console.ReadLine();
if (input == null) break;
string[] AB = input.Split();
int a = int.Parse(AB[0];
int b = int.Parse(AB[1]);
Console.WriteLine(a + b);
}
}
https://www.acmicpc.net/problem/1110
1110번: 더하기 사이클
0보다 크거나 같고, 99보다 작거나 같은 정수가 주어질 때 다음과 같은 연산을 할 수 있다. 먼저 주어진 수가 10보다 작다면 앞에 0을 붙여 두 자리 수로 만들고, 각 자리의 숫자를 더한다. 그 다음,
www.acmicpc.net
이상한 변수 막 집어넣고 while조건도 다르게 하다가 결국은 틀린 문제..
최종적으로 값 비교해야하니까 copyinput에 값 복사하고 input은 그대로,copyinput값 가지고 while문에서 count 1회 증가하면서 연산수행
연산 후 input == copyinput이면 break
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using static System.Console;
namespace Baekjoon
{
class MainApp
{
static void Main(string[] args)
{
int input = int.Parse(ReadLine());
int copyInput = input;
int count = 0;
while (true)
{
count += 1;
int a = copyInput / 10;
int b = copyInput % 10;
int sum = a + b;
copyInput = b * 10 + sum % 10;
if(input == copyInput)
{
break;
}
}
WriteLine(count);
}
}
}