一道华为笔试题

目录

题目:

代码:

执行结果:


题目:

明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作(同一个测试用例里可能会有多组数据(用于不同的调查),希望大家能正确处理)。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct List
{
        int data;
        struct List* next;
};

void MakeList(struct List* head,int array[],int n)
{
        int i = 1;
        struct List* new = NULL;
        n = n-1;
        while(n>0){
                new = (struct List*)malloc(sizeof(struct List));
                head->next = new;
                new->data = array[i];
                new->next = NULL;
                head = new;

                n--;
                i++;
        }
}

void printfList(struct List* head)
{
        while(head){
                printf("%d\n",head->data);
                head = head->next;
        }
}

void OnceQuChong(struct List* head)
{
        int temp = '0';
        temp = head->data;
        while(head->next){
                if(temp == head->next->data && head->next->next == NULL){
                        head->next = NULL;
                        break;
                }
JIXU:
                if(temp == head->next->data && head->next->next != NULL){
                        head->next = head->next->next;
                        if(temp == head->next->data && head->next->next != NULL){
                                goto JIXU;
                        }
                        if(temp == head->next->data && head->next->next == NULL){
                                head->next = NULL;
                                break;
                        }
                }
                head = head->next;
        }
}

void AllQuChong(struct List* head)
{
        while(head){
                OnceQuChong(head);
                head = head->next;
        }
}

int OncePaiXu(struct List* head)
{
        int flag = 0;
        int temp = '\0';
        while(head->next){
                if(head->data > head->next->data){
                        temp = head->data;
                        head->data = head->next->data;
                        head->next->data = temp;
                        flag = 1;
                }
                head = head->next;
        }
        return flag;

}

void AllPaiXu(struct List* head)
{
        int flag = 1;
        while(flag == 1){
                flag = OncePaiXu(head);
        }
}

int main()
{
        int i = 0;
        int n = 0;
        struct List* L1 = NULL;

        printf("please input your size:");
        scanf("%d",&n);

        int in_Data[1000] = {0};

        for(i=0;i<n;i++){
                in_Data[i] = rand()%1000+1;
        }

        L1 = (struct List*)malloc(sizeof(struct List));
        L1->next = NULL;
        L1->data = in_Data[0];

        MakeList(L1,in_Data,n);

        AllQuChong(L1);

        AllPaiXu(L1);

        printfList(L1);

        return 0;
}

执行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值