[解题报告]训练日记3

在这里插入图片描述

A

比较坑的一道题,肯定要用string去存数(貌似数字的位数都很大)

然后考虑几个特殊的地方:0.00和0 0005和5等等

就算是对string类型的数字化简之后再对比吧

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

int T;
char A[100005] , B[100005];

string sol(char x[]) {
    
    
	int len = strlen(x+1) , top = 1 , point , lent; string s="" , t="";
	while(x[top]=='0')
		top++;
	for(int i=top; i<=len; i++) {
    
    
		if(x[i]=='.') {
    
    
			point = i;
			break;
		}
		s += x[i];
	}
	for(int j=point+1; j<=len; j++) 
		t += x[j];
	lent = t.length();
	if(lent==0)
		return s;
	while(t[lent-1]=='0')
		lent--;
	if(lent>=1)
		s += ".";
	for(int i=0; i<lent; i++)
		s += t[i];
	return s;
}

int main() {
    
    
	//fin
	while(scanf("%s%s",A+1,B+1)!=EOF) {
    
    
		if(sol(A)==sol(B))
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
} 

B

模拟题(没有一遍对,调起来比较费劲

在换位置的时候不应该直接对4取模,因为可能会出现0方向(我的代码我的错误)

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
#define L 105
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

struct Node {
    
    
	int x , y , f;
};
Node r[L];
int T , a , b , n , m , flag , type , up , minn;

int change(char f) {
    
    
	if(f=='N')
		return 1;
	if(f=='E')
		return 2;
	if(f=='S')
		return 3;
	if(f=='W')
		return 4;
}

void turn_left(int num , int step) {
    
    
	r[num].f = (r[num].f - step + 4) % 4;
	if(r[num].f==0)
		r[num].f = 4;
}

void turn_right(int num , int step) {
    
    
	r[num].f = (r[num].f + step) % 4;
	if(r[num].f==0)
		r[num].f = 4;
}

void move_r(int num , int step) {
    
    
	minn = step;
	if(r[num].f==1||r[num].f==3) {
    
    
		if(r[num].f==1) {
    
    
			for(int i=1; i<=n; i++) {
    
    
				if(r[i].y<=r[num].y)
					continue;
				if(r[i].y-r[num].y<=minn&&r[i].x==r[num].x) {
    
    
					type = 2;
					minn = r[i].y - r[num].y;
					up = i;
					flag = num;
				}
			}
			if(type==2)
				return ;
			if(r[num].y+step>b) {
    
    
				flag = num;
				type = 1;
				return ;
			} else
				r[num].y += step;
		}	
		if(r[num].f==3) {
    
    
			for(int i=1; i<=n; i++) {
    
    
				if(r[i].y>=r[num].y)
					continue;
				if(r[num].y-r[i].y<=minn&&r[i].x==r[num].x) {
    
    
					type = 2;
					up = i;
					minn = r[num].y - r[i].y;
					flag = num;
				}
			}
			if(type==2)
				return ;
			if(r[num].y-step<1) {
    
    
				flag = num;
				type = 1;
				return ;
			} else
				r[num].y -= step;
		}	
	}
	if(r[num].f==2||r[num].f==4) {
    
    
		if(r[num].f==2) {
    
    
			for(int i=1; i<=n; i++) {
    
    
				if(r[i].x<=r[num].x)
					continue;
				if(r[i].x-r[num].x<=minn&&r[i].y==r[num].y) {
    
    
					type = 2;
					up = i;
					minn = r[i].x-r[num].x;
					flag = num;
				}
			}
			if(type==2)
				return ;
			if(r[num].x+step>a) {
    
    
				flag = num;
				type = 1;
				return ;
			} else
				r[num].x += step;
		}
		if(r[num].f==4) {
    
    
			for(int i=1; i<=n; i++) {
    
    
				if(r[i].x>=r[num].x)
					continue;
				if(r[num].x-r[i].x<=minn&&r[i].y==r[num].y) {
    
    
					type = 2;
					up = i;
					minn = r[num].x-r[i].x;
					flag = num;
				}
			}
			if(type==2)
				return ;
			if(r[num].x-step<1) {
    
    
				flag = num;
				type = 1;
				return ;
			} else
				r[num].x -= step;
		}	
	}
}

void sol(int num , char f , int step) {
    
    
	if(f=='L') {
    
    
		turn_left(num , step%4);
		return ;
	}
	if(f=='R') {
    
    
		turn_right(num , step%4);
		return ;
	}
	move_r(num , step);
}

int main() {
    
    
//	fin
	read(T);
	while(T--) {
    
    
		read(a); read(b);
		read(n); read(m);
		flag = 0; type = 0;
		for(int i=1; i<=n; i++) {
    
    
			char f;
			read(r[i].x);
			read(r[i].y);
			cin>>f;
			r[i].f = change(f);
		}
		for(int i=1; i<=m; i++) {
    
    
			int num , step; char f;
			read(num);
			cin>>f;
			read(step);
			if(flag)
				continue;
			sol(num , f , step);
		}
		if(!type)
			printf("OK\n");
		if(type==1)
			printf("Robot %d crashes into the wall\n",flag);
		if(type==2)
			printf("Robot %d crashes into robot %d\n",flag,up);			 
	} 
	return 0;
}

C

先打表,把斐波那契数列的前多少项打出来(表中的最后一个数一定是小于输入的数字的最大的数)

然后因为我们不知道要几个数字相乘,所以dfs搜索

扫描二维码关注公众号,回复: 13136512 查看本文章
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
#define L 100005
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

int T , n , num[L] , maxx = -1 , f[105] , flag , top;

void init() {
    
    
	top = 1;
	f[0] = 0; f[1] = 1;
	while(1) {
    
    
		if(f[top]>=maxx)
			break;
		 top++;
		 f[top] = f[top-1] + f[top-2];
	}
}

void dfs(LL cnt , LL sum) {
    
    
	if(sum==1||flag) {
    
    
		flag = 1;
		return ;
	}
	if(cnt<=2)
		return ;
	if(sum%f[cnt]==0)
		dfs(cnt , sum/f[cnt]);
	dfs(cnt-1 , sum);
}

int main() {
    
    
	//fin
	read(T);
	for(int i=1; i<=T; i++) {
    
    
		read(num[i]);
		maxx = max(maxx , num[i]);
	}
	init();
	for(int i=1; i<=T; i++) {
    
    
		if(num[i]==0) {
    
    
			printf("Yes\n");
			continue;
		}
		flag = 0;
		dfs(top , num[i]);
		if(flag)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}

D

高精度加法 板子题

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

int T;
string A , B;

void print(int a[] , int len) {
    
    
	len++;
	while(!a[len]&&len>1)
		len--;
	for(int i=len; i>0; i--)
		printf("%d",a[i]);
}

void add(string A , string B) {
    
    
	int a[1005] , b[1005] , len , len1 , len2;
	memset(a , 0 , sizeof(a));
	memset(b , 0 , sizeof(b));
	len1 = A.length();
	len2 = B.length();
	for(int i=1; i<=len1; i++)
		a[i] = A[len1-i]-'0';
	for(int i=1; i<=len2; i++)
		b[i] = B[len2-i]-'0';
	len = max(len1 , len2);
	for(int i=1; i<=len; i++) {
    
    
		a[i] += b[i];
		a[i+1] += a[i]/10;
		a[i] %= 10;
	}
	print(a , len);
}

int main() {
    
    
	//fin
	read(T);
	for(int i=1; i<=T; i++) {
    
    
		cin>>A>>B;
		printf("Case %d:\n",i);
	//	1 + 2 = 3
		cout<<A<<" + "<<B<<" = ";
		add(A , B);
	//	cout<<endl;
		if(i!=T)
			cout<<endl<<endl;
		else
			cout<<endl;
	}
	return 0;
}

E

很明显具有单调性,要二分

一个坑点:输出结果不四舍五入,看代码咯

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
using namespace std;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
#define eps 1e-7
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

LL n , k;
double num[10005] , sum , l , r , mid;

int jud(double x) {
    
    
	LL cnt = 0;
	for(int i=1; i<=n; i++)
		cnt = cnt + (LL)(num[i]/x);
	if(cnt>=k)
		return 1;
	return 0;
}

int main() {
    
    
	//fin
	scanf("%lld%lld",&n,&k);
	sum = 0;
	for(int i=1; i<=n; i++) {
    
    
		scanf("%lf",&num[i]);
		sum += num[i];		
	}
	l = 0; r = sum/k;
	while(fabs(l-r)>eps) {
    
    
		mid = (l+r)/2;
		if(jud(mid))
			l = mid;
		else
			r = mid;
	}
	printf("%.2lf\n",floor(r*100)/100);
	return 0;
}

F

好像这个题的本意是三分来吧

函数求导求出表达式,用数学公式做就行啊

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
using namespace std;
#define PI acos(-1.0)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
#define eps 1e-7
#define L 10005
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

double n , r , v , h;

int main() {
    
    
//	fin
	while(scanf("%lf",&n)!=EOF) {
    
    
		r = sqrt(n/4/PI);
		h = sqrt(pow((3*n)/(4*PI*r),2)- r * r);
		v = PI * r * r * h / 3;
		printf("%.2lf\n%.2lf\n%.2lf\n",v,h,r);
	}
	return 0;
}

G

水题

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
#define L 10005
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

int n , num[L];

int main() {
    
    
	//fin
	read(n);
	for(int i=1; i<=n; i++)
		read(num[i]);
	sort(num+1 , num+n+1);
	printf("%d",num[n/2+1]);
	return 0;
}

H

这个题… 还是比较恶心的

我的输出竟然有-0.00(不知道OJ上评测怎么算

还有就是精度不够,所以我就用类似E题的写法控制了一下精度(这种写法我也不知道对不对 就瞎写 感觉零越多 越准确

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
using namespace std;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

struct Node {
    
    
	double k , m;
};
int n;
int x1 , x2 , x3 , x4 , yy1 , y2 , y3 , y4;

Node line(int a1 , int b1 , int a2 , int b2) {
    
    
	Node x;
	if(a1==a2) {
    
    
		x.k = INF;
		x.m = a1;
	} else {
    
    
		x.k = (b2-b1)*1.0/(a2-a1);
		x.m = b2-x.k*a2;
	}
	return x;
}

void sol() {
    
    
	Node l1 , l2;
	l1 = line(x1 , yy1 , x2 , y2);
	l2 = line(x3 , y3 , x4 , y4);
	if(l1.k==INF||l2.k==INF) {
    
    
		if(l1.k==INF&&l2.k==INF) {
    
    
			if(l1.m==l2.m)
				printf("LINE\n");
			else
				printf("NONE\n");
			return ;
		}
		if(l1.k==INF&&l2.k!=INF) {
    
    
			printf("POINT %.2f %.2f\n",l1.m,l2.k*l1.m+l2.m);
			return ;
		}
		if(l1.k!=INF&&l2.k==INF) {
    
    
			printf("POINT %.2f %.2f\n",l2.m,l1.k*l2.m+l1.m);
			return ;
		}
	}
	if(l1.k==l2.k) {
    
    
		if(l1.m==l2.m)
			printf("LINE\n");
		else
			printf("NONE\n");
	} else {
    
    
		double x = (l2.m-l1.m)/(l1.k-l2.k);
		if(x==-0)
			x = 0;
		printf("POINT %.2f %.2f\n",ceil(x*10000)/10000,l1.k*x+l1.m);
	}
}

int main() {
    
    
//	fin
	read(n);
	printf("INTERSECTING LINES OUTPUT\n");
	for(int i=1; i<=n; i++) {
    
    
		scanf("%d%d%d%d%d%d%d%d",&x1,&yy1,&x2,&y2,&x3,&y3,&x4,&y4);
		sol();
	}
	printf("END OF OUTPUT\n");
	return 0;
} 

I

也是高精加,但是是四个数相加

所以直接把D题稍微改改就行

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
using namespace std;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin);
#define LL long long
#define INF 2147483647
void read(int &x) {
    
    
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}

int T;
string A , B , C , D;

string change(int x) {
    
    
	stringstream ss;
	ss<<x;
    return ss.str();
}

string print(int a[] , int len) {
    
    
	len++;
	while(!a[len]&&len>1)
		len--;
	string s="";
//	cout<<s<<endl;
	for(int i=len; i>0; i--)
		s += change(a[i]);
	return s;
//	cout<<s;
	//	printf("%d",a[i]);
}

string add(string A , string B) {
    
    
	int a[1005] , b[1005] , len , len1 , len2;
	memset(a , 0 , sizeof(a));
	memset(b , 0 , sizeof(b));
	len1 = A.length();
	len2 = B.length();
	for(int i=1; i<=len1; i++)
		a[i] = A[len1-i]-'0';
	for(int i=1; i<=len2; i++)
		b[i] = B[len2-i]-'0';
	len = max(len1 , len2);
	for(int i=1; i<=len; i++) {
    
    
		a[i] += b[i];
		a[i+1] += a[i]/10;
		a[i] %= 10;
	}
	return print(a , len);
}

int main() {
    
    
	//fin
	read(T);
	for(int i=1; i<=T; i++) {
    
    
		cin>>A>>B>>C>>D;
	//	printf("Case %d:\n",i);
	//	1 + 2 = 3
	//	cout<<A<<" + "<<B<<" = ";
	//	add(A , B);
	//	cout<<endl;
	/*	if(i!=T)
			cout<<endl<<endl;
		else
			cout<<endl;*/
		cout<<add(add(A , B) , add(C , D))<<endl;
	}
	return 0;
}

题目整体难度不大,但是给人一种“我觉得我的代码很对,为什么就不能过”的感觉

哈哈哈 我还是有些地方考虑不到 而且模拟的细节处理不到位等问题 QAQ

猜你喜欢

转载自blog.csdn.net/cls1277/article/details/109770489
今日推荐