顯示具有 ACM 解題參考 標籤的文章。 顯示所有文章
顯示具有 ACM 解題參考 標籤的文章。 顯示所有文章

2010年9月26日 星期日

ACM 442 - Matrix Chain Multiplication

#include <stdio.h>
#include <ctype.h>

struct matrix
{
char ch;
int row;
int col;
int count;
};
struct matrix m[26], stack[200];

int main()
{
int n, row, col, i;
char ch, str[200];
scanf("%d", &n);
getchar();
for (i = 0; i < n; i ++)
{
scanf("%c %d %d", &ch, &row, &col);
getchar();
int index = ch - 'A';
m[index].ch = ch, m[index].row = row, m[index].col = col;
}
while (gets(str))
{
int count = 0, error = 0, index = 0;
int row = 0, col = 0;
for (i = 0; (ch = str[i]); i ++)
{
if (ch == '(') stack[index ++].ch = ch;
if (isupper(ch))
{
int s = ch - 'A';
row = m[s].row, col = m[s].col;
if (isupper(stack[index - 1].ch))
{
if (stack[index - 1].col != row)
{ error = 1; break; }
stack[index - 1].count += stack[index - 1].row * row * col;
stack[index - 1].col = col;
}
else
{
stack[index].ch = ch;
stack[index].row = row;
stack[index].col = col;
stack[index].count = 0;
index ++;
}
}
if (ch == ')')
{
if (index - 1 < 0) { error = 1; break; }
if (stack[index - 1].ch == '(') index --;
if (index - 2 >= 0 &&
isupper(stack[index - 1].ch) && stack[index - 2].ch == '(')
{
stack[index - 2].ch = stack[index - 1].ch;
stack[index - 2].row = stack[index - 1].row;
stack[index - 2].col = stack[index - 1].col;
stack[index - 2].count = stack[index - 1].count;
index --;
if (isupper(stack[index - 2].ch))
{
row = stack[index - 1].row;
col = stack[index - 1].col;
count = stack[index - 1].count;

if (stack[index - 2].col != row)
{ error = 1; break; }
stack[index - 2].count += (count + stack[index - 2].row * row * col);
stack[index - 2].col = col;
index --;
}
}
}

}

if (error == 0) printf("%d\n", stack[0].count);
else if (error) printf("error\n");

}
return 0;
}

回目錄
回首頁

ACM 424 - Integer Inquiry

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define NUMLEN 200

struct AddNum
{
int num[NUMLEN];
int length;
};

struct AddNum AN1, AN2;
char numStr[101];

int main()
{
int i, j;
while (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 = NUMLEN - 1; ; i --)
if (AN1.num[i] > 0)
break;
for (; i >= 0; i --)
printf("%d", AN1.num[i]);
printf("\n");
return 0;
}

回目錄
回首頁

ACM 375 - Inscribed Circles and Isosceles Triangles

#include <stdio.h>
#include <math.h>
double B, H, SL, S, area, r, HSub2R, cycleLen;

int main()
{
double PI = 2*acos(0.0);
int n, i;
scanf("%d", &n);
for (i = 0; i < n; i ++)
{
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;
}
if (i != 0) printf("\n");
printf("%13.6lf\n", cycleLen);
}
return 0;
}

回目錄
回首頁

ACM 374 - Big Mod

#include <stdio.h>
int f(int m,int n, int mod)
{
long long int mul = m;
long long int r = 1;
while(n){
if(n % 2 == 1){r *= mul;r %= mod;}
mul *= mul;
mul %= mod;
n >>= 1;
}
return r;
}
int main()
{
int B,P,M;
while(scanf("%d%d%d",&B,&P,&M)==3)
printf("%d\n",f(B,P,M));
return 0;
}

回目錄
回首頁

ACM 344 - Roman Digititis

#include <stdio.h>

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;
}

int main()
{
int i, n;
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);
}
return 0;
}

回目錄
回首頁

2010年9月19日 星期日

ACM 340 - Master-Mind Hints

#include <stdio.h>

struct Ans
{
int ansInt;
int isComp;
};

int main()
{
int n, i, m, isZero, A, B, j, index = 0;
while (scanf("%d", &n) == 1)
{
if (n == 0) break;
printf("Game %d:\n", ++ index);
struct Ans a[n];
for (i = 0; i < n; i ++)
{
scanf("%d", &m);
a[i].ansInt = m;
a[i].isComp = 0;
}
struct Ans b[n];
while (1)
{
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);
}

}
return 0;
}

回目錄
回首頁

ACM 350 - Pseudo-Random Numbers

#include <stdio.h>

