The following program is my ACcepted code for PKU OJ 3122 Pie
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃PKU 3122 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
點這裡看題目 Click here to see this Problem!
//This program is for PKU 3122 Pie
//http://poj.org/problem?id=3122
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>//for the acos()
#define Pi acos(-1.0)
#define eps 1e-5
int N, F;//N is number of pies, F is number of friends
int r[10001];
/*
canbe function use to check whether this size is
@num record the number of pieces of pie if we use parameter "size" to separate pie.
@tmpNum is the number of each pie can be separated if we use parameter "size" to separate pie.
Because "tmpNum" is integer, the decimal point will be omit automatically.
Ex: if parameter "size" is bigger any pie, tmpNum will be 0 automatically.
if "num" is equal or bigger than (F+1)(including the author),
then this "size" would be possible, which is true for this function.
*/
bool canbe(double size)
{
int num = 0, tmpNum;
for(int i=0; i<N; i++)
{
tmpNum = (r[i] * r[i] * Pi)/size;
num += tmpNum;
}
if(num >= (F+1))
return true;
else
return false;
}
/*
findSize function use the binary search to search the maximum(largest) possible size
@the variable beg is the possible minimum size,
which is that only the largest pie will be separated in the party
@the variable end is the possible maximum size,
which is that we can use all the pies in the party, and there is no left pie.
*/
double findSize(double beg, double end)
{
double mid;
do
{
mid = (beg + end)/2.0;
//printf("before: %lf %lf %lf\n", beg, mid, end);//for check
if(canbe(mid)) beg = mid;
else end = mid;
//printf("after : %lf %lf %lf\n", beg, mid, end);//for check
}while( fabs(end - beg)>eps );
return end;
}
int main()
{
int t;//test case
double maxR = 0.0, sumR = 0.0;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &N, &F);
maxR = 0.0;
sumR = 0.0;
for(int i=0;i < N; i++)
{
scanf("%d", &r[i]);
maxR = r[i] > maxR ? r[i] : maxR;
sumR += r[i];// add all the radius
}
//printf("%lf %lf\n", (maxR * maxR * Pi)/(F+1), (sumR * sumR * Pi)/(F+1));
printf("%.4lf\n", findSize( (maxR * maxR * Pi)/(F+1), (sumR * sumR * Pi)/(F+1) ));
}
return 0;
}
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃PKU 3122 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
點這裡看題目 Click here to see this Problem!
//This program is for PKU 3122 Pie
//http://poj.org/problem?id=3122
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>//for the acos()
#define Pi acos(-1.0)
#define eps 1e-5
int N, F;//N is number of pies, F is number of friends
int r[10001];
/*
canbe function use to check whether this size is
@num record the number of pieces of pie if we use parameter "size" to separate pie.
@tmpNum is the number of each pie can be separated if we use parameter "size" to separate pie.
Because "tmpNum" is integer, the decimal point will be omit automatically.
Ex: if parameter "size" is bigger any pie, tmpNum will be 0 automatically.
if "num" is equal or bigger than (F+1)(including the author),
then this "size" would be possible, which is true for this function.
*/
bool canbe(double size)
{
int num = 0, tmpNum;
for(int i=0; i<N; i++)
{
tmpNum = (r[i] * r[i] * Pi)/size;
num += tmpNum;
}
if(num >= (F+1))
return true;
else
return false;
}
/*
findSize function use the binary search to search the maximum(largest) possible size
@the variable beg is the possible minimum size,
which is that only the largest pie will be separated in the party
@the variable end is the possible maximum size,
which is that we can use all the pies in the party, and there is no left pie.
*/
double findSize(double beg, double end)
{
double mid;
do
{
mid = (beg + end)/2.0;
//printf("before: %lf %lf %lf\n", beg, mid, end);//for check
if(canbe(mid)) beg = mid;
else end = mid;
//printf("after : %lf %lf %lf\n", beg, mid, end);//for check
}while( fabs(end - beg)>eps );
return end;
}
int main()
{
int t;//test case
double maxR = 0.0, sumR = 0.0;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &N, &F);
maxR = 0.0;
sumR = 0.0;
for(int i=0;i < N; i++)
{
scanf("%d", &r[i]);
maxR = r[i] > maxR ? r[i] : maxR;
sumR += r[i];// add all the radius
}
//printf("%lf %lf\n", (maxR * maxR * Pi)/(F+1), (sumR * sumR * Pi)/(F+1));
printf("%.4lf\n", findSize( (maxR * maxR * Pi)/(F+1), (sumR * sumR * Pi)/(F+1) ));
}
return 0;
}
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!