输入一个字符串(少于80个字符),去掉重复的字符后,按照字符的ASCII码值从大到小输出。
时间: 2023-05-22 07:03:03 浏览: 51
以下是代码实现:
```
string str;
cin >> str;
int freq[128] = {};
for (char c : str) {
freq[c]++;
}
for (int i = 127; i >= 0; i--) {
if (freq[i] != 0) {
cout << char(i);
}
}
```
这段代码会先读入一个字符串,然后利用 `freq` 数组记录每个字符出现的频次,最后输出没有重复的字符,按照 ASCII 码从大到小排序。
相关问题
C++ 在右侧编辑器补充代码,用户输入一个字符串(少于80个字符),去掉重复的字符后,按照字符的ASCII码值从大到小输出。
下面是代码实现:
```python
def remove_duplicate_chars(s):
seen = set()
result = []
for c in s:
if c not in seen:
seen.add(c)
result.append(c)
return ''.join(result)
def sort_chars_by_ascii(s):
return ''.join(sorted(s, reverse=True))
user_input = input("请输入一个字符串(少于80个字符):")
if len(user_input) > 80:
print("输入的字符串过长!")
else:
no_duplicates = remove_duplicate_chars(user_input)
sorted_chars = sort_chars_by_ascii(no_duplicates)
print(sorted_chars)
```
这段代码可以实现将用户输入的字符串去重并按照字符的 ASCII 码值从大到小输出。
本关任务:输入一个字符串(少于80个字符),去掉重复的字符后,按照字符的ASCII码值从大到小输出。,给出c语言代码
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[81];
int i, j, len;
// 输入字符串
printf("请输入一个字符串(少于80个字符):\n");
scanf("%s", str);
len = strlen(str);
// 去重
for(i=0; i<len; i++)
{
for(j=i+1; j<len; j++)
{
if(str[i] == str[j])
{
str[j] = '\0';
}
}
}
// 排序
char temp;
for(i=0; i<len; i++)
{
for(j=i+1; j<len; j++)
{
if(str[i] < str[j])
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
// 输出结果
printf("去重排序后的结果为:\n");
for(i=0; i<len; i++)
{
if(str[i] != '\0')
printf("%c ", str[i]);
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)