Powered By Blogger

Tìm kiếm Blog này

Thứ Sáu, 20 tháng 8, 2010

Solution Biorhythms

Link đề bài: http://acm.pku.edu.cn/JudgeOnline/problem?id=1006


Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 70422 Accepted: 20904

Description
Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier.
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.

Input
You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output
For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form:

Case 1: the next triple peak occurs in 1234 days.

Use the plural form ``days'' even if the answer is 1.

Sample Input

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Sample Output

Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

Source
East Central North America 1999

Source Code
Problem: 1006 User: ductam1983
Memory: 136K Time: 782MS
Language: C Result: Accepted

* Source Code

#include
// #include

#define maxd 21252

// const char *INP="TEST.INP";
int p,e,i,d;
long int kq,j,M;
long int t=0;
int main()
{
// FILE *f;
// f=fopen(INP,"rb");
while(1)
{
// fscanf(f,"%d%d%d%d",&p,&e,&i,&d);
scanf("%d%d%d%d",&p,&e,&i,&d);
if(p==-1&&e==-1&&i==-1&&d==-1) break;
j=d+1;
M=maxd+d;
while(j<=M)
{
if((j-p)%23==0&&(j-e)%28==0&&(j-i)%33==0)
{
kq=j-d;
break;
}
j++;
}
printf("Case %d: the next triple peak occurs in %ld days.\n",++t,kq);
}

// getch();
return 0;
}

Solution I Think I Need a Houseboat

Link đề bài: http://acm.pku.edu.cn/JudgeOnline/problem?id=1005
I Think I Need a Houseboat
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 53842 Accepted: 22259

Description
Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to erosion caused by the Mississippi River. Since Fred is hoping to live in this house the rest of his life, he needs to know if his land is going to be lost to erosion.

After doing more research, Fred has learned that the land that is being lost forms a semicircle. This semicircle is part of a circle centered at (0,0), with the line that bisects the circle being the X axis. Locations below the X axis are in the water. The semicircle has an area of 0 at the beginning of year 1. (Semicircle illustrated in the Figure.)

Input
The first line of input will be a positive integer indicating how many data sets will be included (N). Each of the next N lines will contain the X and Y Cartesian coordinates of the land Fred is considering. These will be floating point numbers measured in miles. The Y coordinate will be non-negative. (0,0) will not be given.

Output
For each data set, a single line of output should appear. This line should take the form of: “Property N: This property will begin eroding in year Z.” Where N is the data set (counting from 1), and Z is the first year (start from 1) this property will be within the semicircle AT THE END OF YEAR Z. Z must be an integer. After the last data set, this should print out “END OF OUTPUT.”

Sample Input

2
1.0 1.0
25.0 0.0

Sample Output

Property 1: This property will begin eroding in year 1.
Property 2: This property will begin eroding in year 20.
END OF OUTPUT.

Hint
1.No property will appear exactly on the semicircle boundary: it will either be inside or outside.
2.This problem will be judged automatically. Your answer must match exactly, including the capitalization, punctuation, and white-space. This includes the periods at the ends of the lines.
3.All locations are given in miles.

Source
Mid-Atlantic 2001

Code:
Source Code
Problem: 1005 User: ductam1983
Memory: 152K Time: 0MS
Language: C Result: Accepted

* Source Code

#include
// #include

#define Pi 3.1415926535

//const char *INP="TEST.INP";

int N;


int main()
{
int i,j;
float x,y;
int erosion;
float Radius;
float distance;
// FILE *f;

// f=fopen(INP,"rb");
// fscanf(f,"%d",&N);
scanf("%d",&N);
for(i=1;i<=N;i++)
{
// fscanf(f,"%f%f",&x,&y);
scanf("%f%f",&x,&y);
distance=x*x+y*y;
erosion=0;
for(j=1;;j++)
{
erosion+=50;
Radius=(erosion*2)/Pi;
if(distance<=Radius)
{
printf("Property %d: This property will begin eroding in year %d.\n",i,j);
break;
}
}
}
printf("END OF OUTPUT.");
// getch();
return 0;
}