int PseRand(int Z, int I, int M, int L)
{
return (Z * L + I) % M;
}

int main()
{
int Z, I, M, L, caseNum = 1;

while (1)
{
scanf("%d %d %d %d", &Z, &I, &M, &L);
if (Z == 0 && I == 0 && M == 0 && L == 0) break;
int isTrue[10000] = {0}, len[10000] ;
printf("Case %d: ", caseNum++);
int count = 0;
while (!isTrue[L])
{
isTrue[L] = 1, len[L] = count ++;
L = PseRand(Z, I, M, L);
}
printf("%d\n", count - len[L]);
}

return 0;
}

回目錄
回首頁

ACM 299 - Train Swapping

#include <stdio.h>
#include <malloc.h>
int *S, size;

int bubbleSort ()
{
int i, j, tmp, count = 0;
for (i = 0; i < size; i ++)
{
for (j = 0; j < size - i - 1; j ++)
{
if (S[j] > S[j + 1])
{
tmp = S[j];
S[j] = S[j + 1];
S[j + 1] = tmp;
count ++;
}
}
}
return count;
}

int main()
{
/*freopen("111.txt", "r", stdin);
freopen("111w.txt", "w", stdout);*/
int n, i, j;
scanf("%d", &n);
for (i = 0; i < n; i ++)
{
scanf("%d", &size);

S = (int *)malloc(sizeof(int)*size);
for (j = 0; j < size; j ++)
scanf("%d", &S[j]);
printf("Optimal train swapping takes %d swaps.\n", bubbleSort());
free(S);
}
}

回目錄
回首頁

2010年9月18日 星期六

ACM 333 - Recognizing Good ISBNs

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[82];
int i, numCount, s, e;
while (gets(str))
{
if (strlen(str) == 0) { printf(" is incorrect.\n"); continue; }
numCount = 0;
int per = 0;
int tsum = 0;
int sum[9] = {0}, wrong = 0, d = 0, isX = 0, g = 0;
for (s = 0; str[s] == ' ' || str[s] == '\t' || isalpha(str[s]) ; s ++)
if (str[s] == 'X') break;
for (e = strlen(str) - 1; str[e] == ' ' || str[e] == '\t' || isalpha(str[e]); e --)
if (str[e] == 'X') break;
for (i = s; i <= e; i ++)
{
if (str[i] == '-') continue;
else if (isdigit(str[i]))
{
numCount ++;
per += str[i] - '0';
tsum += per;
}
else if (str[i] == 'X')
{
if (numCount != 9) { wrong = 1; break;}
else
{
per += 10;
tsum += per;
numCount ++;
}
}
else { wrong = 1; break;}
}
for (i = 0; str[i] == ' ' || str[i] == '\t'; i ++) ;
s = i;
for (i = strlen(str) - 1; str[i] == ' ' || str[i] == '\t'; i --) ;
e = i;
for (i = s; i <= e; i ++)
putchar(str[i]);
if (wrong) printf(" is incorrect.\n");
else if (numCount >= 10 && tsum % 11 == 0) printf(" is correct.\n");
else printf(" is incorrect.\n");

}
return 0;
}

回目錄
回首頁

ACM 332 - Rational Numbers from Repeating Fractions

#include <stdio.h>
#include <string.h>

int gcd(int a,int b)
{
if (a % b == 0){ return b;}
return gcd(b , a % b);
}

int pow10(int n)
{
int i, sum = 1;
for (i = 0; i < n; i ++)
sum *= 10;
return sum;
}

char str[20];

int main()
{
int j, i, k, h, g, d, s, caseNum = 1;
while(scanf("%d", &j) == 1 && j != -1)
{
scanf("%s", str);
char ch;
int len = strlen(str);
if (j != 0) d = pow10(len - 2) - pow10(len - 2 - j);
else d = pow10(len - 2);
k = 0, h = 0;
for (i = 2, g = 0; i < len; i ++, g ++)
{
ch = str[i];
if (g < (len - 2 - j)) h = h * 10 + (ch - '0');
k = k * 10 + (ch - '0');
}
if (j != 0)
{
s = gcd(k - h, d);
printf("Case %d: %d/%d\n", caseNum ++, (k - h) / s, d / s);
}
else
{
s = gcd(k, d);
printf("Case %d: %d/%d\n", caseNum ++, k / s, d / s);
}


}

return 0;
}

回目錄
回首頁

ACM 326 - Extrapolation Using a Difference Table

#include <stdio.h>

int a[50];

