站点图标 陌路寒暄

串的基本操作三(串)

串的基本操作三(串)

时间: 1ms        内存:64M

描述:

输入一行字符,分别归类出其中英文字母、空格、数字和其他字符,并分别按字母、空格、数字、其他字符输出。
要求:字母、数字字符要排序;空格的首尾用*标志;不删除重复字符。

输入:

输出:

示例输入:

12 sd w$%^. ,/';\'adfw

示例输出:

addfsww
*   *
12
$%^.,/';\'

提示:

参考答案(内存最优[752]):

#include <stdio.h>
#include <string.h>
#define MAXSIZE 100

char s[MAXSIZE];
char s1[MAXSIZE],s2[MAXSIZE],s4[MAXSIZE];
int s3;
void deal()
{
	int i,j=0,k=0,count=0;
	s3=0;
	for(i=0;i<=(signed)strlen(s);i++)
	{
		if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))
			s1[j++]=s[i];
		else
			if(s[i]>='0'&&s[i]<='9')
				s2[k++]=s[i];
			else
				if(s[i]==' ')
					s3++;
				else
					s4[count++]=s[i];
	}
	s1[j]='\0';
	s2[k]='\0';
	s4[count]='\0';
}

void quicksort(char a[],int s,int t)
{
	int i=s,j=t;
	char temp;
	if(i<j)
	{
		temp=a[s];
		while(i!=j)
		{
			while(i<j&&a[j]>temp)
				j--;
			if(i<j)
			{
				a[i]=a[j];
				i++;
			}
			while(i<j&&temp>a[i])
				i++;
			if(i<j)
			{
				a[j]=a[i];
				j--;
			}
		}
		a[i]=temp;
		quicksort(a,s,i-1);
		quicksort(a,i+1,t);
	}
}


int main()
{
	int i;
	gets(s);
	deal();
	quicksort(s1,0,strlen(s1)-1);
	quicksort(s2,0,strlen(s2)-1);
	if(strlen(s1))
		puts(s1);
	if(s3!=0)
	{
		printf("*");
		for(i=1;i<=s3;i++)
			printf(" ");
		printf("*\n");
	}
    if(strlen(s2))
		puts(s2);
	if(strlen(s4))
		puts(s4);
	return 0;
}

参考答案(时间最优[0]):

#include <stdio.h>
#include <string.h>
#define MAXSIZE 100

char s[MAXSIZE];
char s1[MAXSIZE],s2[MAXSIZE],s4[MAXSIZE];
int s3;
void deal()
{
	int i,j=0,k=0,count=0;
	s3=0;
	for(i=0;i<=(signed)strlen(s);i++)
	{
		if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))
			s1[j++]=s[i];
		else
			if(s[i]>='0'&&s[i]<='9')
				s2[k++]=s[i];
			else
				if(s[i]==' ')
					s3++;
				else
					s4[count++]=s[i];
	}
	s1[j]='\0';
	s2[k]='\0';
	s4[count]='\0';
}

void quicksort(char a[],int s,int t)
{
	int i=s,j=t;
	char temp;
	if(i<j)
	{
		temp=a[s];
		while(i!=j)
		{
			while(i<j&&a[j]>temp)
				j--;
			if(i<j)
			{
				a[i]=a[j];
				i++;
			}
			while(i<j&&temp>a[i])
				i++;
			if(i<j)
			{
				a[j]=a[i];
				j--;
			}
		}
		a[i]=temp;
		quicksort(a,s,i-1);
		quicksort(a,i+1,t);
	}
}


int main()
{
	int i;
	gets(s);
	deal();
	quicksort(s1,0,strlen(s1)-1);
	quicksort(s2,0,strlen(s2)-1);
	if(strlen(s1))
		puts(s1);
	if(s3!=0)
	{
		printf("*");
		for(i=1;i<=s3;i++)
			printf(" ");
		printf("*\n");
	}
    if(strlen(s2))
		puts(s2);
	if(strlen(s4))
		puts(s4);
	return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版