6-17acm课程题解

A.老王赛马
首先将渊子和对手的马分别排序,渊子胜利的条件是:只要渊子的马的中位数大于对手的最弱马即可(注意讨论马的个数是偶数的情况)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100100];
int b[100100];
int main(){
	 while(1){
	 	int n;
	 	cin>>n;
	 	if(n==0) break;
	 	for( int i=1;i<=n;i++){
	 		cin>>a[i];
		 }
		 for( int i=1;i<=n;i++){
		 	cin>>b[i];
		 }
		 sort(a+1,a+1+n);
		 sort(b+1,b+1+n);
		 if(n%2==0){
		 	if(a[n/2-1]>b[1]){
		 		cout<<"YES"<<endl;
			 }
			 else{
			 	cout<<"NO"<<endl;;
			 }
		 }
		 else{
		 	if(a[n/2+1]>b[1]){
		 		cout<<"YES"<<endl;
			 }
			 else{
			 	cout<<"NO"<<endl;
			 }
		 }
	 } 
}

B.一元三次方程求解
由于方程组解的范围是-100~+100,所以可以直接枚举所有的解,然后将解带回方程组。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	double a,b,c,d;
	cin>>a>>b>>c>>d;
	double exp=1e-5;
	double ans=-100;
	while(ans<=100){
		if(fabs(ans*ans*ans*a+b*ans*ans+c*ans+d)<exp){
			printf("%.2lf ",ans);
		}
		ans+=0.01;
	}	
}

C.图案生成(二)
直接模拟即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string a;
int main(){
	while(cin>>a){
		int t;
		t=a[0]-'A'+1;
		char now='A';
		int sp=t-1;
		for( int i=1;i<=t;i++){
			for( int j=1;j<=sp;j++){
				printf(" ");
			}
			for( int j=0;j<=now-'A';j++){
				printf("%c",'A'+j);
			}
			for( int j=now-'A'-1;j>=0;j--){
				printf("%c",'A'+j);
			}
			now++;
			for( int j=1;j<=sp;j++){
				printf(" ");
			}
			sp--;
			printf("\n");
		}
		printf("\n");
	}
}

D. TYVJ 1042 表达式计算3
使用python可以简化计算,如果使用c++用栈即可
c++解法点击此处

s=input();
s=s.replace("^","**");
s=s.replace('/','//');
s=eval(s)
print(s)

E.泡泡龙(pop)
使用广度优先搜索将所有连通的部分置为0,然后用栈模拟上面的数字向下面掉落。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[110][110];
int vis[110][110];
int ans[110][110];
struct p{
	int x,y;
};
queue<p>q;
int c;int n;
int rem;
int mov[4][2]={0,1,0,-1,1,0,-1,0};
int ju(p aaa){
	if(vis[aaa.x][aaa.y]==0&&a[aaa.x][aaa.y]==rem
	&&aaa.x>0&&aaa.x<=n&&aaa.y>0&&aaa.y<=n)
	return 1;
	else return 0;
}
void bfs(){
	while(!q.empty()) q.pop();
	p now;
	now.x=n;
	now.y=c;
	q.push(now);
	vis[now.x][now.y]=1;
	a[now.x][now.y]=0;
	while(!q.empty()){
		now=q.front();
		q.pop();
		for( int i=0;i<4;i++){
			p temp=now;
			temp.x+=mov[i][0];
			temp.y+=mov[i][1];
			if(ju(temp)){
				q.push(temp);
				vis[temp.x][temp.y]=1;
				a[temp.x][temp.y]=0; 
			}
		}
	}
}
int main(){
	cin>>n;
	cin>>c;
	for( int i=1;i<=n;i++){
		for( int j=1;j<=n;j++){
			scanf("%d",&a[i][j]);
		}
	}
	memset(vis,0,sizeof(vis));
	rem=a[n][c];
	bfs();
	for( int i=1;i<=n;i++){
		stack<int>st;
		while(!st.empty())st.pop();
 		for( int j=1;j<=n;j++){
			if(a[j][i]!=0){
				st.push(a[j][i]);
			}
		}
		for( int j=n;!st.empty();j--){
			ans[i][j]=st.top();
			st.pop();
		}
	}
	for( int i=1;i<=n;i++){
		for( int j=1;j<=n;j++){
			printf("%d ",ans[j][i]);
		}
		printf("\n");
	}
	
}

F.abcd的“树”
本题数据规模较小,可以直接暴力。
首先用邻接矩阵存储图,如果遇到修改指令,就dfs修改子节点的值。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int col[110];
int a[110][110];
	int n,m;
void cg(int x,int c){
	for( int i=1;i<=n;i++){
		if(a[x][i]==1){
			col[i]=c;
			cg(i,c);
		}
	}
}
int main(){
	cin>>n>>m;
	memset(a,0,sizeof(a));
	memset(col,-1,sizeof(col));
	for( int i=1;i<=n-1;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		a[y][x]=1;
	}
	for( int i=1;i<=m;i++){
		int temp;
		scanf("%d",&temp);
		if(temp==1){
			int x,c;
			scanf("%d%d",&x,&c);
			col[x]=c;
			cg(x,c);
		}
		else{
			int x;
			scanf("%d",&x);
			printf("%d\n",col[x]);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值