杭电复试2016笔试题目解答

/*.判断一个数 N 是否是素数,是的话输出“YES”,否则输出“NO”。(判断 2-sqrt
(N)是否存在 N 的因子即可)

#include<cstdio>
#include<cmath>
bool isprime(int n){
	int sqr=(int)sqrt(n*1.0);
	for(int i=2;i<=sqr;i++){
		if(n%i==0) return false;
	}
	return true;
}

int main(){
	int n;
	scanf("%d",&n);
	if(isprime(n)) printf("是素数\n");
	else printf("不是素数\n");
	return 0;
}

在一个二维平面内有n个点,每个点坐标(x,y),求最近的两点之间的距离
可以直接用暴力法求解,即求任意两个点之间的距离赋给temp,如果temp<ans(存储最短距离),则ans=temp;
上述方法时间复杂度为O(n-1 + n-2 +… + 1)=O(n^2); O(nn)>O(n!)>O(2n)>O(n^2)… 幂指函数>阶乘>指数>幂函数

/*
#include<cstdio>
#include<cmath>

struct node{
	int x,y;
}Node[110];

//求两点之间的距离 
int count(node a,node b){
	return (int)sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int main(){
	int n;
	int ans=10000000;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d%d",&Node[i].x,&Node[i].y);
	} 
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			int temp=count(Node[i],Node[j]);
			if(temp<ans){
				ans=temp;
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}
*/

/*
3.有一个文件记录了学生期末考试的几门成绩和学号,求出这几门课程的平
均分和总分,并按照总分排序,从高到底,如果成绩相同,按照学号从小到大的
顺序。(文件要用 c 语言的读写操作,结构体排序可做)

/*
//学号 语文,数学,英语 
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

struct student{
	int chinese,math,english;
	char id[15];
	int average,total;//平均分和总分 
}stu[110];

bool cmp(student a,student b){
	if(a.total!=b.total) return a.total>b.total;
	else return strcmp(a.id,b.id)<0;//按学号字典序排序 
}

int main(){
	FILE *fp=fopen("student2.txt","r");
	if(fp==NULL){
		return 0;
	} 
	int cnt=0;
	while(fscanf(fp,"%s",stu[cnt].id)!=EOF){
		fscanf(fp,"%d%d%d",&stu[cnt].chinese,&stu[cnt].math,&stu[cnt].english);
		stu[cnt].total=stu[cnt].chinese+stu[cnt].math+stu[cnt].english;
		stu[cnt].average=stu[cnt].total/3;
		cnt++;
	}
	sort(stu,stu+cnt,cmp);
	for(int i=0;i<cnt;i++){
		printf("%s %d %d %d %d %d\n",stu[i].id,stu[i].chinese,stu[i].math,stu[i].english,stu[i].total,stu[i].average);
	}
	fclose(fp);
	return 0;
}
*/

题目略有更改,按照自己理解的来:
有一个由数字组成的二维矩阵,大小为 NM;还有一个大小为 nm 小二维
矩阵,想象将小二维矩阵整个与大矩阵某个位置对应,
在不同的位置,这两个二维矩阵对应位置的数字之差的绝对值的和一般是不同的,求
这个最小的和,并求出对应的大矩阵位置。(暴力求解,枚举大矩阵
的位置即可)
输入
4 4
1 2 3 4
4 5 6 8
1 2 3 4
5 6 7 8
2 2
2 2
4 5
输出:最小距离为0,对应的坐标起始点(0,0)、终点(1,1)。

*/
//按照自己的理解,有点类似于图像处理中的矩阵处理,用第二个矩阵大小的框在第一个矩阵上方滑动,求某一性质

#include<cstdio>
#include<cmath>
int matrix[110][110];
int templ[110][110]; 

int main(){
	int n,m,p,q;
	int start_x,start_y,end_x,end_y; 
	int	temp_x,temp_y;
	int ans=100000000;//ans存储最终的最小距离 
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			scanf("%d",&matrix[i][j]);
		}
	} 
	scanf("%d%d",&p,&q);
	for(int i=0;i<p;i++){
		for(int j=0;j<q;j++){
			scanf("%d",&templ[i][j]);
		}
	}
	//
	for(int i=0;i<=n-p;i++){
		int x=i;
		for(int j=0;j<=m-q;j++){
			int y=j;
			//进行p*q次的运算 ,即将窗口放在一个位置 
			int temp=0;//temp存储暂时的最小距离,
			for(int k=0;k<p;k++){
				for(int l=0;l<q;l++){
					temp+=abs(templ[k][l]-matrix[x][y++]);
					temp_y=y;
				}
				x++;
				 temp_x=x;
				y=j;//matrix矩阵从第j列开始 
			}
			x=i;
			if(temp<ans){
				ans=temp;//更新最小距离
				start_x=i;
				start_y=j;
				end_x=temp_x-1;//减1是因为多加了一个,自己写一遍就知道为啥减1了 
				end_y=temp_y-1;	 
			} 
		}
	} 
	
	printf("最小距离是:%d\n",ans);
	printf("起始点坐标:%d,%d   终止点坐标:%d,%d\n",start_x,start_y,end_x,end_y);
	return 0;
} 
发布了23 篇原创文章 · 获赞 62 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_42264284/article/details/105613194
今日推荐