코딩도장

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

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

#include <iostream>
#include <string>
using namespace std;
int main( int argc , char** argv )
{
 int array[ 10 ] = { 0 , };
 string input = "012345632789";
 for( size_t i = 0 ; i < input.size() ; ++i )
 {
 array[ input[ i ] - '0' ]++;
 }
 for( int i = 0 ; i < 10 ; ++i )
 {
 if( array[ i ] > 1 )
 {
 cout << "false" << endl;
 return 0;
 }
 }
 cout << "true" << endl;
 return 0;
}

2017年04月16日 21:51

오승석

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
#include <iostream>
#include <set>
using namespace std;
int main(int argc, const char * argv[]) {
 int n;
 cin >> n;
 for (int i = 0; i < n; i++) {
 string t;
 cin >> t;
 bool ans = true;
 if (t.size() < 10) {
 ans = false;
 } else {
 set<int> n_set;
 for (int j = 0; j < t.size(); j++) {
 int tmp = t[j] - '0';
 if (n_set.find(tmp) != n_set.end()) {
 ans = false;
 break;
 } else n_set.insert(tmp);
 }
 if (ans && n_set.size() != 10) {
 ans = false;
 }
 }
 cout << (ans ? "true " : "false ");
 }
 cout << endl;
 return 0;
}

2015年11月06日 10:40

Chromatics

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

C

#include <stdio.h>
// 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.
void test_function(int arr[], int length) {
 int i, j;
 int check[10] = { 0, };
 printf("%d\n", length);
 //check 배열에 중복된 거 입력
 for (i = 0; i < length; i++) {
 for (j = 0; j < 10; j++) {
 if (arr[i] == j) {
 check[j] += 1;
 }
 }
 }
 //check 배열을 확인해서 출력
 for (i = 0; i < 10; i++) {
 if (check[i] > 1) {
 printf("[%d] is duplication!\n", i);
 }
 }
}
int main() {
 int arr1[] = { 0,1,2,3,4,5,6,7,8,9 };
 int arr2[] = { 0,1,2,3,4 };
 int arr3[] = { 0,1,2,3,4,5,6,7,8,9,0 };
 int length1 = sizeof(arr1) / sizeof(arr1[0]);
 int length2 = sizeof(arr2) / sizeof(arr2[0]);
 int length3 = sizeof(arr3) / sizeof(arr3[0]);
 test_function(arr1, length1);
 test_function(arr2, length2);
 test_function(arr3, length3);
 return 0;
}

2016年05月06日 22:23

최 진우

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

C++ 이에요 - map 자료구조를 이용했어요.

#include <iostream> 
#include <cstdio> 
#include <map> 
#include <string> 
using namespace std; 
int main(){
 string s; 
 cin >> s; 
 map<char,int> mp; 
 for (char c = '0'; c <= '9'; c++){
 mp[c] = 0; 
 }
 for (int i = 0; i < s.size(); i++){
 mp[s[i]]++; 
 }
 for (map<char,int>::iterator it = mp.begin(); it != mp.end(); it++){
 if (it->second > 1 || it->second == 0){
 cout << "false" << endl; 
 return 0; 
 }
 }
 cout << "true" << endl; 
 return 0; 
}

2016年06月06日 14:54

iljimae

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define STR_LEN 100
void torf(char*);
int main(void){
 char str[STR_LEN] = { 0, };
 printf("input : ");
 gets_s(str);
 printf("sample output : ");
 torf(str);
 system("pause");
 return 0;
}
void torf(char* str) {
 char* rst;
 rst = strtok(str, " ");
 while (rst != NULL) {
 sort(rst, rst + strlen(rst));
 //정렬 했지만 
 if (strstr(rst, "0123456789") != NULL) {
 if (strlen(rst) == 10) {
 printf("true ");
 }//01234567899 와 같은 경우도 true가 나오기에
 else {
 printf("false ");
 }
 }
 else {
 printf("false ");
 }
 rst = strtok(NULL, " ");
 }
}

2016年10月06日 11:46

개허접

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

안녕하세요. C++로 풀어봤습니다.

0~9가 모두 한번씩 사용되어야 true가 나오도록 했습니다.

#include<iostream>
#include<string>
using namespace std;
void main()
{
 cout<<"Hello Stranger??"<<endl;
 string str;
 int arr[10] = {0};
 cout<<"숫자를 을 입력하시오"<<endl;
 cin>>str;
 for(int i=0; i<str.length(); i++)
 {
 int val = (int)str[i]-48;
 arr[val]++;
 }
 int res = 1;
 for(int i=0; i<10; i++)
 res *= arr[i];
 if(res == 1)
 cout<<"true";
 else
 cout<<"false";
}

2016年11月02日 01:28

