2019西工大机试题全

第一题:

一组整数,由小到大排序,有n组排序数,每组8个,排序输出;

例如输入;

2

2 1 3 5 4 6 7 8

11 33 44 22 55 77 66 88

输出

1 2 3 4 5 6 7 8

11 22 33 44 55 66 77 88

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,i=0;
    cin>>n;
while(i<n)
{
    int b[8]={0};
    for(int j=0;j<8;j++)
    { cin>>b[j];}
    for(int j=0;j<8;j++)
    {
        sort(b,b+8);
        cout<<b[j];

    }
    i++;
    cout<<""<<endl;
}

return 0;
}

第二题:

海伦公式求面积,先判断是否是三角形,不是输出NaN,是输出面积;

例如:输入

2

1 2 3

4 5 6

输出:

NaN

6

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
    double a,b,c,s,p;
    int i=0,n;
    cin>>n;
    while(i<n)
    {
     i++;
     cin>>a>>b>>c;
     if(a+b>c&&a+c>b&&b+c>a)
     {
         p=(a+b+c)/2;
         s=sqrt(p*(p-a)*(p-b)*(p-c));
         cout<<s<<endl;
     }
     else
        cout<<"NaN"<<endl;
     }
    return 0;
}

第三题:

判断ip地址是否合法,输入格式为a.b.c.d每个均为整数,且在[0.255]之间,合法输出YES,否则输出no;

例如,输入:

2

1.2.3.4

2222.3.4.5

输出:

yes

no

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<cstring>
using namespace std;
void  judge(int *a)
{   int k=1;
    for(int i=0;i<4;i++)
    {
       if(255<a[i]||a[i]<0)
       k=0;
    }
    if(k==1) cout<<"yes"<<endl;
    else if(k==0) cout<<"NO"<<endl;
}
int main()
{
    char s[100];
    int i=0,n;
    cin>>n;
    while(i<n)
    {
 
      cin>>s;
      int a[4]={0},j=0,num=0,l=strlen(s)-1;
      while(l>=0)
      {
         if(s[l]!='.'&&l>=0)
         {
             a[j]+=(s[l]-'0')*pow(10,num);
             num++;
         }
         if(s[l]=='.')
         {
             j++;
             num=0;
         }
          l--;
      }
      i++;
      int *p=a;
      judge(p);
 
 
    }
    return 0;
 
}

第四题:

从M开始找到第n个质数;

例如输入: 4  3

输出:5   7  11

#include<iostream>
#include<cstdio>
using namespace std;
int judge(int a)
{
    int k=1;
    for(int i=2;i<a;i++)
    {
        if(a%i==0)
            k=0;
    }
    return k;
}
int main()
{
    int a,b,sum=0,i=0,j=0;
    cin>>a>>b;
     for(i=a;j<b;i++)
     {
         if(judge(i)==1)
         {
             cout<<i<<" ";j++;
         }
     }
    return 0;
 
}

第五题:

一年中任意两天差值

例如,输入:

2019 1  1  2019 1 2

2016 1 1 2016  3 1

输出:

2

61

#include<iostream>
#include<cstdio>
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int day1[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int judge(int a,int b,int c)
{
    int sum=c;
    if(a%400==0||(a%4==0&&a%100!=0))
    {
        for(int i=1;i<b;i++)
        sum+=day1[i];
    }
    else
    {
        for(int i=1;i<b;i++)
        sum+=day[i];
    }
    return sum;
 
}
int main()
{
    int k=0,n;
    cin>>n;
    while(k<n)
    {
        int y,m,d,y1,m1,d1;
        cin>>y>>m>>d>>y1>>m1>>d1;
        cout<<judge(y1,m1,d1)-judge(y,m,d)+1<<endl;
        k++;
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值