灵动ICPC冬令营基础-3
A - Birthday Cake
一开始交了好多都是错的,要考虑斜率为0和不存在的情况。
#include<cstdio>
int main(){
int A,B,x[1000],y[1000],t;
int n,I,J;
int a,b;
while(scanf("%d",&n)&&n){
for(int i=1;i<=2*n;i++) scanf("%d%d",&x[i],&y[i]);
for(A=-500;A<=500;A++){
for(B=-500;B<=500;B++){
I=J=0;t=0;
if(A*B==0) continue;
for(int i=1;i<=2*n;i++){
if(x[i]>100||x[i]<-100||y[i]>100||y[i]<-100) continue;
else if((A*x[i]+B*y[i])>0) I++;
else if((A*x[i]+B*y[i])<0) J++;
else if((A*x[i]+B*y[i])==0) break;
}
if(J==I&&I+J==2*n){
a=A,b=B;
t=1;
break;
}
}
if(t==1) break;
}
printf("%d %d\n",a,b);
}
return 0;
}
B - Is This Integration ?
数学题,计算出各个阴影的面积进行了。
#include<cstdio>
#include<cmath>
#define pi acos(-1)
int main(){
double a,x,y,z;
while( ~scanf("%lf",&a) )
{
z=a*a*(1.0-pi/6.0-0.25*sqrt(3.0));
y=a*a-0.25*pi*a*a-2.0*z;
x=a*a-4*y-4*z;
printf("%.3lf %.3lf %.3lf\n",x,4.0*y,4.0*z);
}
return 0;
}
C - Simple division
如果两个不同的数除以一个除数的余数相同,则这两个不同数的差值一定是除数的倍数。利用差值枚举除数即可。用欧几里得算法。
#include<cstdio>
#include<cstdlib>
int gcd(int a, int b) {
if(b==0) return a ;
return gcd(b, a % b);
}
int main()
{
int a[1005];
int tmp, tmp2, i, n, g;
while (scanf("%d", &tmp), tmp)
{
for (i = 0; scanf("%d", &tmp2), tmp2; ++i)
a[i] = tmp2 - tmp, tmp = tmp2;
n = i;
for (i = 0; a[i] == 0; ++i)
;
g = a[i++];
for (; i < n; ++i)
if (a[i]) g = gcd(a[i], g);
printf("%d\n", abs(g));
}
return 0;
}
D - Euclid Problem
用扩展的欧几里得算法。即:
int exgcd(int a, int b, int &x, int &y)
{
if (b==0) {x=1; y=0; return a;}
int t=exgcd(b, a%b, x, y);
int x0=x, y0=y;
x=y0; y=x0-(a/b)*y0;
return t;
}
#include<cstdio>
#include<cmath>
int exgcd(int a, int b, int &x, int &y)
{
if (b==0) {x=1; y=0; return a;}
int t=exgcd(b, a%b, x, y);
int x0=x, y0=y;
x=y0; y=x0-(a/b)*y0;
return t;
}
int main(){
int a,b,x,y,r;
while(scanf("%d%d",&a,&b)!=EOF)
{
r=exgcd(a,b,x,y);
printf("%d %d %d\n",x,y,r);
}
return 0;
}
E - Dead Fraction
Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by “…”. For instance, instead of “1/3” he might have written down “0.3333…”. Unfortunately, his results require exact fractions! He doesn’t have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions.
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).
题目大意为将无限循环小数化为分数但因循环部分有多种情况有点复杂就不想写了,数学的推导也理解了,但写出来有点困难。
F - What is the Probability ?
概率题,高中数学问题,只要计算出公式就可以了。
#include<cstdio>
int main(){
int s,n,i;
double p;
scanf("%d",&s);
while(s--){
scanf("%d%lf%d",&n,&p,&i);
double q=1-p;
double t=q;
for(int j=1;j<n;j++){
t*=q;
}
double a=1;
for(int j=1;j<i;j++){
a*=q;
}
printf("%.4lf\n",p==0?0:p*a/(1-t));
}
return 0;
}
G - Burger
高中数学题,相当于二项式,还有组合数,想要推一下公式还是有点复杂的。
#include<cstdio>
#include<cmath>
double f[500001];
int main(){
int i,n,a;
f[1]=1;
for(i=1;i<50000;i++)
{
f[i+1]=f[i]*(2*i-1)/(2*i);
}
scanf("%d",&n);
while(n--)
{
scanf("%d",&a);
printf("%.4lf\n",1-f[a/2]);
}
return 0;
}
H - Coin Toss
对于落的地方一共有五种可能,分别求出这五种可能对应的式子就行了。
#include<cstdio>
#include<cmath>
const double PI=acos(-1.0);
int main(){
double m,n,t,c,ans[5];
int i,cnt=1;
scanf("%d",&i);
while(i--)
{
scanf("%lf%lf%lf%lf",&m,&n,&t,&c);
ans[0]=(t-c)*(t-c)*(m-2)*(n-2)+(t-c/2)*(t-c/2)*4+(t-c/2)*(t-c)*(m*2+n*2-8);
ans[2]=(c*c-(c/2)*(c/2)*PI)*(m-1)*(n-1);
ans[3]=(c/2)*(c/2)*PI*(m-1)*(n-1);
ans[1]=t*t*m*n-ans[0]-ans[2]-ans[3];
printf("Case %d:\n",cnt++);
printf("Probability of covering 1 tile = %.4lf%%\n",ans[0]*100.0/(n*m*t*t));
for(int i=1;i<4;i++)
{
printf("Probability of covering %d tiles = %.4lf%%\n",i+1,ans[i]*100.0/(n*m*t*t));
}
printf("\n");
}
return 0;
}
I - 498-bis
对于一开始想的是将第二行放到一个数组里,但题目没说范围可能不够,输入也有一定的困难,然后学长推出了一个公式可以读入一个就计算一次太厉害了。
#include<cstdio>
int main(){
int n,a;
char c;
while(~scanf("%d",&n))
{
int ans=0,x=0;
while(~scanf("%d",&a))
{
ans=ans*n+x;
x=x*n+a;
scanf("%c",&c);
if(c=='\n') break;
}
printf("%d\n",ans);
}
return 0;
}