Solution Financial Management

Link đề bài: http://acm.pku.edu.cn/JudgeOnline/problem?id=1004
Financial Management
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 66218
Accepted: 32171

Description
Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.

Input
The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.

Output
The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.

Sample Input

100.00
489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75

Sample Output

$1581.42

Source
Mid-Atlantic 2001
Code:

Source Code
Problem: 1004
User: ductam1983
Memory: 184K
Time: 0MS
Language: C
Result: Accepted

* Source Code

#include
// #include

//const char *INP="TEST.INP";


int main()
{
/* FILE *f;
f=fopen(INP,"rb");
*/
float kq=0.00,month;
int i;
for(i=0;i<12;i++)
{
// fscanf(f,"%f",&month);
scanf("%f",&month);
kq+=month;
}

// fclose(f);

printf("$%0.2f",kq/12);
// getch();
return 0;
}

Thứ Năm, 19 tháng 8, 2010

Solution Hangover

Link đề bài: http://acm.pku.edu.cn/JudgeOnline/problem?id=1003

Hangover
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 59081 Accepted: 27883

Description

How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.


Input
The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.

Output
For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.

Sample Input

1.00
3.71
0.04
5.19
0.00

Sample Output

3 card(s)
61 card(s)
1 card(s)
273 card(s)

Source
Mid-Central USA 2001

Source Code
Problem: 1003 User: ductam1983
Memory: 156K Time: 0MS
Language: C Result: Accepted

* Source Code

#include
// #include

#define e 0.00

