C语言题目3

IT小小龙 posted @ 2014年4月03日 16:03 in C with tags 排序 奇数 偶数 , 512 阅读
//用C编写程序:输入一系列整数,保存这些整数,将所有奇数放在所有偶数之前,并且奇数部分和偶数部分需要按从小到大顺序排列,最后输出。
#include "stdio.h"
#include "stdlib.h"
typedef struct node{
  int x;
  struct node *next;
}llnode;

llnode* getNumberList()//此函数建立临时链表,用来接收用户输入
{
  int number = 0;
  llnode *head = (llnode *)malloc(sizeof(llnode));
  llnode *p = head;
  p->x = 0;
  p->next = NULL;
  printf("请输入一列整数,数字之间用空格隔开,以输入#并回车结束:");
  while(scanf("%d", &number))
    {
      llnode *n = (llnode *)malloc(sizeof(llnode));
      n->x = number;
      n->next = NULL;
      p->next = n;
      p = n;
      head->x ++;
    }
  return head;
}

int* getNumberArray(llnode *head)//此函数用来处理用户输入,把用户输入从链表转移到动态数组,以便后面的操作
{
  llnode *p = head;
  llnode *q = head;
  int *numberarray;
  int i;
  numberarray = (int *)malloc(sizeof(int)*(head->x));
  p = head->next;
  while(p!=NULL)
    {
      numberarray[i] = p->x; 
      i++;
      p = p->next;
    }
  return numberarray;
}
  
void removeNumberList(llnode *head)//此函数用于删除临时链表,释放空间
{
  llnode *p = head;
  llnode *q = head;
  while(p!=NULL)
    {
      q = p->next;
      free(p);
      p = q;
    }
}

void divideNumberArray(int *numberarray, int n)//此函数用于把奇数放到偶数的前面
{
  int start = 0;
  int end = n-1;
  int swap = 0;
  for(start=0; start<end; start++)
    {
      if(numberarray[start]%2 == 0)
	{
	  for(; end>start; end--)
	    {
	      if(numberarray[end]%2 == 1)
		{
		  swap = numberarray[start];
		  numberarray[start] = numberarray[end];
		  numberarray[end] = swap;
		  break;
		}
	    }
	}
    }
}

int getCut(int* numberarray, int n)//此函数用于得到奇数和偶数的划分点
{
  int i = 0;
  for(i=0; i<n; i++)
    {
      if(numberarray[i]%2 == 0)
	{
	  return i;
	}
    }
}

void sortNumberArray(int* numberarray, int start, int end)//此函数用于对指定范围内的数据进行升序排序
{
  int i = 0;
  int j = 0;
  int min = 0;
  int swap = 0;
  for(i=start; i<=end; i++)
    {
      min = i;
      for(j=i+1; j<=end; j++)
	{
	  if(numberarray[j] < numberarray[min])
	    {
	      min = j;
	    }
	}
      swap = numberarray[min];
      numberarray[min] = numberarray[i];
      numberarray[i] = swap;
    }
}

int main()
{
  int i = 0;
  int nodecount = 0;
  int *numberarray;
  int cut = 0;
  llnode *head;
  head = getNumberList();//由于不确定用户输入数据的多少,所以要采用链表,这样就解决了不知道用户会输入多少个数字的情况
  nodecount = head->x; //记录一下用户输入了多少数据
  numberarray = getNumberArray(head);//把临时链表的数据放到动态数组以便后续的处理
  removeNumberList(head);//至此numberarray存储了用户输入的所有数据,我们可以把之前的链表删除了,要不然会浪费内存资源
  //至此我们可以专心处理数组numberarry了,nodecount是数组元素个数
  divideNumberArray(numberarray, nodecount);//把奇数放到偶数的前面
  cut = getCut(numberarray, nodecount);//得到奇数和偶数的划分点
  sortNumberArray(numberarray, 0, cut-1);//对奇数排序
  sortNumberArray(numberarray, cut, nodecount-1);//对偶数排序
  for(i=0; i<nodecount; i++)//输出结果
    {
      printf("%d ", numberarray[i]);
    }
  printf("\n");
  return 0;
}
Avatar_small
BSEB Model Paper Cla 说:
2022年9月15日 16:58

Bihar Board Model Paper 2023 Class 11 Pdf Download for First Year Intermediate Arts, Science & Commerce Stream Question Bank by Every SCERT & NCERT Hindi Medium, English Medium & Urdu Medium Student of Bihar Patna Board can download BSEB 11th Model Paper 2023 or BSEB Intermediate Model Set 2023 for Paper-1 & Paper-2 Exam at BSEB Model Paper Class 11 Bihar School Examination, Patna Board and various private school teaching staff have designed and suggested the Intermediate Education First Year of STD-11 or Higher Secondary Education 11th Standard Arts, Science and Commerce Stream study & learning material as Bihar Board 11th Class Model Paper 2023.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter