for (i = 0; i < 36; i ++)接著我寫一函式,傳入兩整數,一為轉換為錢之整數,一為此整數的進位制。函式程式碼如下:
scanf("%d", &money[i]);
int getMoney(int n, int base)最後將 2 - 36 進位與欲印出的數字丟到上述函式轉換錢,並且進行比較,比較後記錄最小值,最後印出即可。C 語言程式碼如下:
{
int nMoney = 0;
for (; n; n /= base)
nMoney += money[n % base];
return nMoney;
}
index = 0;
scanf("%d", &m);
for (i = 0; i < m; i ++)
{
scanf("%d", &k);
index = 0;
min = getMoney(k, 2);
best[index ++] = 2;
for (j = 3; j <= 36; j ++)
{
count = getMoney(k, j);
if (min == count) best[index ++] = j;
if (min > count) min = count, index = 0, best[index ++] = j;
}
printf("Cheapest base(s) for number %d:", k);
for (j = 0; j < index; j ++)
printf(" %d", best[j]);
printf("\n");
}
By David.K
p11005題目連結
回ACM題庫目錄
回首頁
沒有留言:
張貼留言