当前位置: 首页 > news >正文

数据结构与算法复习:第三十六弹

依旧是查漏补缺为主题~

昨天去迎新结果一个下午都呆在帐篷里,算起来三年迎新每一次都阴差阳错地参加了,感觉还是很有意义der(不过因为是下午场的原因,基本上见不到新生了,于是唠嗑中度过😃)

这两天都在订正之前光顾着刷题但是没有订正的部分,因为一开始写了前面的十几道,后来模拟是从后往前刷的,再回头看前面的一些错误觉得:很哇塞了😅,有一些确实是比较难,也有一些就是当时考虑不周导致测试点卡住,

修复的bug:1015,1009,1021
没有完全修复的bug:1010(24/25)

1. 1015 Reversible Primes

A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (<105) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

分析

首先判断输入的N是否是正素数,满足条件后再判断在指定D位下的逆转后的N是否还是素数,这个逆转后的数字转换成十进制可以在逆转的过程中就实现,代码如下:

#include<bits/stdc++.h>
using namespace std;
int N,D;
bool isPrime(int x){
	if(x<=1){
		return false;
	}
	if(x==2||x==3||x==5||x==7){
		return true;
	}
	for(int i=2;i*i<=x;i++){
		if(x%i==0){
			return false;
		}
	}
	return true;
}
int convert(int N,int D){
	int ans=0;
	//printf("%d",N);
	
	while(N){
		ans=ans*D+N%D;
		N/=D;
	}
	//printf("-%d\n",ans);
	return ans;
}
int main(){
	while(scanf("%d",&N)&&N>=0){
		scanf("%d",&D);
		if(isPrime(N)&&isPrime(convert(N,D))){
			printf("Yes\n");
		}else{
			printf("No\n");
		}
	}
	return 0;
} 

2. 1009 Product of Polynomials

This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1 a**N1 N2 a**N2 … N**K aNK

where K is the number of nonzero terms in the polynomial, N**i and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤N**K<⋯<N2<N1≤1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6

题目分析

一开始写的时候用了Map来避免重复访问0的问题,但是问题在于在多项式乘法个过程中,加和过程会导致产生0的情况,在这种情况下需要用erase删除后之前统计的对应的指数

#include<bits/stdc++.h>
using namespace std;
//多项式乘法
const int maxn=1001;
struct Poly{
	map<int,double>mp;
	int K;
}; 
Poly A,B,C;
int ep;double co;
int main(){
	//输入A 
	scanf("%d",&A.K);
	for(int i=0;i<A.K;i++){
		scanf("%d%lf",&ep,&co);
		A.mp[ep]=co;
	}
	//输入B
	scanf("%d",&B.K);
	for(int i=0;i<B.K;i++){
		scanf("%d%lf",&ep,&co);
		B.mp[ep]=co;
	} 
	//计算乘法
	for(map<int,double>::iterator ait=A.mp.begin();ait!=A.mp.end();ait++){
		for(map<int,double>::iterator bit=B.mp.begin();bit!=B.mp.end();bit++){
			co=ait->second*bit->second;
			ep=ait->first+bit->first;
			if(co==0){
				continue;
			}
			if(C.mp.find(-ep)==C.mp.end()){
				C.mp[-ep]=co;
			}else{
				C.mp[-ep]+=co;
				if(C.mp[-ep]==0){
					C.mp.erase(-ep);
				}
			}
		}
	} 
	//输出
	printf("%d",C.mp.size());
	for(map<int,double>::iterator cit=C.mp.begin();cit!=C.mp.end();cit++){
		printf(" %d %.1f",-cit->first,cit->second);
	} 
	return 0;
}

3. 1010 Radix

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:

N1 N2 tag radix

Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set { 0-9, a-z } where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number radix is the radix of N1 if tag is 1, or of N2 if tag is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print Impossible. If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

题目分析

首先得到给定radix的数字对应的十进制数,然后再逐个计算从2到100radix中哪一个使得另一个数字对应的十进制数等于第一个数字
订正思路主要是radix必须保证原始N2中每一个数字都要小于radix,否则停止计算直接返回false
订正后得分从19变成24,主要是第七个测试点的问题,看了题解说是需要用二分……emmmm好吧,分就分
24分代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
string N1,N2;
ull n1=0;
int tag,radix;
bool flag=true; 
bool ok(int radix){
	ull a=1,n2=0;
	for(int i=N2.length()-1;i>=0;i--){
		if(N2[i]<='9'&&N2[i]>='0'){
			if(N2[i]-'0'>=radix){
				return false;
			}
			n2=n2+(N2[i]-'0')*a;
		}else{
			if(N2[i]-'a'+10>=radix){
				return false;
			}
			n2=n2+(N2[i]-'a'+10)*a;
		}
		a*=radix;
	}
//	cout<<radix<<":"<<n2<<"\n";
	if(n2>n1){
		flag=false;
	}
	if(n2==n1){
		return true;
	}else{
		return false;
	}
}
int main(){
	cin>>N1>>N2>>tag>>radix;
	if(tag==2){
		swap(N1,N2);
	} 
	//N1的十进制的值 
	ull a=1;
	for(int i=N1.length()-1;i>=0;i--){
		if(N1[i]<='9'&&N1[i]>='0'){
			n1=n1+(N1[i]-'0')*a;
			if(N1[i]-'0'>=radix){
				printf("Impossible");
				return 0;
			}
		}else{
			if(N1[i]-'a'+10>=radix){
				printf("Impossible");
				return 0;
			}
			n1=n1+(N1[i]-'a'+10)*a;
		}
		a*=radix;
	}
//	cout<<"原本的数字:"<<n1<<"\n";
	for(int i=2;i<=100000;i++){
		if(ok(i)){
			printf("%d",i);
			return 0;
		}
		if(!flag){
			break;
		}
	}
    
	printf("Impossible");
	return 0;
}

