Ref: ITSA 51 - [Problem 5] Number Puzzle
/*******************************************************/
/* [Problem 5] Number Puzzle */
/* Author: awei0905 [at] awei0905.blogspot.tw */
/* Version: 2017/05/28 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
char buf[3][4];
int ok = 0;
void permute(int *a, int l, int r)
{
if (ok) return;
if (l == r) {
int num3 = a[buf[2][0]] * 100 + a[buf[2][1]] * 10 + a[buf[2][2]];
if (num3 >= 300) return;
int num1 = a[buf[0][0]] * 10 + a[buf[0][1]];
int num2 = a[buf[1][0]] * 10 + a[buf[1][1]];
if (num1 + num2 == num3) ok = 1;
}
else
{
char temp;
for (int i = l; i <= r; i++)
{
temp = *(a + l);
*(a + l) = *(a + i);
*(a + i) = temp;
permute(a, l + 1, r);
temp = *(a + l);
*(a + l) = *(a + i);
*(a + i) = temp;
}
}
}
int main()
{
while (scanf("%s%s%s", buf[0], buf[1], buf[2]) != EOF) {
for (int i = 0; i < 3; i++)
for (int j = 0; buf[i][j] != '\0'; j++)
buf[i][j] -= 'A';
ok = 0;
int str[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
permute(str, 0, 9);
if (ok)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Debug: I/O窮舉法,把所有可能的答案進行排列匹配。
AB
BC
EED
AA
BB
ABA
AB
CD
DFG
AB
CD
EFG
AB
CD
CAD
YES
NO
YES
YES
NO
沒有留言:
張貼留言