【機械試験】2019華科機試験問題【純C実現】


2019年華科コンピュータ学院機試験問題
Pro 01,メイソン素数
#include 
#include 
#define MAXN 100+5
#define LL_MAX ((1LL<<63)-1)
#define ll long long

/**
1、    
    :
           。     ,    2^p-1     ,     p    ,    Mp 。        ,       。
     ,        ,              
*/
// wiki:https://zh.wikipedia.org/wiki/%E6%A2%85%E6%A3%AE%E7%B4%A0%E6%95%B0
///Miller-Rabbin       s   2^(-s)    
const int s=50;
ll pow_mod(ll a,ll b,ll mod) {
    ll ans=1;
    while(b) {
        if (b&1)
            ans=ans*a%mod;
        a = a*a%mod;
        b >>= 1;
    }
    return ans;
}
int MRT(ll n) {
    if(n == 2)
        return 1;///2      
    for(ll i=1; i <= s; i++) {
        ll tmp = rand()%(n-1)+1;
        if(pow_mod(tmp,n-1,n) != 1)
            return 0;///   
    }
    return 1;///  
}

///     
int is_prime(ll n) {
    if(n <= 3)
        return n>1;
    if(n%3 ==0 || n%2 == 0)
        return 0;
    for(int i=5; i*i<=n; i++)
        if(n % i == 0 || n % (i+2) == 0)
            return 0;
    return 1;
}

/**           ...    ..
ll S[10], res[MAXN]; ///   long long   
///    -     (              )
int lucas_lerhm_pest() {
    S[0]=4;
    int i, cnt=0;
    res[cnt++] = 3;///M2  ,        -     
    for(i=1; i<6; i++) {
        S[i] = S[i-1]*S[i-1] - 2;
    }
    ll x=2, j=2;
    while(x < LL_MAX && j<7) {
        x = (1<

int main() {
    ll n;
    while(~scanf("%lld", &n)) {
        ll x=2, j=2;
        while(x <= n) {
            x = (1LL<<j)-1;
            if(is_prime(x)) {
                printf("%d ", x);
            }
            j++;
        }
        puts("");
    }
    return 0;
}


Pro 02,ファイル処理(補足)

//         

//            abc.c  ,    abc.c           ,  // /*……*/       

//(1)  abc     ,         ,            。(10)
//(2)      //               。(10)
//(3)      /*……*/               。(10)

/**
	      https://github.com/RaoXuntian/HUST.CS/blob/master/2019%E5%8D%8E%E7%A7%91%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%80%83%E7%A0%94%E6%9C%BA%E8%AF%95%E9%A2%98%E7%9B%AE/abc.c
        /*……*/         //,                     ,              。
*/
#include 
#include 
#define MAXN 10000+5
#define LL_MAX ((1LL<<63)-1)
#define ll long long
#define mem(a, n) memset(a, n, sizeof(a))