int main()
{
float n;
float sum;
int i;
while(scanf("%f",&n)&&n!=e)
{
sum=0.00;
i=2;
while(sum {
sum+=(float)1/i++;
// printf("sum=%f i=%d\n",sum,i);getch();
}
printf("%d card(s)\n",i-2);
}
// getch();
return 0;
}

Solution 487-3279 (http://acm.pku.edu.cn/JudgeOnline/problem?id=1002)

Link đề bài: http://acm.pku.edu.cn/JudgeOnline/problem?id=1002

487-3279
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 140062 Accepted: 23847

Description
Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:

A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.

Input
The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.

Output
Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.

Sample Input

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279

Sample Output

310-1010 2
487-3279 4
888-4567 3

Source
East Central North America 1999

Source Code
Problem: 1002 User: ductam1983
Memory: 39356K Time: 797MS
Language: C++ Result: Accepted

* Source Code

#include
#include
using namespace std;

int phone[1000][10000];

int table['Z'];

void init_table()
{
int i, j;

table['0'] = 0;

table['1'] = 1; table['2'] = 2; table['3'] = 3;
table['4'] = 4; table['5'] = 5; table['6'] = 6;
table['7'] = 7; table['8'] = 8; table['9'] = 9;

table['A'] = 2; table['B'] = 2; table['C'] = 2;
table['D'] = 3; table['E'] = 3; table['F'] = 3;
table['G'] = 4; table['H'] = 4; table['I'] = 4;
table['J'] = 5; table['K'] = 5; table['L'] = 5;
table['M'] = 6; table['N'] = 6; table['O'] = 6;
table['P'] = 7; table['R'] = 7; table['S'] = 7;
table['T'] = 8; table['U'] = 8; table['V'] = 8;
table['W'] = 9; table['X'] = 9; table['Y'] = 9;

for(i = 0; i < 1000; i++)
for(j = 0; j < 10000; j++)
phone[i][j] = 0;
}

int main()
{
long n, i, j;
long num;
string s;
bool dupl;
scanf("%d ", &n);

init_table();
dupl = false;

while(n--)
{
getline(cin, s);
num = 0;
for(i = 0; i < s.length(); i++)
{
if(s[i] == '-') continue;

num = num * 10 + table[ s[i] ];
}

phone[num / 10000][num % 10000]++;
}

for(i = 0; i < 1000; i++)
{
for(j = 0; j < 10000; j++)
{
if(phone[i][j] > 1)
{
printf("%03d-%04d %d\n", i, j, phone[i][j]);
dupl = true;
}
}
}

if(!dupl) printf("No duplicates.\n");

}

Thứ Tư, 18 tháng 8, 2010

Solution Exponentiation (http://acm.pku.edu.cn/JudgeOnline/problem?id=1001)

Link đề bài: http://acm.pku.edu.cn/JudgeOnline/problem?id=1001
Exponentiation
Time Limit: 500MS
Memory Limit: 10000K
Total Submissions: 73736
Accepted: 17415

Description
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

Sample Input

95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

Hint
If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer

C++

while(cin>>s>>n)

{

...

}

c

while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want

/*while(scanf(%s%d",s,&n)!=EOF) //this also work */

{

...

}

Source
East Central North America 1988
Algorithm:
Xử lý chuỗi.
Code:
#include
// #include
#include


char s[7];
char t[100],kqua[100],u[100];
int n;
int ds;
int a[101];
int kq[101];


long int Pow(int m);

int main()
{
int i,j,l;
int flag;
int sotp;
char c='.';
long int x=0;
int dt;
int dung;
int r;
int pos;
while(scanf("%s%d",s,&n)==2)
{
for(i=0;i<=100;i++) a[i]=0;
r=100;
pos=100;
t[0]='�';
kqua[0]='�';
u[0]='�';
flag=0;
// printf("s=%s ",s);
// printf("n=%d ",n);
ds=strlen(s)-1;
l=ds;
while(s[l]=='0') l--;
s[++l]='�';
// printf("s=%s ",s);
ds=strlen(s)-1;
j=r;
i=ds;
while(i>-1)
{
// printf("s[%d]=%c ",i,s[i]);
if(s[i]!='.')
{
a[j]=s[i]-48;
j--;
}
else if(flag==0)
{
sotp=ds-i;
flag=1;
}
i--;
}
l=j+1;
// printf("l=%d r=%d ",l,r);
// for(i=l;i<=r;i++)printf("%d",a[i]);printf(" ");
x=0;
for(i=l;i<=r;i++) x+=a[i]*Pow(100-i);
// printf("x=%ld ",x);

kq[100]=1;

for(i=1;i<=n;i++)
{
long int du=0;
long int tam;
j=100;
while(j>=pos)
{
tam=kq[j]*x+du;
kq[j]=tam%10;
du=tam/10;
j--;
}
while(du!=0)
{
pos--;
kq[pos]=du%10;
du=du/10;
}
}
sotp=sotp*n;
if(sotp==0)
{
for(i=pos;i<=100;i++) printf("%d",kq[i]);printf(" ");
}
else
{
// printf("sotp=%d ",sotp);
j=-1;
for(i=pos;i<=100;i++) // printf("%d",kq[i]);printf(" ");
{
j++;
t[j]=kq[i]+48;
}
i=j;
while(t[i]=='0') i--;
j=i;
t[++j]='�';

j=-1;
if(sotp>100-pos+1)
{
int them=sotp-(100-pos+1);
u[++j]=c;
for(i=1;i<=them;i++)
{
j++;
u[j]='0';
}
u[++j]='�';
// printf("u=%s ",u);
strcpy(kqua,u);
strcat(kqua,t);
printf("%s ",kqua);
}
else
{
strcpy(kqua,t);
dt=strlen(t)-1;
dung=dt-sotp+1;
j=0;
for(i=1;i<=dung;i++)
{
printf("%c",kqua[j]);
j++;
}
printf("%c",c);
for(i=j;i<=dt;i++)
{
printf("%c",kqua[j]);
j++;
}
printf(" ");
}
}
}
// getch();
return 0;
}
long int Pow(int m)
{
long int kq=1;
int i;
for(i=1;i<=m;i++) kq*=10;
return kq;
}