Codeforces 一

Codeforces 1

A. Theatre Square:

        题目地址:http://codeforces.com/contest/1/problem/A

        题目大意:n*m的长方形用a*a的正方形覆盖,允许超出长方形,问需要几个正方形。

        算法讨论:计算长和宽分别需要几个a,相乘即可。

        Code:

#include <cstdio>
#include <cmath>

using namespace std;

int n,m,a;

int main(){
	scanf("%d%d%d",&n,&m,&a);
	if (n%a==0) n/=a;else n=n/a+1;
	if (m%a==0) m/=a;else m=m/a+1;
	printf("%I64d\n",(long long)n*m);
	return 0;
}


B. Spreadsheets

        题目地址:http://codeforces.com/contest/1/problem/B

        题目大意:将1, 2, 3, ...按A, B, C, ..., AA, AB, ...标号,完成这2种标号的相互转换。

        算法讨论:

                首先先判断读入的某行是哪个询问,只需判断有几个字符和前一个字符的种类不同即可。

                其次先是数字到字母的转化,将A..Z看作1..26,然后看作26进制,将读入的26进制数转化乘10进制即可。

                然后是数字到字母的转化。设数字为x。对x作短除法的时候,若x%26==0,则对应的字母为‘Z’,x=x/26-1;否则1..25对应的字母为'A'..'Y',x=x/26。

        Code:

#include <cstdio>
#include <cstring>

#define N 10000

using namespace std;

int T,l,x,p,cnt,a[N+10];
char s[N+10];

bool type(char x){
	if (x>='0' && x<='9') return 0;
	else return 1;
}

int main(){
	scanf("%d",&T);
	for (int i=1;i<=T;++i){
		scanf("%s",s);
		l=strlen(s),cnt=0;
		for (int j=1;j<l;++j)
			if (type(s[j])!=type(s[j-1])) cnt++;
		if (cnt==1){
			x=p=0;
			for (int j=0;j<l;++j){
				if (type(s[j])!=type(s[j-1])){
					p=j;
					break;
				}
				x=x*26+s[j]-'A'+1;
			}
			printf("R");
			for (int j=p;j<l;++j) printf("%c",s[j]);
			printf("C%d\n",x);
		}
		else{
			x=p=0;
			for (int j=0;j<l;++j)
				if (s[j]=='C'){
					p=j;
					break;
				}
			for (int j=p+1;j<l;++j) x=x*10+s[j]-'0';
			l=0;
			while (x)
				if (x%26) a[l++]=x%26,x/=26;
				else a[l++]=26,x=x/26-1;
			for (int j=l-1;j>=0;--j) printf("%c",a[j]+'A'-1);
			for (int j=1;j<p;++j) printf("%c",s[j]);
			printf("\n");
		}
	}
	return 0;
}


C. Ancient Berland Circus

        题目大意:给出3个点,求最小面积的正多边形,使得这3个点为正多边形的顶点。

        算法分析:

                根据正多边形的性质,正多边形的每个顶点都在其外接圆上。

                已知3个点,可以根据海伦公式求出三角形的面积S。然后根据正弦定理求出外接圆的半径R=abc/(4S),根据余弦定理求出三个圆心角。

                求出三个圆心角的最大公约数A,则正多边形由2*pi/A个小三角形组成。

                根据正弦定理求出每个小三角形的面积S0,则答案即为S0*2*pi/A。

        Code:

#include <cstdio>
#include <cmath>

using namespace std;

const double pi=acos(-1),eps=1e-4;

double x[3],y[3],a,b,c,A,B,C,p,S,R,alpha,S0,n;

double fgcd(double a,double b){
	if (fabs(b)<eps) return a;
	return fgcd(b,fmod(a,b));
}

int main(){
	for (int i=0;i<3;++i) scanf("%lf%lf",&x[i],&y[i]);
	a=sqrt((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1]));
	b=sqrt((x[0]-x[2])*(x[0]-x[2])+(y[0]-y[2])*(y[0]-y[2]));
	c=sqrt((x[1]-x[2])*(x[1]-x[2])+(y[1]-y[2])*(y[1]-y[2]));
	A=2*acos((b*b+c*c-a*a)/2/b/c);
	B=2*acos((a*a+c*c-b*b)/2/a/c);
	C=2*pi-A-B;
	p=(a+b+c)/2;
	S=sqrt(p*(p-a)*(p-b)*(p-c));
	R=a*b*c/4/S;
	alpha=fgcd(fgcd(A,B),C);
	n=2*pi/alpha;
	S0=R*R*sin(alpha)/2;
	printf("%0.7lf\n",n*S0);
	return 0;
}


By Charlie Pan

Apr 30,2014