Có nhiều cách giải cho bài toán này, vd:
http://diendan.congdongcviet.com/threads/t367719::chuyen-chuoi-thanh-so.cpp
//csc /nologo TaySonCs.cs
//enum dg: int{A = 0, G = 1, N = 2, O = 3, Q = 4, R = 5, S = 6, T = 7, U = 8, Y = 9}
const int A
= 0, G
= 1, N
= 2, O
= 3, Q
= 4, R
= 5, S
= 6, T
= 7, U
= 8, Y
= 9;
1000 * p[U] +
100 * p[A] +
10 * p[N] +
p[G];
}
1000 * p[R] +
100 * p[U] +
10 * p[N] +
p[G];
}
10000 * p[A] +
1000 * p[Y] +
100 * p[S] +
10 * p[O] +
p[N];
}
//# ABC .. YZ ==> ZY .. CBA
int i
= ofs, n
= (len
+ ofs
- 1); arr[i] = arr[n];
arr[n] = tmp;
i++; n--;
}
}
int k
= -1, n
= arr
.Length; if (arr
[i
- 1] < arr
[i
]) k
= i
- 1; for (int i
= 0; i
< n
; i
++) arr
[i
] = i
; }
//# Next
if (arr
[k
] < arr
[i
]) j
= i
; //# swap
int t
= arr
[k
]; arr
[k
] = arr
[j
]; arr
[j
] = t
;
daoNguoc(arr, k + 1, n - (k + 1));
}
string digits
= "AGNOQRSTUY"; //set of "QUANG" + "TRUNG" + "TAYSON" Console.WriteLine("\tInput:\n QUANG + TRUNG = TAYSON\n");
//
int[] p
= new int[] { 0,
1,
2,
3,
4,
5,
6,
7,
8,
9 }; k++;
if (p
[Q
] != 0 && p
[T
] != 0){ //# Giảm tính toán : 2 * 9! int x
= Quang
(p
), y
= Trung
(p
), z
= TaySon
(p
); Console.WriteLine("\tOutput:\n {0} + {1} = {2}", x, y, z);
Console.Write("\n\tWith:");
for (int i
= 0; i
< 10; i
++) Console.Write("\n {0} = {1}", digits
[i
], p
[i
]); }
}
}
Console.WriteLine("\n =============\n Dem = {0}", k);
Console.Write("\nDone, bye"); //Console.ReadLine();
}
}
}
Đây không phải là các giải mới, cũng không tìm cách tối ưu, hay chứng minh nó đúng. Đây chỉ đơn thuần dịch lại, không sử dụng hàm lập sẵn (std::next_permutation), nhằm sử dụng cho ngôn ngữ không có hỗ trợ
Code:
Input:
QUANG + TRUNG = TAYSON
Output:
92084 + 15284 = 107368
With:
A = 0
G = 4
N = 8
O = 6
Q = 9
R = 5
S = 3
T = 1
U = 2
Y = 7
=============
Dem = 3628800
Done, bye
...
..
.