不過還是提供程式碼給大家參考:
while (scanf("%d%d", &n, &k) == 2)
{
int total = n, record;
for (; n > k - 1;)
{
total += n / k;
record = n / k;
n = (n % k + record);
}
printf("%d\n", total);
}
By David.K
p10346題目連結
回ACM題庫目錄
回首頁
學習程式設計,語法固然重要,也是許多程式設計課程的教學重點。但是看的懂 C ,不見得會用 C 來解決問題(Problem solving),所以學會解題是重點中的重點。 學習C語言的不二法門,就是從寫程式解題開始,這裡的考古題由淺而深,循序漸進,對初學者甚有助益。 ACM 協會針對每年程式設計比賽的練習需求,建立一個線上的題庫與評分系統,希望藉由題庫練習的機會,在此心得分享,讓有心學習程式解題的人,能有個溝通成長的橋樑。
while (scanf("%d%d", &n, &k) == 2)
{
int total = n, record;
for (; n > k - 1;)
{
total += n / k;
record = n / k;
n = (n % k + record);
}
printf("%d\n", total);
}
int m, n, i, j, k;
scanf("%d%d", &m, &n);
for (i = 0; i < m ; i ++)
scanf("%s %d", w[i].str, &w[i].point);
for (i = 0; i < n; i ++)
{
int point = 0;
while (gets(str))
{
if (str[0] == '.') break;
int index = 0;
for (j = 0; str[j]; j ++)
{
if (isalpha(str[j]))
word[index ++] = str[j];
if (!isalpha(str[j]) || str[j + 1] == '\0')
{
word[index] = '\0';
for (k = 0; k < m; k ++)
if (strcmp(word, w[k].str) == 0)
{ point += w[k].point; break; }
index = 0;
}
}
}
printf("%d\n", point);
}
struct word
{
char str[20];
int point;
};
struct word w[1000];
char str[200];
char word[20];
scanf("%d", &n);
a = n;
while (n --)
{
scanf("%d", &m);
index = 0;
for (;m != 1 && m != 0;)
{
int add = 0;
if (m < 0 && m % 2) add = 1;
ans[index ++] = abs(m % -2);
m /= -2;
if (add) m ++;
if(m == -1) ans[index ++] = 1, m = 1;
}
if (m == 1) ans[index ++] = 1;
printf("Case #%d: ", a - n);
for (i = index - 1; i >= 0; i --)
printf("%d", ans[i]);
if (m == 0) printf("%d", 0);
printf("\n");
}
int i ,total = n, recode;
for (; n > 2;)
{
total += n / 3;
recode = n / 3;
n = n % 3 + recode;
}
if (n == 2) total ++;
printf("%d\n", total);
isPut = 0;
for (j = s; j <= e; j ++)
{
recode = j * j;
mul = 10;
for (k = 0; k < 4; k++)
{
g = 0;
while (recode / mul != 0 && recode % mul != 0)
{
if (recode / mul + recode % mul == j)
{
printf("%d\n",j);
isPut = 1;
g = 1;
break;
}
mul *= 10;
}
if (g) break;
mul *= 10;
}
}
if (!isPut)
printf("no kaprekar numbers\n");
struct RomanNum
{
int i;
int v;
int x;
int l;
int c;
};
struct RomanNum RN[101];
void adder(int index, int n)
{
if (n == 0) return;
if (n >= 40 && n <= 49)
RN[index].l ++, RN[index].x ++, n -= 40;
if (n >= 90 && n <= 99)
RN[index].c ++, RN[index].x ++, n -= 90;
if (n % 10 == 4)
RN[index].v ++, RN[index].i ++, n -= 4;
if (n % 10 == 9)
RN[index].x ++, RN[index].i ++, n -= 9;
if (n / 100 > 0)
RN[index].c += (n/100), n %= 100;
if (n / 50 > 0)
RN[index].l += (n/50), n %= 50;
if (n / 10 > 0)
RN[index].x += (n/10), n %= 10;
if (n / 5 > 0)
RN[index].v += (n/5), n %= 5;
if (n / 1 > 0)
RN[index].i += (n/1), n %= 1;
}
for (i = 0; i < 100; i ++)
{
adder(i, i + 1);
RN[i + 1].i = RN[i].i, RN[i + 1].v = RN[i].v,
RN[i + 1].x = RN[i].x;
RN[i + 1].l = RN[i].l, RN[i + 1].c = RN[i].c;
}
while (1)
{
scanf("%d", &n);
if (n == 0) break;
n -= 1;
printf("%d: %d i, %d v, %d x, %d l, %d c\n", n+1,
RN[n].i, RN[n].v, RN[n].x, RN[n].l, RN[n].c);
}
while (gets(str))
{
int isTurn = 0, i, j;
c[0].ch1 = str[0], c[0].ch2 = str[5], c[0].isComp = 0;
c[1].ch1 = str[1], c[1].ch2 = str[4], c[1].isComp = 0;
c[2].ch1 = str[2], c[2].ch2 = str[3], c[2].isComp = 0;
c[3].ch1 = str[6], c[3].ch2 = str[11], c[3].isComp = 0;
c[4].ch1 = str[7], c[4].ch2 = str[10], c[4].isComp = 0;
c[5].ch1 = str[8], c[5].ch2 = str[9], c[5].isComp = 0;
for (i = 0; i < 3; i ++)
for (j = 3; j < 6; j ++)
{
if (c[i].isComp == 0 && c[j].isComp == 0)
{
if (c[i].ch1 == c[j].ch1 && c[i].ch2 == c[j].ch2 ||
c[i].ch1 == c[j].ch2 && c[i].ch2 == c[j].ch1)
{
c[i].isComp = 1, c[j].isComp = 1;
break;
}
}
}
for (i = 0; i < 6; i ++)
if (c[i].isComp == 0)
isTurn = 1;
if (isTurn)
printf("FALSE\n");
else printf("TRUE\n");
}
struct Cube
{
char ch1;
char ch2;
int isComp;
};
struct Cube c[6];
unsigned int binary[31], total;
void create()
{
int i, j = 1;
for (i = 0, j = 2; i < 31; i ++, j *= 2)
binary[i] = j - 1;
}
scanf("%s", str);
if (str[0] == '0') break;
total = 0;
for (i = 0; i < strlen(str); i ++)
rev[i] = str[strlen(str) - i - 1] - '0';
for (i = 0; i < strlen(str); i ++)
total += rev[i] * binary[i];
printf("%ld\n", total);
int Factorial[10001] = {0};
Factorial[0] = 1;
for (i = 1; i <= 10000; i ++)
{
Factorial[i] = Factorial[i - 1] * i;
while (Factorial[i] % 10 == 0)
Factorial[i] /= 10;
Factorial[i] %= 100000;
}
while (scanf("%d", &n) == 1)
printf("%5d -> %d\n", n, Factorial[n]%10);
struct Ans
{
int ansInt;
int isComp;
};
struct Ans a[n];
for (i = 0; i < n; i ++)
{
scanf("%d", &m);
a[i].ansInt = m;
a[i].isComp = 0;
}
isZero = 1, A = 0, B = 0;
for (i = 0; i < n; i ++)
{
scanf("%d", &m);
if (m != 0) isZero = 0;
b[i].ansInt = m;
b[i].isComp = 0;
a[i].isComp = 0;
}
for (i = 0; i < n; i ++)
if (a[i].ansInt == b[i].ansInt)
A ++, a[i].isComp = 1, b[i].isComp = 1;
for (i = 0; i < n; i ++)
for (j = 0; j < n; j ++)
{
if (i != j && a[i].isComp == 0 && b[j].isComp == 0 && a[i].ansInt == b[j].ansInt)
B ++, a[i].isComp = 1, b[j].isComp = 1;
}
if (isZero) break;
else printf(" (%d,%d)\n", A, B);
int isTrue[10000] = {0}, len[10000];
int count = 0;(註:PseRand() 函式將四個數丟入後,運算 (Z * L + I) % M 後回傳 L 值)
while (!isTrue[L])
{
isTrue[L] = 1, len[L] = count ++;
L = PseRand(Z, I, M, L);
}
printf("%d\n", count - len[L]);
int isHappyNumber(int n)
{
int i, len = 1, sum = 0, recode[50];
if (n == 1) return len;
while (sum != 1)
{
recode[len ++] = n;
sum = 0;
for (i = 10; (n / i) != 0 || (n % i) != 0; )
{
sum += (n % i) * (n % i);
n /= i;
}
n = sum;
for (i = 0; i < len; i ++)
if (recode[i] == sum)
return 0;
}
return len;
}
for (k = i; k <= j; k++)
if (isHappyNumber(k))
printf("%d %d\n", k, isHappyNumber(k));
cycleLen = 1e-12;
scanf("%lf %lf", &B, &H);
B += 1e-15; H += 1e-15;
SL = (double)sqrt( (H*H) + (B*B/4) );
area = B * H / 2;
r = 2 * area / (SL+SL+B);
while (r >= 0.000001)
{
cycleLen += 2 * r * PI;
HSub2R = H - 2 * r;
r *= HSub2R / H;
H = HSub2R;
}
while (1)
{
scanf("%d", &input[len]);
len ++;
if (input[len - 1] == 0) break;
}
printf("PERFECTION OUTPUT\n");
for (j = 0; j < len - 1; j ++)
{
n = input[j];
int total = 1, n1 = n;
for( i = 2 ; i < n1; i++ )
{
int isFactor = 0, add = 1, count = 1;
while ( n % i == 0 && n != 0)
{
n /= i;
add += pow(i, count);
count ++;
isFactor = 1;
}
if (isFactor)
total *= add;
}
if (total - n1 < n1)
printf("%5d DEFICIENT\n", n1);
else if (total - n1 == n1)
printf("%5d PERFECT\n", n1);
else
printf("%5d ABUNDANT\n", n1);
}
printf("END OF OUTPUT\n");
int i, j, length, num = 0;
length = strlen(str);
for (i = length - 1; i >= 0; i --)
intArray[length - 1 - i] = charToInt(str[i]);
for (i = 0, j = 1; i < length; i ++, j *= sc)
num += intArray[i] * j;
int charToInt(char hex)
{
if (hex == 'A') return 10;
else if (hex == 'B') return 11;
else if (hex == 'C') return 12;
else if (hex == 'D') return 13;
else if (hex == 'E') return 14;
else if (hex == 'F') return 15;
else return (hex - '0');
}
char intToChar(int i)
{
if (i == 10) return 'A';
else if (i == 11) return 'B';
else if (i == 12) return 'C';
else if (i == 13) return 'D';
else if (i == 14) return 'E';
else if (i == 15) return 'F';
else return (i + '0');
}
void print(int num)
{
int i, j, isPut = 0;
if (num == 0) printf("0");
else if (num / pow(ec, 7) > 0) printf("ERROR");
else
{
for (i = pow(ec, 6); i >= 1; i /= ec)
{
if (isPut) printf("%c", intToChar(num / i)), num %= i;
else if (num / i > 0 && !isPut)
printf("%c", intToChar(num / i)), num %= i, isPut = 1;
else printf(" ");
}
}
printf("\n");
}
int isPut = 0, i;
for (i = 0; i < 9; i ++)
{
if (isPut && pow[i] != 0)
{
if (pow[i] < 0) printf(" - ");
else printf(" + ");
pow[i] = pow[i] < 0 ? -1 * pow[i]: pow[i];
if (pow[i] != 1) printf("%d", pow[i]);
else if (i == 8) printf("%d", pow[i]);
if (i == 7) printf("x");
else if (i != 8) printf("x^%d", 9 - i - 1);
}
else if (!isPut && pow[i] != 0)
{
if (pow[i] < 0) printf("-");
pow[i] = pow[i] < 0 ? -1 * pow[i]: pow[i];
if (pow[i] != 1) printf("%d", pow[i]);
else if (i == 8) printf("%d", pow[i]);
if (i == 7) printf("x");
else if (i != 8) printf("x^%d", 9 - i - 1);
isPut = 1;
}
}
if (!isPut) printf("0");
printf("\n");
int i, isZero = 1, total = 0;
for (i = 0; i < 6; i ++)
{
scanf("%d", &packets[i]);
residual[i] = 0;
if (packets[i] != 0)
isZero = 0;
}
if (isZero) break;
total += packets[5];
X | X | X | X | X | O |
X | X | X | X | X | O |
X | X | X | X | X | O |
X | X | X | X | X | O |
X | X | X | X | X | O |
O | O | O | O | O | O |
total += packets[4];
residual[0] += packets[4] * 11;
total += packets[3];
residual[1] += packets[3] * 5;
total += packets[2] / 4;
packets[2] %= 4, residual[2] = packets[2];
if (residual[2] == 1)
residual[2] = 0, residual[0] += 7, residual[1] += 5, total ++;
else if (residual[2] == 2)
residual[2] = 0, residual[0] += 6, residual[1] += 3, total ++;
else if (residual[2] == 3)
residual[2] = 0, residual[0] += 5, residual[1] += 1, total ++;
if (residual[1] >= packets[1])
residual[1] -= packets[1], packets[1] = 0;
else
packets[1] -= residual[1], residual[1] = 0;
total += packets[1] / 9;
packets[1] %= 9;
if (packets[1] > 0)
residual[0] += 36 - packets[1] * 4, packets[1] = 0, total ++;
else residual[0] += residual[1] * 4, residual[1] = 0;
if (residual[0] >= packets[0])
residual[0] -= packets[0], packets[0] = 0;
else
packets[0] -= residual[0], residual[0] = 0;
total += packets[0] / 36;
packets[0] %= 36;
if (packets[0] > 0)
total ++;
int strPeriod(char str[], int j)
{
int i;
for (i = j; i < strlen(str); i ++)
if (str[i] != str[i % j]) break;
if (i == strlen(str)) return 1;
else return 0;
}
gets(str);
length = strlen(str);
for (j = 1; j < length; j ++)
{
if (length % j == 0)
if (strPeriod(str, j) == 1)
break;
}
printf("%d\n", j);
#define MAXLEN 35
char str[MAXLEN];
char compStr[] = {'A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j'
,'K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t'
,'U','u','V','v','W','w','X','x','Y','y','Z','z'};
int comp(char ch1, char ch2)
{
int i, ch1i, ch2i;
for (i = 0; i < strlen(compStr); i ++)
if (ch1 == compStr[i]) ch1i = i;
for (i = 0; i < strlen(compStr); i ++)
if (ch2 == compStr[i]) ch2i = i;
if (ch1i < ch2i) return 1;
else return 0;
}
void sort()
{
int i, j;
char ch;
for (i = strlen(str) - 1; i >= 0; i--)
for (j = 0; j < strlen(str); j++)
if (comp(str[strlen(str) - j - 1], str[i]) )
ch = str[strlen(str) - j - 1],
str[strlen(str) - j - 1] = str[i], str[i] = ch;
}
int next_permutation()
{
int i, j, k;
int length;
length = strlen(str);
for(i = length - 1;i > 0 ; --i)
if(comp(str[i-1], str[i])) break;
j = i;
if(j == 0) return 0;
for(i = length - 1;i > 0; --i)
if(comp(str[j-1], str[i])) break;
k = i;
swap(j-1, k);
for(i = j, j = length - 1;i < j; ++i, --j)
swap(i, j);
return 1;
}
int n, i;
scanf("%d", &n);
getchar();
for (i = 0; i < n; i ++)
{
gets(str);
sort();
printf("%s\n", str);
while (next_permutation())
printf("%s\n", str);
}
#define MAXLEN 60以上程式碼參考來源
char str[MAXLEN];
void swap(int i, int j)
{
char tmp;
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
int next_permutation()
{
int i, j, k;
int length;
length = strlen(str);
for(i = length - 1;i > 0 ; --i)
if(str[i-1] < str[i]) break;
j = i;
if(j == 0) return 0;
for(i = length - 1;i > 0; --i)
if(str[j-1] < str[i]) break;
k = i;
swap(j-1, k);
for(i = j, j = length - 1;i < j; ++i, --j)
swap(i, j);
return 1;
}
#define NUMLEN 200
struct AddNum
{
int num[NUMLEN];
int length;
};
struct AddNum AN1, AN2;
char numStr[101];
gets(numStr);
if (numStr[0] == '0') break;
AN2.length = strlen(numStr) - 1;
for (i = AN2.length, j = 0; i >= 0; i --, j ++)
AN2.num[j] = numStr[i] - '0';
for (i = 0; i < NUMLEN; i ++)
{
AN1.num[i] += AN2.num[i];
if (AN1.num[i] >= 10)
AN1.num[i + 1] ++, AN1.num[i] %= 10;
}
for (i = 0; i < NUMLEN; i ++)
{
AN1.num[i] += AN2.num[i];
if (AN1.num[i] >= 10)
AN1.num[i + 1] ++, AN1.num[i] %= 10;
}
#define LEN 5001
#define NUMLEN 1050
struct FibNum
{
int num[NUMLEN];
int length;
};
struct FibNum FN[LEN];
FN[0].num[0] = 0, FN[0].length = 0,
FN[1].num[0] = 1, FN[1].length = 0;
int i, j, k, n;
for (i = 2; i < LEN; i ++)
{
for (j = 0; j < NUMLEN; j ++)
{
FN[i].num[j] += FN[i - 1].num[j] + FN[i - 2].num[j];
if (FN[i].num[j] >= 10)
FN[i].num[j + 1] ++, FN[i].num[j] %= 10;
}
for (j = NUMLEN - 1; ; j--)
if (FN[i].num[j] > 0)
break;
FN[i].length = j ;
}
while (1)
{
if (scanf("%d", &n) < 1) break;
printf("The Fibonacci number for %d is ", n);
for (i = FN[n].length ;i >= 0 ; i--)
printf("%d", FN[n].num[i]);
printf("\n");
}