二分代码如下【参考链接】

4. 1021 Deepest Root

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤104) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes’ numbers.

Output Specification:

For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print Error: K components where K is the number of connected components in the graph.

Sample Input 1:

5
1 2
1 3
1 4
2 5

Sample Output 1:

3
4
5

Sample Input 2:

5
1 3
1 4
2 5
3 4

Sample Output 2:

Error: 2 components

本题是求连通图的个数+树的直径求解
首先dfs判断连通图个数,大于1的则Error,在DFS过程中找出任意起点开始的最大深度导向的结点,存入ans数组中,之后同样的DFS过程对ans中任一结点操作得到新的ans数组,两次ans数组的并集即为答案。
至于为什么两次DFS就可以得到答案,是有证明der,这里不列了,记住就好~
代码如下:

#include<bits/stdc++.h>
using namespace std;
vector<vector<int> >graph;
vector<bool>vis;
int N,v1,v2,cnt=0;
int max_deep=1;
vector<int>ans;
void dfs(int x,int deep){
	deep++;
	vis[x]=true;
	bool flag=false;
	for(int i=0;i<graph[x].size();i++){
		if(!vis[graph[x][i]]){
			dfs(graph[x][i],deep);
			flag=true;
		}
	}
	if(!flag){
		if(deep>max_deep){
			max_deep=deep;
			ans.clear();
			ans.push_back(x);
		}else if(deep==max_deep){
			ans.push_back(x);
		}
	}
}
int main(){
	scanf("%d",&N);
	graph.resize(N+1);
	vis.resize(N+1,false);
	for(int i=0;i<N-1;i++){
		scanf("%d%d",&v1,&v2);
		graph[v1].push_back(v2);
		graph[v2].push_back(v1);
	}
	for(int i=1;i<=N;i++){
		if(!vis[i]){
			cnt++;
			dfs(i,0);
		}
	}
	if(cnt>1){
		printf("Error: %d components",cnt);
	}else{
		set<int>res;
		for(int i=0;i<ans.size();i++){
			res.insert(ans[i]);
		}
		int x=ans[0];
		ans.clear();
		fill(vis.begin(),vis.end(),false);
		dfs(x,0);
		for(int i=0;i<ans.size();i++){
			res.insert(ans[i]);
		}
		for(set<int>::iterator it=res.begin();it!=res.end();it++){
			printf("%d\n",*it); 
		}
	}
	return 0;
}

相关文章:

  • CSS - 响应式布局(一)媒体查询
  • 【JAVA预备】课程目录
  • 2022年0902Maven的依赖学习<第四课>
  • Android 11 上的文件读写权限(MANAGE_EXTERNAL_STORAGE)
  • Vue模板语法(01)
  • 世界第一台通用计算机:ENIAC
  • JAVA学习----HashSet类
  • 文章组合生成-免费文章组合生成软件
  • 华为面试应该怎么准备?
  • JDBC如何记忆
  • C语言之预处理
  • Flutter 系列---入门篇
  • 全球与中国汽车多楔带行业发展趋向分析及投资前景预测报告2022-2028年
  • Java IO流详解
  • 迷宫_Sarsa算法_边做边学深度强化学习:PyTorch程序设计实践(2)
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • 【刷算法】从上往下打印二叉树
  • FineReport中如何实现自动滚屏效果
  • Git的一些常用操作
  • Hibernate最全面试题
  • Java 内存分配及垃圾回收机制初探
  • mysql innodb 索引使用指南
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • SQLServer之索引简介
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • Vue2 SSR 的优化之旅
  • vue2.0项目引入element-ui
  • XForms - 更强大的Form
  • 从零开始学习部署
  • 关于List、List?、ListObject的区别
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 后端_MYSQL
  • 前端攻城师
  • 什么是Javascript函数节流?
  • 硬币翻转问题,区间操作
  • 再次简单明了总结flex布局,一看就懂...
  • Java总结 - String - 这篇请使劲喷我
  • ​TypeScript都不会用,也敢说会前端?
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #调用传感器数据_Flink使用函数之监控传感器温度上升提醒
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (04)odoo视图操作
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (pytorch进阶之路)扩散概率模型
  • (排序详解之 堆排序)
  • (三)mysql_MYSQL(三)
  • (一)Linux+Windows下安装ffmpeg
  • (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • /etc/sudoer文件配置简析
  • :O)修改linux硬件时间