開始可以創一個陣列,如同以下表格:
n/m | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
2 | 0 | 1 | 4 | 9 | 6 | 5 | 6 | 9 | 4 | 1 |
3 | 0 | 1 | 8 | 7 | 4 | 5 | 6 | 3 | 2 | 9 |
4 | 0 | 1 | 6 | 1 | 6 | 5 | 6 | 1 | 6 | 1 |
5 (與 1 重複) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
也就是說 2 * 2,只須對應到 m = 2、n = 2 的數值 4,恰好就是 2 * 2 的個位數。所以寫成 C 語言程式碼如下:
int cycle[4][10] = { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 4, 9, 6, 5, 6, 9, 4, 1},
{0, 1, 8, 7, 4, 5, 6, 3, 2, 9},
{0, 1, 6, 1, 6, 5, 6, 1, 6, 1}};
char m[102], n[102];
int mLen, nLen, mJudge, nJudge;
while (scanf("%s %s", m, n) == 2)
{
if (m[0] == '0' && n[0] == '0') break;
if (m[0] == '0' && m[1] == '\0') { printf("0\n"); continue; }
if (n[0] == '0' && n[1] == '\0') { printf("1\n"); continue; }
mLen = strlen(m), nLen = strlen(n);
printf("%d\n", cycle[( (n[nLen - 1] - '0') + ( nLen - 2 == -1 ? 0: n[nLen - 2] - '0' ) * 10 + 3) % 4 ][m[mLen - 1] - '0']);
}
By David.K
p10515題目連結
回ACM題庫目錄
回首頁
沒有留言:
張貼留言