코딩 공부/C#-코딩테스트

(백준/C#) 문자열

공부를 함 2024. 9. 9. 18:05

//27866

using static System.Console;

string s = ReadLine();
int i = int.Parse(ReadLine());
WriteLine(s[i-1]);

 

 

//2743

using static System.Console;

string s = ReadLine();
WriteLine(s.Length);

 

 

//9086

using static System.Console;

int count = int.Parse(ReadLine());
string s = "";
for (int i = 0; i < count;i++)
{
    s = ReadLine();
    WriteLine($"{s[0]}{s[s.Length - 1]}");
}
using static System.Console;

string x() => ReadLine();
var s = x();
while ((s = x()) != null)
    WriteLine($"{s[0]}" + s[^1]);

 

정의 된 x()메서드는 ReadLine()을 호출하고 입력을 문자열로 반환 한다.

입력값이 null이거나 사용자가 종료할 경우를 제외하고 지속해서 x()를 호출하여 s를 초기화 한다.

^1은 C# 8.0에서 도입된 역 인덱스 기능이며 s.Length - 1과 같은 의미를 지니고 있다.

 

 

//11654

using static System.Console;

var x = ReadLine();
char c = x[0];
WriteLine(Convert.ToInt32(c));
using static System.Console;
WriteLine(ReadLine());

 

 

//11720

using static System.Console;

int count = int.Parse(ReadLine());
char[] num = ReadLine().ToCharArray();
int answer = 0;
for(int i = 0; i < count;i++)
{
    answer += (int)Char.GetNumericValue(num[i]);
}
WriteLine(answer);
int a = 0;
Console.ReadLine();
foreach(char c in Console.ReadLine())
{
    a += c - '0';
}
Console.Write(a);

문자 c에서 '0'을 빼는 것은 문자를 정수로 변환하는 과정이다.

C#에서 문자 '0'의 ASCII 값은 48이다. 따라서, 문자 '3'의 ASCII 값은 51이므로 '3' - '0'의 결과는 51 - 48 = 3이 된다.

 

 

//10809

string s = Console.ReadLine();
char x = 'a';
for(int i = 0; i<26;i++)
    Console.Write(s.IndexOf((char)(x+i))+" ");

아스키코드에 대해 이해하고 string.IndexOf()를 사용하는 게 관건인 듯 하다.

해당 메소드는 입력된 문자열에서 해당 문자가 처음 나타나는 위치(인덱스)를 반환한다. 만약 문자가 문자열에 없다면 -1을 반환한다.

 

 

//2675

string num = Console.ReadLine();
string[] s = { };
for(int n = 0; n < int.Parse(num); n++)
{
    s = Console.ReadLine().Split();
    for (int i = 0; i < s[1].Length; i++)
        for (int j = 0; j < int.Parse(s[0]); j++)
            Console.Write(s[1][i]);
    Console.WriteLine();
}
int n = int.Parse(Console.ReadLine());
while (n-- > 0)
{
    string[] s = Console.ReadLine().Split(' ');
    foreach (char c in s[1])
        Console.Write(new string(c, int.Parse(s[0])));
    Console.Write("\n");
}

 

 

//1152

Console.Write(Console.ReadLine().Split(' ').Count(e=>e!=""));

 

 

//2908

string[] s = Console.ReadLine().Split();
int num1 = int.Parse(new string(s[0].Reverse().ToArray()));
int num2 = int.Parse(new string(s[1].Reverse().ToArray()));

Console.WriteLine(num1>num2 ? num1 : num2);

 

 

//5622

Console.Write(Console.ReadLine().Select(i=>5*Math.Min(i,'X')/16-17).Sum());

 

 

//11718

string x() => Console.ReadLine();
var s = x();
Console.WriteLine(s);
while ((s = x()) != null)
{
    Console.WriteLine(s);
}
Console.Write(Console.In.ReadToEnd());

'코딩 공부 > C#-코딩테스트' 카테고리의 다른 글

(백준/C#) 일반 수학 1  (0) 2024.09.24
(백준/C#) 심화 1  (0) 2024.09.23
(백준/C#) 1차원 배열  (0) 2024.08.19
(백준/C#) 반복문  (0) 2024.08.19
(백준/C#) 조건문  (0) 2024.08.19