최근 알고리즘보다 실제 코드에 대해서 좀 작성을 하다 알고리즘의 부족함을 깨닫고 다시 백준을 풀기로 결심함
다음에 종강 후 시간이 된다면 비동기 프로그래밍과 파이어베이스에 대해서 다룬 프로젝트와 코드를 소개할 예정이다.
백준 온라인 코딩 문제풀이
Baekjoon Online Judge
Baekjoon Online Judge 프로그래밍 문제를 풀고 온라인으로 채점받을 수 있는 곳입니다.
www.acmicpc.net
코드 참고
wny0320 (박 상운)
Run code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
replit.com
10988
팰린드롬인 것을 찾는 문제, 팰린드롬이 아닌 회문으로 불리기도 하는데 회문이 좀 더 익숙함.
using System;
class Program
{
static void Main(string[] args)
{
string text = Console.ReadLine();
string re_text = "";
int size = text.Length;
for(int i = size-1; i >= 0; i--)
{
re_text += text[i];
}
if(text == re_text)
Console.WriteLine(1);
else
Console.WriteLine(0);
}
}
vs 없이 백준에서 바로 짜다보니 코드가 원시적으로 좀 작성된 경향이 있지만 간단한 문제기에 문제가 되지 않을것이라고 생각함
9506
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<string> outputList = new List<string>();
while(true)
{
string input = Console.ReadLine();
int inputInt = int.Parse(input);
if(inputInt == -1)
break;
int nowNum = inputInt;
List<int> divisorList = new List<int>();
for(int i = 2; i < nowNum; i++)
{
if(nowNum % i == 0)
{
divisorList.Add(i);
}
}
divisorList.Add(1);
divisorList.Sort();
int size = divisorList.Count;
int sum = 0;
string output = inputInt + " = ";
for(int i = 0; i < size; i++)
{
sum += divisorList[i];
if(i != 0)
output += " + ";
output += divisorList[i];
}
if(sum == inputInt)
outputList.Add(output);
else
outputList.Add(inputInt + " is NOT perfect.");
}
int size2 = outputList.Count;
for(int i = 0; i < size2; i++)
{
Console.WriteLine(outputList[i]);
}
}
}
출력이 한번에 되어야하기 때문에 반복문의 수를 줄이고자 리스트를 사용하였음
수가 입력될때 -1인지 확인
-> -1이라면 반복을 멈추고 출력리스트를 출력
-> -1이 아니라면 아래 코드 실행
자기 자신과 1을 뺀 모든 약수를 구하고 반복이 끝난 후 약수 리스트에 1을 추가.
Linq에 있는 List.Sort()기능을 이용하여 오름차순으로 정렬하였다.
원래는 약수의 합과 입력값이 같으면 반복문으로 출력 string에 데이터를 넣는것으로 구조를 계획했으나, 같은 반복문이 사용되어야 하는 것을 보고 하나의 반복문에 출력 string을 추가하는것으로 변경.
약수의 합과 입력값이 같다면 출력 string을 출력 리스트에 저장, 아니라면 is not perfect 문구가 들어간 출력 string을 출력 리스트에 저장
이후 모든 출력 리스트를 순서대로 출력
위와같이 동작하는 코드이다.
'BaekJoon[C#]' 카테고리의 다른 글
자료구조 9012 (0) | 2025.03.25 |
---|---|
백준 24313 알고리즘 수업 - 점근적 표기 (0) | 2023.12.06 |
정렬 단계 2751 (0) | 2022.12.27 |
정렬 단계 25305 (0) | 2022.12.26 |
정렬 단계 2587 (0) | 2022.12.25 |