일전에 뭐 게임 회사에서 본 간단한 퀴즈 테스트 입니다.
0~9까지의 문자로 된 숫자를 입력 받았을 때, 이 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.
sample inputs: 0123456789 01234 01234567890 6789012345 012322456789
sample outputs: true false false true false
자바로 풀어 봤습니다.
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");
}
}
}
}
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));
}
}
자바입니당.
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;
}
}
}
}
2022年03月24日 14:44
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");
}
}
}
}
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;
}
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) + " ");
}
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
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");
}
}
```{.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)+"가 중복되었습니다.");
}
}
}
}
}
2021年03月04日 15:16
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;
}
}
풀이 작성