杭电acm step 的题还真不错。
已知抛物线的顶点和两个与直线的交点,求相交的面积
View Code
/* 已知抛物线的顶点和两直线的交点,求相交的面积。 用微积分求面积, 简单的数学题. 求出k, h, a, b, c y1 = kx + h; y2 = a*x*x + b * x + c; 再求出f (x) = (y1 - y2)的原积函数(x2 ~ x3) area = F(x3) - F(x2). */ #include <stdio.h> #include <stdlib.h> int main() { int cas; double x1, y1, x2, y2, x3, y3, k, h, a, b, c, area; scanf("%d", &cas); while ( cas-- ) { scanf("%lf%lf", &x1, &y1); scanf("%lf%lf", &x2, &y2); scanf("%lf%lf", &x3, &y3); /* y = kx + h */ k = (y3 - y2) / (x3 - x2); h = y2 - k * x2; /* y = ax*x + b*x + c*/ a = (((y2 - y1) / (x2 - x1)) - ((y3 - y2) / (x3 - x2))) / (x1 - x3); b = ((y2 - y1) / (x2 - x1)) - (a * (x1 + x2)); c = y3 - (a * x3 * x3 + b * x3); area = ((a / 3)*x3*x3*x3 + ((b-k)/2)*x3*x3 + (c-h)*x3) - ((a / 3)*x2*x2*x2 + ((b-k)/2)*x2*x2 + (c-h)*x2); printf("%.2lf\n", area); } return 0; }
Sample Input
2
5.000000 5.000000
0.000000 0.000000
10.000000 0.000000
10.000000 10.000000
1.000000 1.000000
14.000000 8.222222
Sample Output
33.33
40.69
n的n次方最高位是几。
View Code
/* In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2. In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2. 对一个数num可写为 num=10n * a, 即科学计数法,使a的整数部分即为num的最高位数字,numnum=10n * a 这里的n与上面的n不等 两边取对数: num*lg(num) = n + lg(a); ∵a<10 ∴0<lg(a)<1 设x=n+lg(a) → n为x的整数部分,lg(a)为x的小数部分 ∵x=num*lg(num); ∴a=10(x-n) = 10(x-int(x))) 再取a的整数部分即得num的最高位 */ #include <iostream> #include <math.h> using namespace std; int main() { int t; while(cin>>t) { while(t--) { long n; cin>>n; double x=n*log10(n*1.0); x-=(__int64)x; int a=pow(10.0, x); cout<<a<<endl; } } return 0; }
Sample Input
2
3
4
Sample Output
2
2
Hint
In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2.
小数化分数
View Code
/* 将有限循环小数化为分数,刚看到这题表示没什么思路,百度了一下后表示思路就比清晰了。 1. a=0.(3) a*10=3+a; a*10-a=a*(10-1)=3; 故a=3/9=1/3; 2. a=0.( 47 ) a*100=47+a; a*100-a=a* ( 100-1 ) = 47; 故a=47/99, 3. a=0.32(56) b=a*100 =32 + 0.(56); c=a*10000 =3256 + 0.(56); c-b,答案已经很明显了 a*(10000-100)=3256-32 注意到b=a*pow(10,n), n 为循环节前的小数位个数 c=a*pow (10,m ), m为n加上循环节长度 */ #include<iostream> using namespace std; int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } void solve(char *s,int &a,int &b) { int t1=1,t2,i,k; a=0; for(i=2;s[i]&&s[i]!='(';++i) { a=a*10+s[i]-'0'; t1*=10; } b=a; t2=t1; if(s[i]!='(') { k=gcd(t1,a); a=a/k; b=t1/k; return; } for(++i;s[i]!=')';++i) { b=b*10+s[i]-'0'; t2*=10; } a=b-a; b=t2-t1; k=gcd(a,b); a/=k; b/=k; } int main() { char str[15]; int a,b,t; scanf("%d",&t); while(t--) { scanf("%s",str); solve(str,a,b); printf("%d/%d\n",a,b); } return 0; }
Input
第一行是一个整数N,表示有多少组数据。
每组数据只有一个纯小数,也就是整数部分为0。小数的位数不超过9位,循环部分用()括起来。
Output
对每一个对应的小数化成最简分数后输出,占一行。
Sample Input
3
0.(4)
0.5
0.32(692307)
Sample Output
4/9
1/2
17/52
In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.
所有质因数之和
View Code
#include <iostream> #include<cstdio> #include<string.h> #define N 500001 using namespace std; __int64 s[N+10]; int main() { int i,j,k,n,m; for(i = 1 ; i <= N/2 ; i++) { for(j=i+i ; j< N ; j+=i) s[j]+=i; } scanf("%d",&k); while(k--) { scanf("%d",&m); printf("%I64d\n",s[m]); } return 0; }
Sample Input
3
2
10
20
Sample Output
1
8
22