1. 程式人生 > >Lily上課時使用字母數字圖片教小朋友們學習英語單詞,每次都需要把這些圖片按照大小(ASCII碼值從小到大)排列收好。請大家給Lily幫忙,通過C語言解決。

Lily上課時使用字母數字圖片教小朋友們學習英語單詞,每次都需要把這些圖片按照大小(ASCII碼值從小到大)排列收好。請大家給Lily幫忙,通過C語言解決。

描述

Lily上課時使用字母數字圖片教小朋友們學習英語單詞,每次都需要把這些圖片按照大小(ASCII碼值從小到大)排列收好。請大家給Lily幫忙,通過C語言解決。

 


知識點 字串
執行時間限制 0M
記憶體限制 0
輸入

Lily使用的圖片包括"A"到"Z"、"a"到"z"、"0"到"9"。輸入字母或數字個數不超過1024。

 


輸出

Lily的所有圖片按照從小到大的順序輸出

 


樣例輸入 Ihave1nose2hands10fingers
樣例輸出 0112Iaadeeefghhinnnorsssv

解題思路:利用ASCII值,過於複雜,請參加程式碼2

       ASCII值  

0--9   48--57

A--Z       65--90

a--z        97--122



#include<iostream>
using namespace std;
int main()
{
	char str[1024];
	gets(str);
	int len=strlen(str);
	int temp[80],count=0;
	char outstr[1024];
	for(int i=0;i<80;i++)
	{
		temp[i]=0;
	}
	for(int i=0;i<len;i++)
	{
		temp[str[i]-'0']++;
	}
	for(int i=0;i<80;i++)
	{
		if(i>=0 && i<=9 && temp[i]!=0)
		{
			for(int j=0;j<temp[i];j++)
			{
				outstr[count++]=i+'0';
			}
		}
		else if(i>=17 && i<=42 && temp[i]!=0)
		{
			for(int j=0;j<temp[i];j++)
			{
				outstr[count++]=i+'0';
			}
		}
		else if(i>=49 && i<=74 && temp[i]!=0)
		{
			for(int j=0;j<temp[i];j++)
			{
				outstr[count++]=i+'0';
			}
		}
	}
	outstr[count]='\0';
	cout<<outstr;
	system("pause");
	return 0;
}

程式碼2:

字串陣列中的元素可以直接排序,這樣的話,直接對陣列元素進行排序就行了

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char a[1000];
	gets(a);
	int k=strlen(a);
	for(int i=0;i<k;i++)//選擇排序
	{
		for(int j=i+1;j<k;j++)//在i+1到k中,找比a[i]小的數
		{
			 if(a[i]>a[j])
			 {
				char temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			 }
		}
	}
	printf("%s",a);
 system("pause");
return 0;
}