1. 程式人生 > >2018【位元杯】程式設計大賽

2018【位元杯】程式設計大賽

2018【位元杯】程式設計大賽

1. D

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
int main(){
    int a=10;
    int x=a++;
    printf("a=%d x=%d\n",a,x);//a=11,x=10
    return 0;
}

2.C

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
int main(){
    char acX[]="abcdefg";
    char acY[]={'a','b','c','d','e','f','g'};
    int lenx=strlen(acX);
    int leny=strlen(acY);
    printf("lenx=%d,leny=%d\n",lenx,leny);
    return 0;
}

 

3.指標:C

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
int main(){
   unsigned long pulArray[]={6,7,8,9,10};
   unsigned long *pulptr;
   pulptr=pulArray;
   *(pulptr+3)+=3;
   printf("%d,%d\n",*pulptr,*(pulptr+3));//6,12
    return 0;
}

4.聯合體:A

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
int main(){
    union{
        short k;
        char i[2];
    }*s,a;
    s=&a;
    s->i[0]=0x39;
    s->i[1]=0x38;
    printf("%x\n",a.k);//3839
    return 0;
}

 

5.資料溢位:C

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
int main(){
    unsigned char a=200;
    unsigned char b=100;
    unsigned char c;
    c=a+b;
    printf("%d %d\n",a+b,c);//300,44
    //300-2^8=44
    return 0;
}

 6.巨集定義:B

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
#define SQUARE(x) x*x
#define SQUARE2(x)  (x)*(x)
int main(){
    //預處理:printf("%d\n",1+2*1+2);
   printf("%d\n",SQUARE(1+2));//5
    
    //預處理:printf("%d\n",(1+2)*(1+2));
   printf("%d\n",SQUARE2(1+2));//9
    return 0;
}

7.結構體:D

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
struct Test{
    int i;
    double d;
    char c;
};

int main(){
  struct Test T;
   printf("%d\n",sizeof(T));//24
    return 0;
}

 8.D

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
unsigned short *sum(unsigned char a,unsigned char b){
    unsigned short s=0;
    s=a+b;
    return &s;
}
int main(){
    unsigned short *p=NULL;
    unsigned char a=1,b=2;
    p=sum(a,b);
    printf("%u+%u=%u\n",a,b,*p);//1+2=3
    return 0;
}

填空題1 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;

int main(){
    char *pcColor="CSOFTX3000";
    char acColor[]="CSOFTX3000";
    printf("%d\n",strlen(pcColor));//10
    printf("%d\n",strlen(acColor));//10
    printf("%d\n",sizeof(pcColor));//4
    printf("%d\n",sizeof(acColor));//11,字串結束標誌'\0'
    return 0;
}

 

 

填空題2 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
unsigned short* pucCharArray[10][10];
typedef union unRec{
    unsigned long ullndex;
    unsigned short usLeve[7];
    unsigned char ucPos;
}REC_S;
int main(){
    REC_S stMax,*pstMax;
    printf("%d\n",sizeof(pucCharArray));//400
    printf("%d\n",sizeof(stMax));//16
    printf("%d\n",sizeof(pstMax));//4
    printf("%d\n",sizeof(*pstMax));//16
    return 0;
}

填空題3

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;

int main(){
    unsigned char puc[4];
    struct tagPIM{
        unsigned char ucPim1;
        unsigned char ucData0:1;
        unsigned char ucData1:2;
        unsigned char ucData2:3;
    }*pstPimData;
    pstPimData=(struct tagPIM*)puc;
    memset(puc,0,4);
    pstPimData->ucPim1=2;
    pstPimData->ucData0=3;
    pstPimData->ucData1=4;
    pstPimData->ucData2=5;
    printf("%02x %02x %02x %02x\n",puc[0],puc[1],puc[2],puc[3]);//02,29,00,00
    printf("%02d %02d %02d %02d\n",puc[0],puc[1],puc[2],puc[3]);
    return 0;
}

程式設計題1:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
int fun(int a[],int n)//0~n-1
{
    int cnt=1;
    for(int i=1;i<n;i++){
        if(a[i]!=a[i-1]){
            a[cnt++]=a[i];
        }
    }
    for(int i=0;i<cnt;i++)
        printf("%d\n",a[i]);
    return cnt;
}
int main(){
    int a[]={1, 1, 2, 2, 3, 4, 5, 6, 6};
    printf("%d\n",fun(a,sizeof(a)/sizeof(a[0])));
    return 0;
}

 程式設計題2:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
int fun(int k,int a[],int n,int b[],int m)//0~n-1,0~m-1
{
    int tot=0;
    int i=0,j=0;
    while(i<n&&j<m){
        if(a[i]<b[j]){
            tot++;
            if(tot==k)  return a[i];
            i++;
            //c[tot++]=a[i],i++;
        }
        else{
            tot++;
            if(tot==k)  return b[j];
            j++;
            //c[tot++]=b[j],j++;
        }
    }
    while(i<n){
        //c[tot++]=a[i],i++;
        tot++;
        if(tot==k)  return a[i];
        i++;
    }

    while(j<m){
        //c[tot++]=b[j],j++;
        tot++;
        if(tot==k)  return b[j];
        j++;
    }
}
int main(){
    int a[]={1, 3, 5, 7, 9, 11, 13 };
    int b[]={ 2, 4, 6, 8, 10};

    printf("%d\n",fun(5,a,7,b,5));
    return 0;
}

程式設計題3

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
class Date
{
public:
    Date(int year=1900,int month=1,int day=1):_year(year),_month(month),_day(day)
    {
        //檢查如果輸入引數是非法時間,初始化為1900-1-1
        if(CheckIsInvaildDate())
        {
            year=1900;
            month=1;
            day=1;
        }
    }

    Date(const Date& d):_year(d.year),_month(month),_day(day){}

    Date& operator =(const Date& d)
    {
        if(this!=d){
            this._year=d._year;
            this._month=d._month;
            this._day=d._day;
        }
        return *this;
    }
    //檢查時間是否有效
    bool CheckIsInvaildDate()
    {
        if(_year<1||(_month<1||_month>12)||(_day<1||_day>DateOfMonth(_year,_month)))
           return true;
        return false;
    }
    void Display()
    {
        cout<<_year<<"-"<<_month<<"-"<<_day<<endl<<endl;
    }

};