int main() {
int n, i, j, k;
scanf("%d", &n);
while (n)
{
for (i=0; i<n; i++)
scanf("%d", &a[i]);
scanf("%d", &k);
for (j=n-1; j>0; j--)
for (i=0; i<j; i++)
a[i] = a[i+1] - a[i];
for (i=0; i<k; i++)
for (j=1; j<n; j++)
a[j] += a[j-1];
printf("Term %d of the sequence is %d\n", n+k, a[n-1]);
scanf("%d", &n);
}
return 0;
}

回目錄
回首頁

ACM 311 - Packets

#include <stdio.h>

int residual[6], packets[6];

int main()
{
while (1)
{
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]; /* 6*6一人一個盒子 */
total += packets[4]; /* 5*5一人一個盒子,剩餘 11 個 1*1 空間 */
residual[0] += packets[4] * 11;
total += packets[3]; /* 4*4一人一個盒子,剩餘 5 個 2*2 空間 */
residual[1] += packets[3] * 5;

total += packets[2] / 4; /* 3*3 之處理方式 */
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]) /* 2*2 之處理方式 */
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]) /* 1*1 之處理方式 */
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 ++;
printf("%d\n", total);
}
return 0;
}

回目錄
回首頁

ACM 300 - Maya Calendar

#include <stdio.h>
#include <string.h>

char hMonthStr[19][8] = {"pop", "no", "zip", "zotz",
"tzec", "xul", "yoxkin", "mol",
"chen", "yax", "zac", "ceh",
"mac", "kankin", "muan", "pax",
"koyab", "cumhu", "uayet"};
char tMonthStr[20][10] = {"imix", "ik", "akbal", "kan",
"chicchan", "cimi", "manik", "lamat",
"muluk", "ok", "chuen", "eb",
"ben", "ix", "mem", "cib",
"caban", "eznab", "canac", "ahau"};
char dStr[6], mStr[8];
int main()
{
int hDay, hMonth, hYear, tDay, tMonth, tYear;
int n, days, i;
scanf("%d", &n);
printf("%d\n", n);
while (n --)
{
char *d;
scanf("%s %s %d", dStr, mStr, &hYear);
d = strtok(dStr, ".");
sscanf(d, "%d", &hDay);
for (i = 0; i < 19; i ++)
{
if (strcmp(hMonthStr[i], mStr) == 0)
{ hMonth = i; break; }
}
days = hDay + hMonth * 20 + hYear * 365;
tYear = days / 260;
days %= 260;
tMonth = days % 20;
tDay = days % 13 + 1;

printf("%d %s %d\n", tDay, tMonthStr[tMonth], tYear);
}
return 0;
}

回目錄
回首頁

ACM 272 - TEX Quotes

#include <stdio.h>

int main()
{
int style = 0;
char c;
while (1){
c = getchar();
if (c == EOF) break;
if (c == '\"')
{
if (style == 0)
{
printf("``");
style = 1;
}
else
{
printf("''");
style = 0;
}
}
else putchar(c);
}
return 0;
}

回目錄
回首頁

ACM 264 - Count on Cantor

#include<stdio.h>    
#include<stdlib.h>
#include<string.h>
#include<math.h>
main()
{
int n,a,b,c,d;
while(scanf("%d",&n)==1)
{
for(d=1,c=n;c>d;d++) c=c-d;
if ( d%2==1 ) a=d-c+1;
else a=c;
b=d+1-a;
printf("TERM %d IS %d/%d\n",n,a,b);
}
return 0;
}
以上程式轉自:http://mypaper.pchome.com.tw/iustlovefish/post/1311842753
稍微說明一下程式碼,先看以下圖形:標紅色部分,依照每斜行遞增,所以 d=1, c=n 初始化後,用一迴圈每做一次 c 減掉 d 而做完後 d 累加一,所以這一部份是做算出 n 是在第 d 斜行的第 c 個位置。
而這個圖表有一個特性,請看以下表:
x - 1 / y - 1x - 1 / yx - 1 / y + 1
x / y - 1x / yx / y + 1
x + 1 / y - 1x + 1 / yx + 1 / y + 1
只要 d 在奇數它是往左下走,偶數則往右上走,而只要 d 在奇數則該斜行的第一個位置為 1 / d,偶數為 d / 1,最後再依照它的位置算出正確答案。

此提與 ACM 10182 Bee Maja 頗像,我是用圖法煉鋼的方法寫出,但似乎比上面的程式碼還慢,所以還是參考他的做法。

回目錄
回首頁

ACM 256 - Quirksome Squares

#include <stdio.h>

