C语言题目8

IT小小龙 posted @ 2014年4月05日 22:23 in C with tags 排序 集合 消重 , 920 阅读
//有两个集合A={a,b,f,d,e} B={g,b,h,d,e,e} 合并为一个集合C, C中的元素是递增排列的,并且无重复元素。
//这道题也可以拼接两个集合到一个集合,然后排序消重。

#include"stdio.h"
int getmin(char k[], int n)
{
  int min = -1;
  int i;
  for(i=0; i<n; i++)
    {
      if(k[i] != '#')
	{
	  min = i;
	}
    }
  if(min != -1)
    {
      for(i=0; i<5; i++)
	{
	  if((k[i] == '#') || i == min)
	    {
	      continue;
	    }
	  if(k[i] < k[min])
	    {
	      min = i;
	    }
	  else if(k[i] == k[min])
	    {
	      k[i] = '#';
	    }
	}
    }
  return min;
}

int main()
{
  char a[5] = {'a','b','f','d','e'};
  char b[6] = {'g','b','h','d','e','e'};
  char c[12] = {};
  int i = 0;
  int j = 0;
  int mina;
  int minb;
  int k = 0;
  int flaga = 0;
  int flagb = 0;
  while(1)
    {
      mina = getmin(a,5);
      minb = getmin(b,6);
      if((mina != -1) && (minb != -1))
	{
	  if(a[mina] < b[minb])
	    {
	      c[k] = a[mina];
	      a[mina] = '#';
	      k = k+1;
	    }
	  else if(a[mina] == b[minb])
	    {
	      c[k] = a[mina];
	      a[mina] = '#';
	      b[minb] = '#';
	      k = k+1;
	    }
	  else
	    {
	      c[k] = b[minb];
	      b[minb] = '#';
	      k = k+1;
	    }
	}
      else if((mina == -1) && (minb != -1))
	{
	  c[k] = b[minb];
	  b[minb] = '#';
	  k = k+1;
	}
      else if((mina != -1) && (minb == -1))
	{
	  c[k] = a[mina];
	  a[mina] = '#';
	  k = k+1;
	}
      else
	{
	  c[k] = '#';
	  break;
	}
    }
  i = 0;
  while(i<12)
    {
      if(c[i] == '#')
	{
	  printf("\n");	 
	  break;
	}
      else
	{
	  printf("%c ", c[i]);
	  i = i+1;
	}
    }
}
Avatar_small
HDFC Life Premium Pa 说:
2022年8月07日 12:24

HDFC Life is one of the top insurance providers in India which helps many subscribers with their benefited plans, and customers get easy avenues that make their online payment and other options very easy, where HDFC was established in 2000 and it has been growing in service that it provides to customers. HDFC Life Premium Payment The insurance policy through HDFC Life does carry in every different stream which ensures customers get their desired policy for their purpose, and customers can get more information about the HDFC Life Insurance from the official website of HDFC and here we will let you know how to get the installments for the policy to be paid.


登录 *


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