이재웅

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

C(11) gcc -std=c11

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// 문자->정수
inline static int ctoi(char c) { return c - '0'; }
// 0~9까지 숫자가 한번씩만 사용했는지
bool is_duplicate(char *str);
int main(void)
{
 char temp[80];
 while (scanf("%s", temp) == 1)
 printf("%s\n", is_duplicate(temp) ? "true" : "false");
 return 0;
}
bool is_duplicate(char *str)
{
 if (strlen(str) != 10)
 return false;
 int number[10] = {[0] = 0};
 while (*str)
 ++number[ctoi(*str++)]; 
 for (int i=0; i<10; ++i)
 if (number[i] != 1)
 return false;
 return true;
}
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
// C++ 0을 제외한 나머지 수의 합을 등차수열의 합과 비교하였습니다.
bool CheckNum( char* pcBuff, int nMax = 9 )
{
 int nSum = 0;
 int nZero = 0;
 char nNum = 0;
 nMax = ( nMax * ( nMax + 1 ) ) / 2;
 for( size_t x = 0 ; x < strlen( pcBuff ) ; ++x )
 {
 if( pcBuff[ x ] == '0' )
 {
 nZero += 1;
 if( nZero > 1 )
 {
 return false;
 }
 }
 else
 {
 nNum = pcBuff[ x ];
 nSum += atoi( &nNum );
 if( nSum > nMax )
 {
 return false;
 }
 }
 }
 if( nSum != nMax )
 {
 return false;
 }
 return true;
}
int main()
{
 char acBuff[ 1000 ] = { 0, };
 cin >> acBuff;
 cout << CheckNum( acBuff );
 return 0;
}
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
/*
dev : peanutBro
date : 170218
content :
일전에 뭐 게임 회사에서 본 간단한 퀴즈 테스트 입니다.
0~9까지의 문자로 된 숫자를 입력 받았을 때, 이 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.
sample inputs: 0123456789 01234 01234567890 6789012345 012322456789
sample outputs: true false false true false
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
 int inputCount;
 string *inputString;
 int** numCount;
 bool flagOverlap;
 cout << "숫자를 몇개 입력하실껀가요? : ";
 cin >> inputCount;
 inputString = new string[inputCount];
 numCount = new int*[inputCount];
 for (int i = 0; i < inputCount; i++)
 {
 numCount[i] = new int[10]; // 10은 0~9
 for (int j = 0; j < 10; j++)
 {
 numCount[i][j] = 0;
 }
 }
 cout << "숫자를 " << inputCount << "개 입력하세요 : ";
 for (int i = 0; i < inputCount; i++)
 {
 cin >> inputString[i];
 }
 for (int i = 0; i < inputCount; i++)
 {
 for (int j = 0; j < inputString[i].length(); j++)
 {
 numCount[i][inputString[i][j]-'0']++;
 }
 }
 for (int i = 0; i < inputCount; i++)
 {
 flagOverlap = true;
 for (int j = 0; j < 10; j++)
 {
 if (numCount[i][j] != 1)
 {
 flagOverlap = false;
 }
 }
 if (flagOverlap)
 {
 cout << "true ";
 }
 else
 {
 cout << "false ";
 }
 }
 for (int i = 0; i < inputCount; i++)
 {
 delete[] numCount[i];
 }
 delete[] numCount;
 delete[] inputString;
 return 0;
}
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

c언어입니다. 학교 과제용으로 풀었습니다..만 공유합니다.

#include <stdio.h>
int main() {
 char MunNumber[12] = {0}; 
 //11개 이상의 숫자가 입력된 경우 무조건 false, null문자까지 배열공간12 
 int NumChack[10] = {0};
 char boolChack = 1; 
 printf("숫자를 0부터 9까지 하나씩 입력하세요. : ");
 scanf("%s", MunNumber);
 for(char i = 0; i <= 10 ; i++) {
 for(char j = 0; j <= 10 ; j++) {
 if(MunNumber[j] - 48 == i) NumChack[i] += 1;
 //ASCII코드의 0~9값이 48~57이기 때문에 -48 해서 값을 숫자로 변환 
 //입력값중 i와 같은 값이 있을경우, NumChack[i]에 1을 더하기.
 } 
 }
 for(char i = 0; i <= 9 ; i++) {
 if(NumChack[i] != 1) boolChack = 0;
 //NumChack[]의 값 중 1이 아닌 값이 있을 경우 false 
 //(boolChack는 1로 초기화되어있음)
 }
 if(boolChack == 1) printf("true");
 else printf("false");
 return 0;
}
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

풀이 작성

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

코딩도장은 프로그래밍 문제풀이를 통해서 코딩 실력을 수련(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 によって変換されたページ (->オリジナル) /