华为13年机试题

及格成绩 描述:10个学生考完期末考试评卷完成后,A老师须要划及格线,要求以下: (1)及格线是10的倍数;(2)保证至少有60%的学生及格;(3)若是全部的学生都高于60分,则及格线为60分; 运行时间限制:无限制 内存限制: 无限制 输入: 输入10个整数,取值0-100 输出: 输出及格线,10的倍数 样例输入: 61 51 49 30 20 10 70 80 90 99 样例输出: 50node

亮着电灯的盏数 一条长廊里一次装有n(1<= n<=65536)盏电灯,从头至尾编号一、二、三、...n-一、n。每盏电灯由一个拉线开关控制。开始,电灯所有关着。有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下,接着第二个学生把号码凡是2的倍数的电灯的开关拉一下,接着第三个学生把号码凡是3的倍数的电灯的开关拉一下,如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。注:电灯数和学生一致。 运行时间限制:无限制 内存限制: 无限制 输入: 电灯的数量 输出: 亮着的电灯数量 样例输入: 3 样例输出: 1code

地铁换乘 已知2条地铁线路,其中A为环线,B为东西向线路,线路都是双向的。通过的站点名分别以下,两条线交叉的换乘点用T一、T2表示。编写程序,任意输入两个站点名称,输出乘坐地铁最少须要通过的车站数量(含输入的起点和终点,换乘站点只计算一次) 地铁线A(环线)通过车站:A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18 地铁线B(直线)通过车站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15 运行时间限制: 无限制 内存限制: 无限制 输入: 输入两个不一样的站名 输出: 输入最少通过的站数,含输入的起点和终点,换乘站点只计算一次 样例输入: A1 A3 样例输出: 3 第一题: <!-- lang: cpp --> #include <stdio.h> #include <stdlib.h> int main() { int score[10]; int pass_score = 0; int tmp = 0; int i = 0,j = 0; int flag = 1; printf("please input the students' score(10)\n"); for(;i< 10; i++) { scanf("%d",&score[i]); if(score[i] < 60) flag = 0; } for(i = 0;i < 10;i++) { for(j = 0;j < 10 - i - 1;j++) { if(score[i] > score[i + j + 1]) { tmp = score[i + j + 1]; score[i + j + 1] = score[i]; score[i] = tmp; } } } printf("the score (desc):\n"); for(i = 0;i < 10;i++) { if(i == 5) printf("\n"); printf("%d\t",score[i]); } printf("\n"); if(flag == 0) pass_score = score[4]/10*10; else pass_score = 60; printf("the pass_score = %d\n",pass_score); return 0; } 第二题: #include <stdio.h> #include <stdlib.h> #include <math.h> int issqrt(int n); int main(){ int light_num = 0; int sum = 0; int i = 0; printf("please input the number of the lights: "); scanf("%d",&light_num); for(i = 1; i < light_num + 1; i++) if(issqrt(i)) sum++; printf("the number of the lights who are on at last is %d\n",sum); return 0; }内存

int issqrt(int n){
    int i = 0;
    for(i = 1; i < n + 1; i++)
	    if(i * i == n)
		    return 1;
    return 0;
}

第三题: #include <stdio.h> #include <stdlib.h> #include <string.h>ci

#define CIR_NUM 20
#define LIN_NUM 17
typedef struct station{
        char name[5];           //the name of the station
        struct station *next;    //the pointer that points to the next node
        struct station *pre;     //the pointer that points to the pre node
        int flag;               //the type of the subway
        int number;
}station;

void create_circle_list(station **clist_head);
void create_line_list(station **llist_head);
station *search_station_c(char name[5],station **clist_head);
station *search_station_l(char name[5],station **llist_head);
int ways_1(int num_1,int num_2);
int ways_2(int num_1,int num_2);

