일전에 뭐 게임 회사에서 본 간단한 퀴즈 테스트 입니다.
0~9까지의 문자로 된 숫자를 입력 받았을 때, 이 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.
sample inputs: 0123456789 01234 01234567890 6789012345 012322456789
sample outputs: true false false true false
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로 확인
num = input("숫자를 입력하세요 = " ) #str
list_num = list(num)
check_list = []
for i in range(0, 10):
check = list_num.count(str(i))
check_list.append(check)
a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
if check_list == a : print("true")
else : print("false")
2016年12月03日 00:32
box=range(0,10) values=raw_input("> ") for i in values: box.remove(int(i)) if box: print 'false!' else: print 'true!'
2016年12月05日 00:17
int main() { int i; int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; char c[256]; fgets(c, sizeof(c), stdin); c[strlen(c)-1] = '0円';
if (strlen(c) != 10) {
printf("False\n");
return 0;
}
for (i = 0; i < 10; i++) {
a[(int)(c[i])-48]++;
}
for (i = 0; i < 10; i++) {
if (a[i] != 1) {
printf("False\n");
return 0;
}
}
printf("True\n");
return 0;
}
2016年12月13日 13:12
#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
$input = "0123456789 01234 01234567890 6789012345 012322456789";
$output = array();
foreach(explode(" ",$input) as $item){
$aItem = array_count_values(str_split($item));
$chk = 0;
for($i=0;$i<=9;$i++) if(isset($aItem[$i]) && $aItem[$i]==1) $chk++;
$output[] = (count($aItem) == 10 && $chk == 10) ? 'true' : 'false';
}
echo sprintf("sample inputs: %s",$input);
echo "\n";
echo sprintf("sample outputs: %s",join(" ",$output));
// 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
풀이 작성