从键盘接收一个字符串。然后按照字符串顺序从小到大进行排序,并删除重复字符

fashionzhou 2005-12-05 07:47:42
各位帮忙!
...全文
2602 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeqihong 2006-03-17
  • 打赏
  • 举报
回复
tmp[s[i++] - 'a']++;这句俺不太清楚意思得~,楼上帮解释下。小弟数据结构学差了。
  • 打赏
  • 举报
回复

算法确实很好的说
  • 打赏
  • 举报
回复


TO 楼上的

这段代码是将输入的字符转换成ASC码存在数据tmp[]中

对ASC码排序
lei001 2006-03-17
  • 打赏
  • 举报
回复
简单的美
blh 2006-03-17
  • 打赏
  • 举报
回复
改一下:
:P
#include <stdio.h>

int main()
{
char s[256];
int i = 0, tmp[256] = {0};

printf("Enter the string :\n");
gets(s);

while(s[i] != '\0')
tmp[s[i++]] = 1;

puts("\nAfter sort and delete operation:");
for(i = 0; i < 256;i++)
if(tmp[i])
printf("%c", i);

return 0;
}
blh 2006-03-17
  • 打赏
  • 举报
回复
改一下:
:P
#include <stdio.h>

int main()
{
char s[256];
int i = 0, tmp[256] = {0};

printf("Enter the string :\n");
gets(s);

while(s[i] != '\0')
tmp[s[i++]] = 1;

puts("\nAfter sort and delete operation:");
for(i = 0; i < 256;i++)
if(tmp[i])
printf("%c", i);

return 0;
}
wumingchenchao 2006-03-17
  • 打赏
  • 举报
回复
这个题我刚做过。采用冒泡排序法排序后,“删除”重复的字符即可。
#include<stdio.h>
#include<string.h>
main()
{
char string[123],*p,*q,*r,temp;
printf("Please input the characters:\n");
gets(string);
for(p=string;*p;p++) /*排序*/
{
for(q=r=p;*q;q++)
if(*r>*q)
r=q;
if(r!=p) /*如果此句执行,说明*p不是最小的,上面的r
记录下最小的字符的地址*/
{ temp=*r;
*r=*p;
*p=temp;
}
}
for(p=string;*p;p++)/*覆盖掉重复的字符*/
{
for(q=p;*p==*q;q++);
strcpy(p+1,q);
}
printf("Result:%s\n",string);
getch();
}
我在TC2下调试过了,没问题。LZ,看在我就一个三角的份上,能不能多给我那么一点点分?呵呵。算是对一个菜鸟的鼓励。
yeqihong 2006-03-17
  • 打赏
  • 举报
回复
那后面++是什么呢?不好意思。
boyzone_hero 2006-03-16
  • 打赏
  • 举报
回复
jixingzhong(瞌睡虫:选择了远方,只顾风雨兼程!)
算法不错我也学到了!
jixingzhong 2006-03-16
  • 打赏
  • 举报
回复
注意上面输入字母都要小写的 ~
不要超过 9 个(多了的话,把数组加大就可以了)

如果有大写字母或者其他字符,
扩展下程序就可以了 ~

主要就是表达一下我的思想 ~
不过空间有点浪费 ...
jixingzhong 2006-03-16
  • 打赏
  • 举报
回复
假设只有 26个字母(都小写),
如果处理的字符串比我假设的字符集大,
就扩展开了就可以了 :

#include <stdio.h>

int main()
{
char s[10];
int i = 0, tmp[26] = {0};

printf("Enter the string :\n");
gets(s);

while(s[i] != '\0')
tmp[s[i++] - 'a']++;

puts("\nAfter sort and delete operation:");
for(i = 0; i < 26; i++)
if(tmp[i] > 0)
printf("%c", i+'a');

return 0;
}
bjstcm 2006-03-16
  • 打赏
  • 举报
回复
我刚学范型算法,看看这个可不可以。
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str;
cin>>str;
sort(str.begin(),str.end());
string::iterator iter;
iter=unique(str.begin(),str.end());
str.erase(iter,str.end());
cout<<str;
}
wumingchenchao 2006-03-16
  • 打赏
  • 举报
回复
mark
fashionzhou 2006-03-16
  • 打赏
  • 举报
回复
自己先顶起来!
fashionzhou 2005-12-05
  • 打赏
  • 举报
回复
难道没人顶吗?

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