코딩도장

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

68개의 풀이가 있습니다. 1 / 7 Page

자바로 풀어 봤습니다.

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
 public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);
 ArrayList<Integer> inputNumbers = new ArrayList<>();
 ArrayList<Integer> compareNumbers = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8,9));
 System.out.println("0~9까지의 숫자가 각각 한 번 씩만 사용된 문자열 입력하시오: (ex, 6789012345)");
 String inputData = scan.next();
 if(inputData.length() != 10) {
 System.out.println("False");
 }else {
 long numbers = Long.parseLong(inputData);
 int j = 10;
 while(j>0) {
 inputNumbers.add((int)(numbers%10));
 numbers /= 10;
 j--;
 }
 for(int i=0; i<inputNumbers.size(); i++) {
 if(compareNumbers.remove(inputNumbers.get(i)) == false) {
 System.out.println("False");
 }
 }
 if(compareNumbers.size()== 0) {
 System.out.println("True");
 }
 }
 }
}

2022年06月05日 23:59

유로

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
package com.algorithm.algorithmpractice.dojang;
import java.util.*;
public class Memory {
 public static void main(String[] args) {
// 일전에 뭐 게임 회사에서 본 간단한 퀴즈 테스트 입니다.
// 0~9까지의 문자로 된 숫자를 입력 받았을 때, 이 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.
// sample inputs: 0123456789 01234 01234567890 6789012345 012322456789
// sample outputs: true false false true false
 String input = "6789012345";
 String[] inputArr = input.split("");
 List<String> inputList = new ArrayList(Arrays.asList(inputArr));
 Set inputSet = new HashSet();
 boolean result = true;
 while (inputList.size() > 0){
 String temp = inputList.get(0);
 inputList.remove(0);
 for(int i = 0; i < inputList.size(); i++){
 if(temp.equals(inputList.get(i))){
 result = false;
 break;
 }
 }
 }
 System.out.println(Boolean.toString(result));
 }
}

자바입니당.

2022年04月28日 07:51

inkuk ju

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
import java.util.Arrays;
import java.util.Scanner;
public class DupulicateNum {
 public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 String[] arr = sc.next().split(""); // 하나씩 잘라서 arr에 문자를 넣어줌
 int[] intArr = new int[arr.length]; // 문자를 Integer로 변환후 넣어줄 배열
 for (int i = 0; i < arr.length; i++) {
 intArr[i] = Integer.parseInt(arr[i]); // 문자 arr[i]를 int형식으로 변환후 intArr에 삽입
 }
 Arrays.sort(intArr); // 비교를 위해 정렬
 for (int i = 0; i < arr.length; i++) {
 if (intArr[i] == i && intArr.length == 10) { // intArr에 저장된 수가 10개 이면서 동시에 배열의[i] 번째와 i의 숫자가 똑같다면.
 System.out.println("true");
 break;
 } else { // 조건을 만족하지 않으면
 System.out.println("false");
 break;
 }
 }
 }
}
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
package org.javaturotials.ex;
import java.util.*;
import java.util.stream.Collectors;
public class test {
 public static void main(String[] args) { 
 Scanner sc =new Scanner(System.in);
 while(true) {
 String num = sc.next();
 String ox = "";
 int count =0;
 for(int i=0; i<num.length(); i++) {
 for(int j=0; j<num.length(); j++) {
 if(i!=j) {
 if(num.charAt(i)==num.charAt(j)) {
 count++;
 }
 }
 }
 }
 if(count==0) {
 System.out.println("true");
 }
 else {
 System.out.println("false");
 }
 }
 }
 }

2022年02月20日 23:32

