B-三等分角
Description 尺规作图三等分角是古希腊三大几何问题之一…
现在将这个问题稍作简化,给定三角形ABC,需要你在线段BC上找一点D使得: ( ∠BAC = 3 * ∠BAD )
Input
六个浮点数分别是x1,y1,x2,y2,x3,y3,分别代表点A(x1,y1),B(x2,y2), C(x3,y3)的坐标。Output
输出点D的坐标,结果保留两位小数。
Sample Input
0 0 0 1 1 1Sample Output
0.27 1.00
HINT
0<= x, y <=1e8
这题用二分可以少考虑很多东西(注意不要把三等分角和三等分点混淆)
版本1:暴力求解
#include <bits/stdc++.h>
using namespace std;
double PI=acos(-1);
long double aw(long double x1,long double y1,long double x2,long double y2) {
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
long double cv(long double a,long double b,long double c) {
return acos((b*b+c*c-a*a)/(2*b*c));
}
int main()
{
long double x1,y1,x2,y2,x3,y3;
scanf("%Lf%Lf%Lf%Lf%Lf%Lf",&x1,&y1,&x2,&y2,&x3,&y3);
long double a=aw(x2,y2,x3,y3);
long double b=aw(x1,y1,x3,y3);
long double c=aw(x1,y1,x2,y2);
long double Q=cv(a,b,c);
long double s1=x2,yy1=y2,s2=x3,yy2=y3;
while(fabs(s1-s2)>1e-5)
{
long double x,y;
x=(s1+s2)/2,y=(yy1+yy2)/2;
long double c1=aw(x1,y1,x2,y2);
long double a1=aw(x2,y2,x,y);
long double b1=aw(x1,y1,x,y);
long double Q1=cv(a1,b1,c1);
if(Q1>=Q/3) s2=x,yy2=y;
else s1=x,yy1=y;
}
printf("%.2Lf %.2Lf",s1,yy1);
return 0;
}
版本2:二分
#include <iostream>
#include <cmath>
#define eps 1e-5 //eps是二分精度,如果有问题就把他调得再小一些
using namespace std;
double x[4],y[4];
double lenth(double x1,double y1,double x2,double y2) {
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
inline double BAD(double ans_x,double ans_y) {
double a=lenth(x[1],y[1],x[2],y[2]);
double b=lenth(x[1],y[1],ans_x,ans_y);
double c=lenth(x[2],y[2],ans_x,ans_y);
return acos((a*a+b*b-c*c)/a/b/2.0);
}
int main()
{
for(int i=1; i<=3; i++) cin>>x[i]>>y[i];
double ans_x,ans_y,lx=x[2],rx=x[3],ly=y[2],ry=y[3];
double BAC = BAD(x[3],y[3]);
while(abs(rx-lx)>eps) {
ans_x=(lx+rx)/2;
ans_y=(ly+ry)/2;
if(BAD(ans_x,ans_y)*3>=BAC) rx=ans_x,ry=ans_y;
else lx=ans_x,ly=ans_y;
}
printf("%.2lf %.2lf\n",ans_x,ans_y);
return 0;
}
C-单科成绩排序
Description
n个学生,每人m门成绩,现在要求根据第i门成绩由高到低排序。
Input
第一行两个整数,分别是:n,m;
接下来n行,每行m个数,表示第i个人的第j门分数;
第n+2行,一个整数k,表示有k组询问;
接下来k行,每行一个整数t (1<=t<=m),表示根据第t门成绩排序.Output
根据第i门成绩排序后的n个人的名次.如果此门科目存在多个人的成绩相同,由序号从小到大输出。
Sample Input
4 3 95 53 68 41 70 65 33 53 91 66 41 99 2 2
3 Sample Output
2 1 3 4 4 3 1 2
HINT
1<=n<=10000;
1<=t<=m<=100;
1<=k<=100;
0<=分数<=100;
注意大数快读,自定义排序函数
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+10,M=2e5;
typedef long long LL;
inline int read() {
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x = x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline string readstring() {
string str;
char s=getchar();
while(s==' '||s=='\n'||s=='\r') {
s=getchar();
}
while(s!=' '&&s!='\n'&&s!='\r') {
str += s;
s=getchar();
}
return str;
}
struct f{int a[110],z,nu;}s[N];
bool cmp(struct f x,struct f y) {
if(x.z==y.z) return x.nu<y.nu;
return x.z>y.z;
}
int main()
{
int n,m;
n=read(),m=read();
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
scanf("%d",&s[i].a[j]);
s[i].nu=i;
}
int k; k=read();
while(k--)
{
int p;
p=read();
for(int i=1; i<=n; i++)
s[i].z=s[i].a[p];
sort(s+1,s+n+1,cmp);
for(int i=1; i<=n; i++)
printf("%d ",s[i].nu);
printf("\n");
}
return 0;
}
D-任意进制A+B
Sample Input
170 169 2 2 1 0 2 1 1Sample Output
1 0 1 0 1 0 1 0 0
HINT
((0){10} <= (a)x , (b)y <= (10^{17}){10} ) ((2){10} <= x , y , z <= (10^{17}){10} )
C++版
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+10;
ll x,y,z,a[N],b[N],s[N],Q1,Q2;
int la,lb;
int main()
{
scanf("%lld %lld %lld %d",&x,&y,&z,&la);
for(int i=la-1; i>=0; i--) cin>>a[i];
scanf("%d",&lb);
for(int i=lb-1; i>=0; i--) cin>>b[i];
for(int i=0; i<=la-1; i++) Q1 += a[i]*pow(x,i);
for(int i=0; i<=lb-1; i++) Q2 += b[i]*pow(y,i);
ll Q=Q1+Q2; int S=0;
while(Q) {
int t = Q%z;
s[++S] = t;
Q /= z;
}
for(int i=S; i>=1; i--) cout<<s[i]<<" ";
return 0;
}
JAVA版:直接把他们化成二进制,再变成c的进制
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
long a,b,c;
a=sc.nextLong();b=sc.nextLong();c=sc.nextLong();
int l1=sc.nextInt();
int x[]=new int[l1];
for (int i = 0; i < x.length; i++) {
x[i]=sc.nextInt();
}
int l2=sc.nextInt();
int y[]=new int[l2];
for (int i = 0; i < y.length; i++) {
y[i]=sc.nextInt();
}
long result1,result2;
result1=result2=0;
System.out.println(Arrays.toString(x));
System.out.println( Arrays.toString(y));
//
for (int i = 0; i < x.length; i++) {
result1+=x[i]*Math.pow(a, l1-1-i);
}
for (int i = 0; i < y.length; i++) {
result2+=y[i]*Math.pow(b, l2-1-i);
}
long ok=result1+result2;
System.out.println(Long.toString(ok,(int) c));
}
}
}
I-博弈论
Description:
给定两个正整数 M 和 N,从 Stan 开始,从其中较大的一个数,减去较小的数的正整数倍,当然,得到的数不能小于 0。然后是 Ollie,对刚才得到的数,和 M,N 中较小的那个数,再进行同样的操作……直到一个人得到了 0,他就取得了胜利。下面是他们用 (25,7) 两个数游戏的过程:
Start:(25,7)(25,7)
Stan:(11,7)(11,7)
Ollie:(4,7)(4,7)
Stan:(4,3)(4,3)
Ollie:(1,3)(1,3)
Stan:(1,0)(1,0)
Stan 赢得了游戏的胜利。
现在,假设他们完美地操作,谁会取得胜利呢?
Input
每行两个正整数 M,N(M,N<2^{31})。
Output
输出一行,如果 Stan
胜利,则输出 Stan wins;否则输出 Ollie wins。
Sample Input
24 15
Sample Output
Ollie wins
套板子,找规律
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[2021]={0};
int main()
{
int a,b,temp,ans=0;
cin>>a>>b;
if(a<b) swap(a,b);
while(b!=0)
{
if(a>2*b || a%b==0) break;
a -= b;
swap(a,b);
ans ^= 1;
}
if(ans) cout<<"Ollie wins"<<endl;
else cout<<"Stan wins"<<endl;
return 0;
}
L-最短路
Description
给定一个 n×m 的棋盘,上面有两种格子#和@。游戏的规则很简单:给定一个起始位置和一个目标位置,小明每一步能向上,下,左,右四个方向移动一格。如果移动到同一类型的格子,则费用是0,否则费用是1。计算从起始位置移动到目标位置的最小花费。
Input
多组数据
输入第一行包含两个整数n,m,分别表示棋盘的行数和列数,当n = 0 且 m = 0时结束。 输入接下来的n 行,每一行有 m 个格子(使用#或者@表示)。 输入接下来一行有四个整数(x_1, y_1, x_2, y_3)分别为起始位置和目标位置。
Output
输出从起始位置到目标位置的最小花费。
Sample Input
2 2
@#
#@
0 0 1 1
2 2
@@
@#
0 1 1 0 0 0
Sample Output
2 0
HINT
对于20%的数据满足:1≤n,m≤10。
对于40%的数据满足:1≤n,m≤300。
对于100%的数据满足:1≤n,m≤500。
用DFS好像会被卡…
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=501000,M=666;
const int dx[4]={0,-1,0,1}, dy[4]={-1,0,1,0};
struct node{int x,y;}lt[N];
int hy,ty,d[M][M],n,m,vx,vy,px,py;
char mp[M][M];
bool v[M][M];
int wm(int x,int y,int u,int v)
{
if(mp[x][y]!=mp[u][v])
return 1;
return 0;
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
for(int i=1; i<=n; i++) scanf("%s",mp[i]+1);
scanf("%d %d %d %d",&vx,&vy,&px,&py);
vx++; vy++; px++; py++;
memset(d,63,sizeof(d));
d[vx][vy]=0;
memset(v,false,sizeof(v));
lt[1].x=vx; lt[1].y=vy;
v[vx][vy]=true;
hy=1; ty=2;
while(hy!=ty)
{
node t=lt[hy];
for(int k=0; k<=3; k++)
{
int x=t.x+dx[k],y=t.y+dy[k];
if(x<1||x>n||y<1||y>m) continue;
int op=wm(t.x,t.y,x,y);
if(d[x][y]>d[t.x][t.y]+op)
{
d[x][y]=d[t.x][t.y]+op;
if(v[x][y]==false)
{
v[x][y]=true;
lt[ty].x=x; lt[ty].y=y;
ty++;
if(ty==n*m+1) ty=1;
}
}
}
v[t.x][t.y]=false; hy++;
if(hy==n*m+1) hy=1;
}
printf("%d\n",d[px][py]);
}
return 0;
}