프로그래밍
프로그래머스 C# 암호 해독(주기, P, J)
노마드선샤인
2022. 12. 29. 16:47
728x90
using System;
public class Solution {
public string solution(string cipher, int code) {
string answer = "";
for(int i=0;i<cipher.Length;i++)
{
if(i%code == 0 && code+i-1 < cipher.Length && i != 0) //code로 나뉘어지고, cipher.Length+1보다 작은 범위 내에서 출력.
{
answer += cipher[code+i-1];
}
}
return answer;
}
}
////
using System;
public class Solution {
public string solution(string cipher, int code) {
string answer = "";
for(int i=code-1;i<cipher.Length;i+=code)
{
answer+=cipher[i];
}
return answer;
}
}
파이썬
def solution(cipher, code):
answer = cipher[code-1::code]
return answer
자바
import java.util.*;
class Solution {
public String solution(String cipher, int code) {
String[] arr = cipher.split("");
StringBuilder sb = new StringBuilder();
int x = code-1;
while(x < arr.length)
{
sb.append(arr[x]);
x += code;
}
return sb.toString();
}
}
class Solution {
public String solution(String cipher, int code) {
String answer = "";
for(int i=code-1; i<cipher.length(); i+=code)
{
answer += cipher.substring(i, i+1);
}
return answer;
}
}
728x90