实验11-1-1 英文单词排序

本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。

输入格式:

输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。

输出格式:

输出为排序后的结果,每个单词后面都额外输出一个空格。

输入样例:

blue
red
yellow
green
purple
#

输出样例:

red blue green yellow purple 

 

//单词长度从小到大
#include<stdio.h>
#include<string.h>
struct danci{
    char s[20];
    int lenth;
};
int main(){
    struct danci a[20],tmp;
    int count=0;
    for(int i=0;;i++){
        gets(a[i].s);
        if(strcmp(a[i].s,"#")==0) break;
        a[i].lenth=strlen(a[i].s);
        count++;
    }//得到字符数组,下面进行排序
    for(int i=0;i<count;i++){
        for(int j=i+1;j<count;j++){
            if(a[i].lenth>a[j].lenth){
                tmp=a[i];
                a[i]=a[j];
                a[j]=tmp;
            }
        }
    }
    //输出
    for(int i=0;i<count;i++)
        printf("%s ",a[i].s);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python中的collections模块的Counter类来实现:# 导入所需的模块 import collections# 读取文件 with open('实验1-数据1.txt') as f: text = f.read()# 统计文本中每个单词出现的次数 count = collections.Counter(text.split())# 输出出现次数最多的10个单词 for word, count in count.most_common(10): print(word, count) ### 回答2: 要统计一个英文文档中出现次数最多的10个单词,可以使用Python的统计功能和文本处理模块来完成。下面是一个简单的Python代码示例: ```python # 打开文件并读取文本 with open('实验1-数据1.txt', 'r') as file: data = file.read() # 将文本转换为小写字母,以便统一计数 data = data.lower() # 替换文本中的标点符号 punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' for punctuation in punctuations: data = data.replace(punctuation, ' ') # 根据空格分割文本为单词列表 words = data.split() # 创建一个空字典来存储每个单词的频次 word_counts = {} # 统计每个单词的出现频次 for word in words: if word in word_counts: word_counts[word] += 1 else: word_counts[word] = 1 # 排序并取出出现频次最多的10个单词 sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)[:10] # 输出结果 for word, count in sorted_word_counts: print(word, count) ``` 这段代码首先打开文件并读取文本,然后将文本转换为小写字母,并且替换文本中的标点符号。之后,根据空格分割文本为单词列表,并且创建一个空字典来存储每个单词的频次。接下来,遍历单词列表,统计每个单词的出现频次,并在字典中更新计数。最后,根据频次对字典进行排序,并取出出现频次最多的10个单词,并输出结果。 ### 回答3: 要统计一个英文文档中出现次数最多的10个单词,并输出单词和频次,可以使用Python编程语言来实现。以下是一个可能的实现步骤和代码示例: 1. 打开文档: ```python with open('实验1-数据1.txt', 'r') as file: content = file.read() ``` 2. 清理文本并将其拆分为单词列表: ```python import re words = re.findall(r'\w+', content.lower()) ``` 3. 统计单词出现的频次: ```python from collections import Counter word_counts = Counter(words) ``` 4. 找到出现频次最多的10个单词: ```python top_10_words = word_counts.most_common(10) ``` 5. 输出最常出现的10个单词和它们的频次: ```python for word, count in top_10_words: print(f"{word}: {count}") ``` 这样就可以统计文档中出现次数最多的10个单词,并输出它们和对应的频次。需要注意的是,代码示例中的文档名假设为“实验1-数据1.txt”,请根据实际情况修改文件名。此外,代码示例还使用了正则表达式模块(模块名为 re)和计数器模块(模块名为 collections.Counter),请确保已安装相关模块。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值