일전에 뭐 게임 회사에서 본 간단한 퀴즈 테스트 입니다.
0~9까지의 문자로 된 숫자를 입력 받았을 때, 이 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.
sample inputs: 0123456789 01234 01234567890 6789012345 012322456789
sample outputs: true false false true false
#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;
}
#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
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;
}
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;
}
#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, " ");
}
}
안녕하세요. 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";
}
#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;
}
2016年12月23日 17:04
// 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;
}
2017年01月17日 10:13
/*
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;
}
2017年02月18日 19:00
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;
}
2018年05月02日 21:34
풀이 작성