杭电计算机2016年机试真题汇总

杭电计算机2016年机试真题汇总

  1. 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;
}

}


  1. 在一个二维平面内有n个点,每个点坐标为(x,y),求最近的两点的距离。(暴力求解即可)
    输入:
    5
    1 2
    100 200
    1000 2000
    1000 1
    1 3
    输出:
    1

思路:

  1. 一个平面内有n个点,那我只需要进行两个for循环即可,分别算出该点与其他点的距离差。

  2. 将上个过程中的距离差,使用数组存储起来。

  3. 对这个数组进行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);
    }
    

  1. 有一个文件记录了学生期末考试的几门成绩和学号,求出这几门课程总分,并按照总分排序,从高到底,如果成绩相同,按照学号从小到大的顺序。(文件要用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;
}


  1. 有一个由数字组成的二维矩阵,大小为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年的题目十分的相似

    1. 构造一个模块儿count用来计算两个二维矩阵对应位置的差和.
      题意没弄明白,但是大概思路还是比较清晰的,你首先要构造一个模块儿实现题目要求指定的功能。

    2. 类似于17年的dfs对这道题进行深度搜索

      1. 递归出口是程序已经运行到了最后一行。
      2. 该行的每一个位置进行count模块儿计算,并将这个数据存储到一个二维数组里面,该二维数组的坐标刚好是图的坐标
    3. 将得到的二维数组进行排序,输出值最小的,数组的坐标。(这一步要做一个控制,因为很有可能会有多个同样最小的坐标),把这些坐标输出出来,我们就能得到题目要求的答案。

      #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;

      }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值