char buf[MAXN];
int flag = 1, line = 1, flag2 = 1;
void judge(int i) {
    if (buf[i+1]=='/' && buf[i+2]=='/') { //     
        flag = 0;
    }
    if(buf[i+1]=='
'
&& flag == 0) { // flag = 1; } if(buf[i+1]=='/' && buf[i+2]=='*') { // flag = -1; // flag2 = -1; } if((buf[i-2]=='*' && buf[i-1]=='/') && (flag==-1)) { flag = 1; // flag2 = 1; } } /* void judge(int i) { if(buf[i+1] == '/' && buf[i+2] == '/') { // flag = 0; } if(buf[i+1]=='
' && flag == 0) { // if(flag2 == 0) flag = 2; else flag = 1; } if(buf[i+1] == '/' && buf[i+2] == '*') { // flag2 = 0; } if((buf[i-2]=='*'&&buf[i-1] == '/') && flag2 == 0 ) { flag2 = 1; } }*/
int main() { FILE *fp; if ((fp=fopen("abc.c","r")) == NULL) { perror(" "); exit(0); } for (int i = 0; i < MAXN; i++) { buf[i] = fgetc(fp); // stream ( ), if (buf[i]==EOF) { break; } } printf("%d ", line++); judge(-1); // for (int i = 0; buf[i] != EOF; i++) { // if (flag == 1 ) { printf("%c", buf[i]); } else if(flag!=1 && buf[i]=='
'
) { // printf("%c", buf[i]); } if(buf[i]=='
'
) { // printf("%d ", line++); } judge(i); } printf("
"
); return 0; }

Pro 03、文字列の操作
#include 
#include 
#define MAXN 100+5
#define LL_MAX ((1LL<<63)-1)
#define ll long long
/**
3、      
    :
               ,                ,   
     。   ,     aBX ,     cDZ,             
 ,       8      ,                   。
*/
void solve(char *str) {
    int i, biNum[8];
    for(i=0; str[i]; i++) {
        int num = str[i], cnt=8;
        while(num) {//      
            biNum[--cnt] = num%2;
            num /= 2;
        }
        while(cnt>0) {/// 0
            biNum[--cnt] = 0;
        }
        int j, times=0;
        for(j=0; j<8; j++) {
            if(biNum[j] == 1) times++;
        }
        if(times % 2 == 0) ///   
            biNum[0]=1;
        int ans=0;
        for(j=0; j<8; j++) {
            ans=ans*2+biNum[j];
            printf("%d", biNum[j]);
        }
        printf(" %d
"
,ans); } } int main() { char mes[MAXN]; while(~scanf("%s", mes)) { int i; for(i=0; mes[i]; i++) { if(mes[i]>='A'&&mes[i]<='Z') { mes[i] = (mes[i]-65+2)%26+65; } else if(mes[i]>='a' && mes[i]<='z') { mes[i] = (mes[i]-97+2)%26+97; } } puts(mes); solve(mes); } return 0; }

2019年華科軟院機試験問題
Pro 01,奇数キューブ
#include 
#include 
#define MAXN 1000+5
#define LL_MAX ((1LL<<63)-1)
#define ll long long
#define mem(a, n) memset(a, n, sizeof(a))

/**
 1.    ,    “   ”,           1 、 2 n      n × n  
  ,            ,   、     、       n     
    。       3 。
  3 × 3     :
8 1 6
3 5 7
4 9 2

            :
(1)   1          ;
(2)   2      n × n             ;           
       1 ,    1 (           5   4        );
(3)                    11,         ,         n(n(          ););    11     ,    , 22       ,            ,     11;;
(4))                  nn ,          ,         11,    ,    11。  。  22    33     ,      , 33         ;         ;
(5)                ,                          ,          nn  ,   ,               。        ,              。        ,44          11    22 , ,         ,           ,  44      33   ;   ;

    :
        (      
3     )
    :
     
    :
3
    :
8 1 6
3 5 7
4 9 2
2.
*/
int matrix[MAXN][MAXN];
void solve(int n) {
    mem(matrix, 0);
    matrix[0][n/2] = 1; //  (1)
    int preRow=0, preCol=n/2, nextRow, nextCol;
    int num=2; //         
    while(num<=n*n) {
        //   (2) (3) (4)
        nextRow = (preRow-1+n) % n;
        nextCol = (preCol+1+n) % n;
        if(matrix[nextRow][nextCol]!=0 || (preRow==0 && preCol==n-1)) {//(5)
            nextRow=preRow+1;
            nextCol=preCol;
        }
        matrix[nextRow][nextCol] = num++;
        preRow = nextRow, preCol = nextCol;
    }
    int i,j;
    for(i=0; i<n; i++) {
        for(j=0; j<n; j++) {
            printf("%5d ", matrix[i][j]);
        }
        puts("");
    }
    puts("");
}

int main() {
    int n;
    while(~scanf("%d", &n)) {
        solve(n);
    }
    return 0;
}


Pro 02,単語個数統計(','判断を含む)
#include 
#include 
#define MAXN 1000+5
#define LL_MAX ((1LL<<63)-1)
#define ll long long
#define mem(a, n) memset(a, n, sizeof(a))

/**
2.         ,     1000 ,         ,       
 ,         。
      ,           。
    :
        。           。(           ,  
    if   ,             ,  if         ,   
    (                              
 ))
    :
     
    :
To be or not to be This is a question
    :
10
*/
//      ,       
char str[MAXN];
int main() {
    freopen("dataIn.txt", "r", stdin);
    while(gets(str)!=NULL) {
        int num=0,i ;
        for(i=0; str[i]; ) {
            if(str[i]==' '){
                i++;
                continue;
            }
            if(str[i]==',') {
                while(str[i++]==',') ;
                num++;
                continue;
            }
            while((str[i]>='A' && str[i]<='Z') || (str[i]>='a'&&str[i]<='z')) {
                i++;
            }
            num++;
        }
        printf("%d
"
, num); } return 0; }