int main(){
    station *clist_hd;      //the pointer that point to the first node on the station subway
    station *llist_hd;          //the pointer that point to the first node on the station subway
    station *phead,*ptail;
    char query_name_first[5];
    char query_name_second[5];
    int yes_or_no = 1;
    int a,b,c,d;
    int result;

    phead = ptail = NULL;
    //create the circle_list;
    printf("now create the cirlce_list!\n");
    create_circle_list(&clist_hd);
    printf("circle_list is created successfully!\n");

    printf("now create the line_list!\n");
    create_line_list(&llist_hd);
    printf("line_list is created successfully!\n");
    while(yes_or_no == 1){
        printf("please input the names of the stations which you want to query!\n");
        scanf("%s%s",query_name_first,query_name_second);
        phead = (station *)malloc(sizeof(station));
        ptail = (station *)malloc(sizeof(station));

        if(search_station_c(query_name_first,&clist_hd) != NULL)
                phead = search_station_c(query_name_first,&clist_hd);
        if(search_station_l(query_name_first,&llist_hd) != NULL)
            phead = search_station_l(query_name_first,&llist_hd);
        if(search_station_c(query_name_second,&clist_hd))
            ptail = search_station_c(query_name_second,&clist_hd);
        if(search_station_l(query_name_second,&llist_hd))
            ptail = search_station_l(query_name_second,&llist_hd);

        if(phead->flag == 1 && ptail->flag == 1){
            result = ways_1(phead->number,ptail->number);
        }
        else if(phead->flag == 0 && ptail->flag == 0){
            result = ways_2(phead->number,ptail->number);
        }
        else if(phead->flag ==1 && ptail->flag == 0){
            a = ways_1(phead->number,10);
            b = ways_1(phead->number,15);
            c = ways_2(ptail->number,6);
            d = ways_2(ptail->number,12);
            result = (a + c) > (b + d)? b + d : a + c;
            result--;
        }
        else{
            a = ways_2(phead->number,10);
            b = ways_2(phead->number,15);
            c = ways_1(ptail->number,6);
            d = ways_1(ptail->number,12);
            result = (a + c) > (b + d)? b + d : a + c;
            result--;
        }
        printf("the number of the least stations between %s and %s is %d\n\n",\
            phead->name,ptail->name,result);
        printf("do you want to continue('1' for yes,'0' for no)!\n");
        scanf("%d",&yes_or_no);
        }
    return 0;
}

void create_circle_list(station **clist_head){
    station *tail = NULL;
    station *head = NULL;
    int count = 0;
    printf("please input the name of the station!(%d)\n",CIR_NUM);
    head = (station *)malloc(sizeof(station));
    scanf("%s",head->name);
    head->flag = 1;
    count++;
    head->number = count;
    tail = head;
    while(count < CIR_NUM){
        tail->next = (station *)malloc(sizeof(station));
        tail->next->pre = tail;
        tail = tail->next;
        scanf("%s",tail->name);
        tail->flag = 1;
        count++;
        tail->number = count;
    }
    tail->next = head;
    head->pre = tail;
    *clist_head = head;
}

void create_line_list(station **llist_head){
    station *head = NULL;
    station *tail = NULL;
    int count = 0;
    printf("please input the name of the station!(%d)\n",LIN_NUM);
    head = (station *)malloc(sizeof(station));
    scanf("%s",head->name);
    head->flag = 0;
    count++;
    head->number = count;
    tail = head;
    while(count < LIN_NUM){
        tail->next = (station *)malloc(sizeof(station));
        tail->next->pre = tail;
        tail = tail->next;
        scanf("%s",tail->name);
        tail->flag = 0;
        count++;
        tail->number = count;
    }
    tail->next = NULL;
    *llist_head = head;
}

station *search_station_c(char name[5],station **clist_head){
    station *ptemp;
    ptemp = NULL;
    ptemp = *clist_head;
    int count = 0;
    while(count < CIR_NUM){
        if(strcmp(ptemp->name,name) == 0){
            return ptemp;
        }
        ptemp = ptemp->next;
        count++;
    }
    return NULL;
}

station *search_station_l(char name[5],station **llist_head){
    station *ptemp;
    ptemp = NULL;
    ptemp = *llist_head;
    int count = 0;
    while(count < LIN_NUM){
        if(strcmp(ptemp->name,name) == 0){
            return ptemp;
        }
        ptemp = ptemp->next;
        count++;
    }
    return NULL;
}

int ways_1(int num_1,int num_2){
    int result;
    result = num_1 - num_2;
    if(result < 0)
        result = -result;
    result += 1;
    if(result > CIR_NUM / 2 + 1)
        result = CIR_NUM - result + 2;
    return result;
}

int ways_2(int num_1,int num_2){
    int result;
   result = num_1- num_2;
    if(result < 0)
        result =  -result;
    result += 1;
    return result;
}
相关文章
相关标签/搜索