int main( void )
{
int i, n, j;
while (1)
{
if (scanf("%d", &n) < 1) break;
if (n == 2)
{
for (i = 0; i < 10; i ++)
for (j = 0; j < 10; j ++)
{
if ( i + j >= 10) break;
if ((i + j)*(i + j) == (i*10 + j))
printf("%02d\n",(i*10 + j));
}
}
if (n == 4)
{
for (i = 0; i < 100; i ++)
for (j = 0; j < 100; j ++)
{
if ( i + j >= 100) break;
if ((i + j)*(i + j) == (i*100 + j))
printf("%04d\n",(i*100 + j));
}
}
if (n == 6)
{
for (i = 0; i < 1000; i ++)
for (j = 0; j < 1000; j ++)
{
if ( i + j >= 1000) break;
if ((i + j)*(i + j) == (i*1000 + j))
printf("%06d\n",(i*1000 + j));
}
}
if (n == 8)
{
for (i = 0; i < 10000; i ++)
for (j = 0; j < 10000; j ++)
{
if ( i + j >= 10000) break;
if ((i + j)*(i + j) == (i*10000 + j))
printf("%08d\n",(i*10000 + j));
}
}
}
return 0;
}

回目錄
回首頁

ACM 253 - Cube painting

#include <stdio.h>

char str[15] = {"\0"};
struct Cube
{
char ch1;
char ch2;
int isComp;
};

struct Cube c[6];

int main()
{
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");
}
return 0;
}

回目錄
回首頁

2010年9月17日 星期五

ACM 202 - Repeating Decimals

#include <stdio.h>
#include <memory.h>

#define MAX 5000
#define DISPLAY_LIMIT 50
#define MAX_INT 3000

int min (int a , int b){
return a > b ? b : a;
}


int main()
{
int digits[MAX + 1], remainderExist[MAX_INT], remainderPos[MAX_INT];
while (1)
{
int numerator, denominator, oNumerator, quotient, remainder, recode, i;

if (scanf("%d", &numerator) < 1) break;
oNumerator = numerator;
scanf("%d", &denominator);

memset (remainderExist, 0, sizeof(remainderExist));
memset (remainderPos, 0, sizeof(remainderPos));

quotient = numerator / denominator;
remainder = numerator % denominator;

recode = quotient;
int n = 0, isCycle = 0, cyclePos = MAX, cycleLen = 0;

while ( n <= MAX && !isCycle)
{
if (remainderExist[remainder] )
{
cyclePos = remainderPos[remainder];
cycleLen = n - cyclePos;
isCycle = 1;
}
else
{
remainderExist[remainder] = 1;
remainderPos[remainder] = n;
}
numerator = remainder * 10;

quotient = numerator / denominator;
remainder = numerator % denominator;
digits[n] = quotient;
n ++;
}
printf("%d/%d = %d.", oNumerator, denominator, recode);
int limit = min(cyclePos, DISPLAY_LIMIT);

for (i = 0; i < limit; ++i )
printf("%d", digits[i]);

if (cyclePos < DISPLAY_LIMIT)
{
printf("(");
limit = min(n - 1, DISPLAY_LIMIT);
for (i = cyclePos; i < limit; ++i )
printf("%d", digits[i]);
if (n > DISPLAY_LIMIT)
printf("...");
printf(")");
}
printf("\n");
printf(" %d = number of digits in repeating cycle\n\n",cycleLen );

}
return 0;
}

回目錄
回首頁

ACM 11799 - Horror Dash

#include <stdio.h>

int getInt()
{
char ch;
int n = 0;
while( ch = getchar())
if(ch != ' ' && ch != '\n') break;
n = ch - 48;
while( ch = getchar())
{
if(ch == ' ' || ch == '\n') break;
n = n * 10 + ch - 48;
}
return n;
}

int main()
{
int caseNum, i, n, m, max, j;
caseNum = getInt();

for (i = 1; i <= caseNum; i ++)
{
n = getInt();
max = 0;
while (n --)
{
m = getInt();
if (m > max) max = m;
}
printf("Case %d: %d\n", i, max);
}
return 0;
}


回目錄
回首頁

ACM 11805 - Bafana Bafana

#include <stdio.h>

int getInt()
{
char ch;
int n = 0;
while( ch = getchar())
if(ch != ' ' && ch != '\n') break;
n = ch - 48;
while( ch = getchar())
{
if(ch == ' ' || ch == '\n') break;
n = n * 10 + ch - 48;
}
return n;
}

int main()
{
int i, caseNum, N, K, P, j;
caseNum = getInt();
for (i = 1; i <= caseNum; i ++)
{
N = getInt(), K = getInt(), P = getInt();
j = (K + P) % N;
if (j == 0) j = N;
printf("Case %d: %d\n", i, j);
}
return 0;
}


回目錄
回首頁