일전에 뭐 게임 회사에서 본 간단한 퀴즈 테스트 입니다.
0~9까지의 문자로 된 숫자를 입력 받았을 때, 이 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.
sample inputs: 0123456789 01234 01234567890 6789012345 012322456789
sample outputs: true false false true false
C#
private bool checkCount(string test)
{
char[] array = test.ToCharArray();
int max =
(from t in array
group t by t into g
select g.Count()).Max();
return max > 1 ? false : true;
}
2015年09月16日 11:47
0을 제외하고 1부터 9까지 더하면 45인걸 이용했습니다.
static void Main(string[] args)
{
string input = "0123456789 01234 01234567890 6789012345 012322456789";
input.Split(new char[] { ' ' }).ToList().ForEach((s) => Console.WriteLine("{0} ", IsDuplicate(s)));
}
static bool IsDuplicate(string input)
{
var table = new List<int>();
table.AddRange(input.Select((c) => (int)char.GetNumericValue(c)));
return table.Where((i) => i == 0).Count() == 1 & table.Count == 10 & table.Sum() == 45;
}
2015年11月28日 00:50
const int LEN = '0' + '1' + '2' + '3' + '4' + '5' + '6' + '7' + '8' + '9';
private static bool analysis(string data) {
int[] b = data.Distinct().Select(_s => Convert.ToInt32(_s)).ToArray();
return (b.Length == data.Length && b.Min() == '0' && b.Max() == '9' && b.Sum() == LEN);
}
2015年12月11日 16:28
C#으로 작성했습니다. time complexity는 O(n) 입니다.
public bool DuplicateNumbers(List<string> inputs)
{
var hash = new HashSet<int>(from i in inputs select int.Parse(i));
return hash.Count == 10;
}
2015年12月18日 14:10
C# 입니다.
string line = "0123456789 01234 01234567890 6789012345 012322456789";
string[] split = line.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < split.Length; i++)
Console.Write(new string(split[i].OrderBy(x => x).ToArray()) == "0123456789");
각 문자열을 나누고 정렬후에 비교하는 간단한 방법입니다.
2015年12月18日 19:28
String Input = "0123456789";
Char[] C = Input.ToCharArray();
bool Ans = true;
if (C.Length != 10)
{
Ans = false;
}
else
{
for (int i = 0; i < 10; i++)
{
if (!C.Contains(Char.Parse(i.ToString())))
{
Ans = false;
break;
}
}
}
일단 input을 chararray 로 쪼게고 길이가 10인지 확인한뒤 각 숫자들이 있는지 contains로 확인
using static System.Console;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string sample = "0123456789 01234 01234567890 6789012345 012322456789";
sample.Split(' ').ToList().ForEach(str => Write(IsDuple(str) + " ")); ;
WriteLine();
}
static bool IsDuple(string str)
{
var hsc = new HashSet<char>(str);
return hsc.Count == 10 && str.Length == hsc.Count;
}
}
2017年05月30日 13:04
C#
using System;
class Program
{
static void Main(string[] args)
{
//Console.Write("inputs: ");
//string inputString = Console.ReadLine();
string inputString = "0123456789 01234 01234567890 6789012345 012322456789";
string[] inputStrings = inputString.Split();
bool isValid(string s) // 로컬 함수
{
byte[] counters = new byte[10]; // 1 byte 정수형
foreach (char c in s) // string --> char 바로 되네!
{
try
{
counters[c - '0']++;
}
catch // index 벗어날 경우
{
return false;
}
}
foreach (int counter in counters)
{
if (counter != 1)
{
return false;
}
}
return true;
}
foreach (string s in inputStrings) // "var": 타입 추론
{
Console.Write("{0} ", isValid(s));
}
}
}
처음 써 봅니다.
2017年07月15日 22:43
class Program
{
static void Main(string[] args)
{
bool bOk = true;
string strInput = Console.ReadLine();
if (strInput.Length != 10)
{
Console.WriteLine("0 ~ 9까지의 문자로 된 숫자를 입력하세요!");
return;
}
char[] digits = strInput.ToCharArray();
for (int k = 0; k < digits.Length - 1; k++)
{
for (int m = k; m < digits.Length - 1; m++)
{
if (digits[k] == digits[m+1])
{
bOk = false;
break;
}
}
if (!bOk)
break;
}
Console.WriteLine(bOk.ToString());
}
}
2018年08月15日 20:40
풀이 작성