Kkubuck

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
public static void main(String[] args) {
 String[] sample = { "0123456789", "01234", "01234567890", "6789012345", "012322456789"};
 for (int i = 0; i < sample.length; i++) {
 if (isCheck(sample[i].split("")) == true) {
 System.out.print(true + " ");
 } else {
 System.out.print(false + " ");
 }
 }
 }
 public static boolean isCheck(String[] arr) {
 boolean isc = false;
 if (arr.length == 10 && isCheckNum(arr) != true) {
 isc = true;
 }
 return isc;
 }
 public static boolean isCheckNum(String[] arr) {
 boolean isc = false;
 for (int i = 0; i < arr.length; i++) {
 for (int j = i+1; j < arr.length; j++) {
 if(arr[i] == arr[j]) {
 isc = true;
 break;
 }
 }
 }
 return isc;
 }

2022年02月01日 18:48

김우연

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
static boolean outputs(String x) {
 boolean isNum = true;
 int[] str = new int[10];
 for (int i = 0; i < x.length(); i++) {
 str[x.charAt(i)-48] += 1;
 }
 for (int i = 0; i < str.length; i++) {
 if (str[i] == 0 || str[i] >1)
 isNum = false;
 }
 return isNum;
 }
 public static void main(String[] args) {
 String[] num = {"0123456789", "01234", "01234567890", "6789012345", "012322456789"};
 for(String i:num)
 System.out.print(outputs(i) + " ");
 }

2021年10月24日 08:59

박대현

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
package exam;
import java.util.Scanner;
public class EX17 {
 public static void main(String[] args) {
 // 0~9까지 배열 생성 
 // 여기서 equals로 맞는지 확인할 것
 String index [] = new String[] {"0","1","2","3",
 "4","5","6","7",
 "8","9",};
 // 맞으면 1씩 증가해서 10이되면 멈춤
 int count = 0;
 Scanner sc = new Scanner(System.in);
 while(true) {
 System.out.print("수 입력 : ");
 String str = sc.next();
 String [] arr = str.split("");
 // 입력 받은 수를 10자리 이상
 if(str.length() < 9) {
 System.out.print("false\n");
 continue;
 }
 // 이중 for문을 돌려 배열마다 값 확인 
 // 같으면 index[]를 비우고 count를 1씩 올린다
 for(int i = 0; i < index.length; i++) {
 for(int j = 0; j < index.length; j++) {
 if(arr[i].equals(index[j])) {
 index[j] = null;
 count++;
 }
 }
 }
 // count가 10이 되면 true를 출력하고 멈춘다
 if(count == 10) {
 System.out.print("true");
 break;
 }else {
 System.out.print("false");
 }
 sc.close();
 }
 }
}

java

2021年08月16日 14:42

전채

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 System.out.print("0~9까지 숫자를 입력하세요: ");
 String[] str = sc.nextLine().split("");
 List<String> list = new ArrayList<>(Arrays.asList(str));
 Set<String> set = new LinkedHashSet<>(list);
 if (list.size() != set.size() || list.size() < 10) {
 System.out.println("false");
 } else {
 System.out.println("true");
 }
 }

2021年06月26日 18:14

커피코딩

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

```{.java} import java.util.Scanner; import java.util.Arrays;

public class main {

public static void main(String[] args) {
 Scanner s = new Scanner(System.in);
 String[] NumberLine = s.nextLine().split(" "); 
 //입력받은 수를 정렬하고 앞뒤수가 같으면 false 다르면 true 
 for(int i=0; i<NumberLine.length; i++) {
 for(int j=0; j<NumberLine[i].length()-1; j++) {
 if(NumberLine[i].charAt(j)==NumberLine[i].charAt(j+1)) {
 System.out.println(NumberLine[i].charAt(j)+"가 중복되었습니다.");
 }
 }
 } 
}

}

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
public class CodingTest {
 public static void main(String[] args) {
 System.out.println(isUnique("01234256789"));
 }
 public static boolean isUnique(String text) {
 boolean result = true;
 for(int i = 0; i < text.length()&&result; i++) {
 for(int j = i + 1; j < text.length()&&result; j++) 
 result &= (text.charAt(j)!=text.charAt(i));
 }
 return result;
 }
}

2021年01月28日 10:12

김용현

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

풀이 작성

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

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

no duplicates x 1

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

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