코딩도장

Duplicate Numbers

일전에 뭐 게임 회사에서 본 간단한 퀴즈 테스트 입니다.

0~9까지의 문자로 된 숫자를 입력 받았을 때, 이 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.

  • sample inputs: 0123456789 01234 01234567890 6789012345 012322456789

  • sample outputs: true false false true false

no duplicates
(追記) (追記ここまで)
댓글 작성은 로그인이 필요합니다.
+1 sample output에서 두 번째꺼는 01234니까 0~9사이의 숫자가 각각 하나씩 사용되어서 true 아닌가요?? 아니면.... 0~9까지 모든 숫자가 나와야 하는건가요??? - SPJung, 2015年12月31日 10:44 M D
+1 두 번째 예시는 한 번씩이 아니라 0번 씩 사용된 숫자들이 있으므로 false 입니다. - SungWook Jung, 2017年04月24日 15:17 M D
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

13개의 풀이가 있습니다. 1 / 2 Page

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;
 }
댓글 작성은 로그인이 필요합니다.
이렇게 하면 중복은 발라낼 수 있는데 빠진 숫자가 있는지 체크가 안 될꺼 같아서 static 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(); int cnt = (from t in array group t by t into g select g.Count()).Count(); return ( max == 1 && cnt == 10) ? true : false; } static void Main(string[] args) { string s = "0123456789 01234 01234567890 6789012345 012322456789"; foreach (string ss in s.Split() ) Console.WriteLine( ss + " " + ( checkCount(ss) ? "true" : "false" ) ); } 이렇게 고쳐봤습니다. 개인적으로 from select 이런 식의 C#구문 처음 써보는데 편하군요. - Kim JungRae, 2015年09月16日 15:55 M D
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

C# 박재우님의 아이디어를 차용해서 새로 짜 보았습니다.

 private bool checkCount2(string test)
 {
 char[] array = test.ToCharArray();
 return Enumerable.SequenceEqual(array, array.Distinct().ToArray<char>());
 }

2015年09月17日 09:39

임 정구

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

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;
 }
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
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);
}
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

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;
 }
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

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");

각 문자열을 나누고 정렬후에 비교하는 간단한 방법입니다.

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
 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로 확인

2016年11月26日 18:59

Choco

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

C-Sharp

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;
 }
}
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

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));
 }
 }
}

처음 써 봅니다.

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
 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());
 }
 }
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

풀이 작성

(注記) 풀이작성 안내
  • 본문에 코드를 삽입할 경우 에디터 우측 상단의 "코드삽입" 버튼을 이용 해 주세요.
  • 마크다운 문법으로 본문을 작성 해 주세요.
  • 풀이를 읽는 사람들을 위하여 풀이에 대한 설명도 부탁드려요. (아이디어나 사용한 알고리즘 또는 참고한 자료등)
  • 작성한 풀이는 다른 사람(빨간띠 이상)에 의해서 내용이 개선될 수 있습니다.
풀이 작성은 로그인이 필요합니다.
목록으로
코딩도장

코딩도장은 프로그래밍 문제풀이를 통해서 코딩 실력을 수련(Practice)하는 곳입니다.

no duplicates x 1

언어별 풀이 현황
전 체 x 501
python x 321
java x 68
기 타 x 61
cpp x 16
cs x 13
javascript x 9
objectivec x 5
ruby x 1
php x 3
r x 3
matlab x 1
코딩도장 © 2014 · 문의 [email protected]
피드백 · 개인정보취급방침 · RSS

AltStyle によって変換されたページ (->オリジナル) /