杭电计算机2016年机试真题汇总
- 1.判断一个数N是否是素数,是的话输出“YES”,否则输出“NO”。(判断2-sqrt(N)是否存在N的因子即可)
输入:
1000000007
100
输出:
YES
NO
#include<stdio.h>
#include
#include
#include
#include<bits/stdc++.h>
using namespace std;
//21:56
int main()
{
int N,flag=1;//默认是素数
while(cin>>N)
{
flag=1;
if(N<=1)flag=0;
for(int i=2;i<=sqrt(N);i++)
{
if (N%i==0)
{
flag=0;
break;
}
}
if(flag==0)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
- 在一个二维平面内有n个点,每个点坐标为(x,y),求最近的两点的距离。(暴力求解即可)
输入:
5
1 2
100 200
1000 2000
1000 1
1 3
输出:
1
思路:
-
一个平面内有n个点,那我只需要进行两个for循环即可,分别算出该点与其他点的距离差。
-
将上个过程中的距离差,使用数组存储起来。
-
对这个数组进行sort排序,输出最小的距离值即可
#include<stdio.h>
#include
#include
#include
#include<bits/stdc++.h>
using namespace std;typedef struct {
int x;
int y;
}seet;//15:10
int main()
{
seet temp;
vectors1;
int N;
cin>>N;
for(int i=1;i<=N;i++)
{
cin>>temp.x>>temp.y;
s1.push_back(temp);
}
float Min=pow(s1[1].x-s1[0].x,2)+pow(s1[1].y-s1[0].y,2);
float t;
for(int i=0;i<N;i++)
{
for(int j=i+1;j<N;j++)
{
t =pow(s1[i].x-s1[j].x,2)+pow(s1[i].y-s1[j].y,2);
if(t<Min)
Min=t;} } cout<<sqrt(Min); }
- 有一个文件记录了学生期末考试的几门成绩和学号,求出这几门课程总分,并按照总分排序,从高到底,如果成绩相同,按照学号从小到大的顺序。(文件要用c语言的读写操作,结构体排序可做)
为了解决这一道题目,我们首先要解决数据问题,因为这道题的数据是有关文件的读写的,我们可以自己创建一个文件,类似于下图:
| 姓名 | 学号 | 语文 | 数学 | 英语 |
| :–: | :–: | :–: | :–: | :–: |
| A | 1 | 20 | 40 | 40 |
| B | 2 | 66 | 39 | 68 |
| C | 3 | 99 | 5 | 5 |
#include<stdio.h>
#include
#include
#include
#include<bits/stdc++.h>
using namespace std;
typedef struct
{
string ID;
int num;
int chinese;
int math;
int english;
int score;
}student;
//15:10
bool cmp(student A,student B)
{
if(A.score==B.score)
return A.ID<B.ID;
else
return A.score>B.score;
}
int main()
{
vectors1;
student temp;
int N;
cin>>N;
for(int i=1;i<=N;i++){
cin>>temp.ID>>temp.num>>temp.chinese>>temp.math>>temp.english;
temp.score=temp.chinese+temp.math+temp.english;
s1.push_back(temp);
}
sort(s1.begin(),s1.end(),cmp);
for(int i=0;i<N;i++)
cout<<s1[i].ID<<s1[i].num;
}
-
有一个由数字组成的二维矩阵,大小为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,对应的坐标起始点(1,1)、(3,1)。思路:
这道题和17年的题目十分的相似-
构造一个模块儿count用来计算两个二维矩阵对应位置的差和.
题意没弄明白,但是大概思路还是比较清晰的,你首先要构造一个模块儿实现题目要求指定的功能。 -
类似于17年的dfs对这道题进行深度搜索
- 递归出口是程序已经运行到了最后一行。
- 该行的每一个位置进行count模块儿计算,并将这个数据存储到一个二维数组里面,该二维数组的坐标刚好是图的坐标
-
将得到的二维数组进行排序,输出值最小的,数组的坐标。(这一步要做一个控制,因为很有可能会有多个同样最小的坐标),把这些坐标输出出来,我们就能得到题目要求的答案。
#include<bits/stdc++.h>
using namespace std;vector<vector>arr;
vector<vector>brr;
int N,M,n,m;
typedef struct {
int x;
int y;
}seet;
int search(int x,int y)
{
int cha,sum=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cha=abs(arr[x+i][y+j]-brr[i][j]);
sum+=cha;
}
}
return sum;
}
int main()
{
cin>>N>>M;
int i,j;
arr.resize(N);
for(i=0;i<N;i++)
arr[i].resize(M);
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
cin>>arr[i][j];
}
}
cin>>n>>m;
brr.resize(n);
for(i=0;i<n;i++)
brr[i].resize(m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>brr[i][j];
}
}
vectors1;
long long Min=2e18;
for(i=0;i<=N-n;i++)
{
for(j=0;j<=M-m;j++)
{
int temp=search(i,j);
seet fou;
fou.x=i;
fou.y=j;
if(temp<Min)
{
Min=temp;
while(!s1.empty())
s1.pop_back();
s1.push_back(fou);
}
else if(temp==Min)
{
s1.push_back(fou);
}
}
}
for(int i=0;i<s1.size();i++)
cout<<s1[i].x<<s1[i].y<<endl;}
-

398

被折叠的 条评论
为什么被折叠?



