某虎公司的笔试

来源:互联网 发布:linux磁盘空间扩容 编辑:程序博客网 时间:2024/06/09 16:59

笔试有两个编程题,第一个是输出每个数字的质因数,并用5*3的像素打印出来;第二个是判断输入数据的类别。第二个相对容易一些,第一个当时没写完,题目如下:

输入:

24

输出:

 -  | -| - -   -  |   | - * -|   | -   -

当时想到用队列处理,把质因数获取之后,输出形式没来得及做,回去之后又完善了下,代码如下:


#include <stdio.h>#include <stdlib.h>struct Result{    char data;    struct Result *next;};typedef struct Result * resPtr;void enqueue(resPtr *head, resPtr *tail, int value);void printQueue(resPtr head);void addqueue(resPtr *head, resPtr *tail, int n);void printNum(resPtr head);int main(int argc, char **argv){        int n,i;    char c;        resPtr head = NULL;    resPtr tail = NULL;        while(1){        scanf("%d",&n);        if(n < 1)            break;        for (i = 2; i *i <= n;)        {            if (n % i == 0)            {                addqueue(&head,&tail,i);                enqueue(&head,&tail,0);                n /= i;            }            else              i++;            }            addqueue(&head,&tail,n);            enqueue(&head,&tail,0);            enqueue(&head,&tail,0);                    }        printNum(head);        //printQueue(head);return 0;}void printNum(resPtr head){    resPtr now = head;        if(head == NULL)        printf("The Queue is empty!\n\n");    else{        while(head != NULL){            now = head;            while(1){                if(now->data == '*' && now->next->data == '*')                    break;                else                switch(now->data){                    case '1':                    case '4':   printf("   "); break;                    case '2':                    case '3':                    case '5':                    case '6':                    case '7':                    case '8':                    case '9':   printf(" - "); break;                    case '*':   printf(" "); break;                    }                now = now->next;                }            printf("\n");            now = head;            while(1){                if(now->data == '*' && now->next->data == '*')                    break;                else                switch(now->data){                    case '1':                    case '2':                    case '3':                    case '7':   printf("  |"); break;                    case '4':                    case '8':                    case '9':   printf("| |"); break;                    case '5':                    case '6':   printf("|  "); break;                    case '*':   printf(" "); break;                    }                now = now->next;                }            printf("\n");            now = head;            while(1){                if(now->data == '*' && now->next->data == '*')                    break;                else                switch(now->data){                    case '1':                    case '7':   printf("   "); break;                    case '2':                    case '3':                    case '4':                    case '5':                    case '6':                    case '8':                    case '9':   printf(" - "); break;                    case '*':   printf("*"); break;                    }                now = now->next;                }            printf("\n");            now = head;            while(1){                if(now->data == '*' && now->next->data == '*')                    break;                else                switch(now->data){                    case '1':                    case '3':                    case '4':                    case '5':                    case '7':                    case '9':   printf("  |"); break;                    case '2':   printf("|  "); break;                    case '6':                    case '8':   printf("| |"); break;                    case '*':   printf(" "); break;                    }                now = now->next;                }            printf("\n");            now = head;            while(1){                if(now->data == '*' && now->next->data == '*')                    break;                else                switch(now->data){                    case '1':                    case '4':                    case '7':   printf("   "); break;                    case '2':                    case '3':                    case '5':                    case '6':                    case '8':                    case '9':   printf(" - "); break;                    case '*':   printf(" "); break;                    }                now = now->next;                }            printf("\n");            //while * 5            now = now->next;            head = now->next;        }    }    }void addqueue(resPtr *head, resPtr *tail, int n){    if(n < 10){        enqueue(head,tail,n);        }    else{        if(n > 10)            addqueue(head,tail,n/10);            enqueue(head,tail,n % 10);    }}void enqueue(resPtr *head, resPtr *tail, int value){    resPtr newPtr;        newPtr = malloc(sizeof(struct Result));        if(newPtr != NULL){        if(value == 0)            newPtr->data = '*';        else            newPtr->data = '0'+value;                    newPtr->next = NULL;        if(*head == NULL)            *head = newPtr;        else            (*tail)->next = newPtr;        *tail = newPtr;    }    else        printf("No memory :( \n");}void printQueue(resPtr head){    if(head == NULL)        printf("The Queue is empty!\n\n");    else{        while(head != NULL){            printf("%4c",head->data);            head = head->next;        }    }}

按部就班的写,没有用什么巧妙的办法

0 0
原创粉丝点击