/*
http://p...content-available-to-author-only...h.net/test/read.cgi/tech/1402622093/81
[ プログラム ] プログラミングのお題スレ Part4
81 名前:デフォルトの名無しさん [sage]: 2014/06/20(金) 00:35:34.20 ID:eyF0lSTD
お題:0から9のうち8種類の数字を使って4桁の10進数を2個つくり、その積をとると
4桁の2個の数に使用した8種類の数字はひとつも現れなかった。
この4桁の2個の10進数を求める。
ver.2
*/
#include<stdio.h>
#include<stdlib.h>
#define DBG_COUNT // 関数コール回数をカウントするならこれを有効にする
// 時間測りたければコメントアウトして無効にする
#ifdef DBG_COUNT
int nKeisan = 0; // keisan(); call counter
int nCheck = 0; // check (); call counter
#endif
//左辺と右辺で同じ数字があるか探す
// -1 : 同じ数字 あり
// 0 : 〃 なし
int keisan(int *res, int *a)
{
int r, x, i;
#ifdef DBG_COUNT
nKeisan++;
#endif
r = *res = (a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3])
* (a[4] * 1000 + a[5] * 100 + a[6] * 10 + a[7]);
while (r) {
x = r % 10;
for (i = 0; i < 8; i++)
if (a[i] == x)
return -1;
r /= 10;
}
return 0;
}
//左辺に同じ数字があるか探す
// -1 : 同じ数字 あり
// 0 : 〃 なし
int check(int *a, int n)
{
int i;
#ifdef DBG_COUNT
nCheck++;
#endif
for (i = 0; i < n; i++)
if (a[i] == a[n])
return -1;
return 0;
}
//
int main()
{
int a[8], r;
for (a[0] = 1; a[0] < 9; a[0]++) {
for (a[1] = 0; a[1] < 10; a[1]++) { if (check(a, 1)) continue;
for (a[2] = 0; a[2] < 10; a[2]++) { if (check(a, 2)) continue;
for (a[3] = 0; a[3] < 10; a[3]++) { if (check(a, 3)) continue;
for (a[4] = a[0] + 1; a[4] < 10; a[4]++) { if (check(a, 4)) continue;
for (a[5] = 0 ; a[5] < 10; a[5]++) { if (check(a, 5)) continue;
for (a[6] = 0 ; a[6] < 10; a[6]++) { if (check(a, 6)) continue;
for (a[7] = 0 ; a[7] < 10; a[7]++) { if (check(a, 7)) continue;
if(0 == keisan(&r, a))
printf("%d%d%d%d x %d%d%d%d = %d\n", a
[0], a
[1], a
[2], a
[3], a
[4], a
[5], a
[6], a
[7], r
); } } } } } } } }
#ifdef DBG_COUNT
"check (); %d called\n"
"keisan(); %d called\n"
, nCheck, nKeisan
);
#endif
return 0;
}
LyoKaHR0cDovL3AuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmgubmV0L3Rlc3QvcmVhZC5jZ2kvdGVjaC8xNDAyNjIyMDkzLzgxClsg44OX44Ot44Kw44Op44OgIF0g44OX44Ot44Kw44Op44Of44Oz44Kw44Gu44GK6aGM44K544OsIFBhcnQ0Cgo4MSDlkI3liY3vvJrjg4fjg5Xjgqnjg6vjg4jjga7lkI3nhKHjgZfjgZXjgpMgW3NhZ2Vd77yaIDIwMTQvMDYvMjAo6YeRKSAwMDozNTozNC4yMCBJRDpleUYwbFNURArjgYrpoYzvvJow44GL44KJOeOBruOBhuOBoTjnqK7poZ7jga7mlbDlrZfjgpLkvb/jgaPjgaY05qGB44GuMTDpgLLmlbDjgpIy5YCL44Gk44GP44KK44CB44Gd44Gu56mN44KS44Go44KL44GoCjTmoYHjga4y5YCL44Gu5pWw44Gr5L2/55So44GX44GfOOeorumhnuOBruaVsOWtl+OBr+OBsuOBqOOBpOOCguePvuOCjOOBquOBi+OBo+OBn+OAggrjgZPjga405qGB44GuMuWAi+OBrjEw6YCy5pWw44KS5rGC44KB44KL44CCCgogICAgdmVyLjIKKi8KI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CgojZGVmaW5lIERCR19DT1VOVCAgIC8vIOmWouaVsOOCs+ODvOODq+WbnuaVsOOCkuOCq+OCpuODs+ODiOOBmeOCi+OBquOCieOBk+OCjOOCkuacieWKueOBq+OBmeOCiwogICAgICAgICAgICAgICAgICAgIC8vIOaZgumWk+a4rOOCiuOBn+OBkeOCjOOBsOOCs+ODoeODs+ODiOOCouOCpuODiOOBl+OBpueEoeWKueOBq+OBmeOCiwoKI2lmZGVmIERCR19DT1VOVAogICAgaW50IG5LZWlzYW4gPSAwOyAvLyBrZWlzYW4oKTsgY2FsbCBjb3VudGVyCiAgICBpbnQgbkNoZWNrICA9IDA7IC8vIGNoZWNrICgpOyBjYWxsIGNvdW50ZXIKI2VuZGlmCgovL+W3pui+uuOBqOWPs+i+uuOBp+WQjOOBmOaVsOWtl+OBjOOBguOCi+OBi+aOouOBmQovLyAgLTEgOiDlkIzjgZjmlbDlrZcg44GC44KKCi8vICAgMCA6ICAgIOOAgyAgICDjgarjgZcKaW50IGtlaXNhbihpbnQgKnJlcywgaW50ICphKQp7CiAgICBpbnQgciwgeCwgaTsKCiNpZmRlZiBEQkdfQ09VTlQKICAgIG5LZWlzYW4rKzsKI2VuZGlmCiAgICByID0gKnJlcyA9IChhWzBdICogMTAwMCArIGFbMV0gKiAxMDAgKyBhWzJdICogMTAgKyBhWzNdKQogICAgICAgICAgICAgKiAoYVs0XSAqIDEwMDAgKyBhWzVdICogMTAwICsgYVs2XSAqIDEwICsgYVs3XSk7CiAgICB3aGlsZSAocikgewogICAgICAgIHggPSByICUgMTA7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IDg7IGkrKykKICAgICAgICAgICAgaWYgKGFbaV0gPT0geCkKICAgICAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICByIC89IDEwOwogICAgfQoKICAgIHJldHVybiAwOwp9CgovL+W3pui+uuOBq+WQjOOBmOaVsOWtl+OBjOOBguOCi+OBi+aOouOBmQovLyAgLTEgOiDlkIzjgZjmlbDlrZcg44GC44KKCi8vICAgMCA6ICAgIOOAgyAgICDjgarjgZcKaW50IGNoZWNrKGludCAqYSwgaW50IG4pCnsKICAgIGludCBpOwoKI2lmZGVmIERCR19DT1VOVAogICAgbkNoZWNrKys7CiNlbmRpZgogICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykKICAgICAgICBpZiAoYVtpXSA9PSBhW25dKQogICAgICAgICAgICByZXR1cm4gLTE7Cg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