目录
题目:
代码:
执行结果:
题目:
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了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;
}
执行结果:
|