PTA 找出最长的单词分数 10

找出长度最长的单词(不同长度的单词只出现一次)。

输入格式:

输入格式为单行形式,单词之间使用空格分割。

输出格式:

输出格式为长度最长的一个单词。

输入样例:

在这里给出一组输入。例如:

an not need happy suggest

输出样例:

在这里给出相应的输出。例如:

suggest

代码长度限制16 KB

时间限制400 ms

内存限制64 MB

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        ArrayList<String> list = new ArrayList<>();
        int max = 0, num = 0;
        String str = sc.nextLine();
        for(String s : str.split(" ")){
            list.add(s);
            if(s.length() > max){
                max = s.length();
                num = list.indexOf(s);
            }
        }
        System.out.println(list.get(num));
    }
}

### PTA Python 相关资料概述 #### 词频统计实现 对于给定的字符串,可以通过构建一个计数器来记录每个单词出现的次数。具体方法是先将整个字符串按照空格分割成多个单词,然后遍历这些单词并更新它们对应的频率。 ```python from collections import Counter def word_frequency(text): words = text.split() frequency = Counter(words) return dict(frequency) text = "life is short we need python we need python" result = word_frequency(text) print(result) # {'life': 1, 'is': 1, 'short': 1, 'we': 2, 'need': 2, 'python': 2}[^1] ``` #### 阶乘计算函数优化 阶乘是一个常见的数学运算,在编程中经常被用来测试算法效率或作为教学案例展示递归和迭代的不同方式。下面展示了如何通过循环结构高效地求解指定范围内奇数位置上的阶乘值: ```python def factorial(n): product = 1 for factor in range(2, n + 1): product *= factor return product n = int(input()) for index in range(1, n + 1, 2): print(f"{index}! =", factorial(index)) ``` 此代码片段会依次打印出从`1`至用户输入的最大数值之间的所有奇数位阶乘结果[^2]。 #### 解决猴子吃桃问题 该问题是关于逆向思维的应用实例之一。为了找出最初摘得多少颗桃子,可以从最后一天剩余的一个桃子出发向前推算每一天原来的数量。每次增加一倍后再额外加上一颗以补偿当天消耗的部分。 ```python days = int(input()) remaining_peaches = 1 for day in reversed(range(days - 1)): remaining_peaches = (remaining_peaches + 1) * 2 print(remaining_peaches) ``` 上述程序能够正确处理任意合理范围内的天数,并返回第一天所采摘的具体数目[^3]。 #### 多选题评分逻辑解析 当面对包含多项选择类型的测验时,合理的分数分配机制至关重要。这里介绍了一种基于权重的方法来进行自动打分:首先定义好每道题目可能获得最高得分以及哪些字母代表的是标准答案;接着读取每位考生提交的答案列表并与之对比得出最终成绩。 ```python students_count, questions_count = map(int, input().split()) question_data = [] for _ in range(questions_count): full_score, options_num, correct_options_num, *correct_answers = input().split() question_data.append({ 'full_score': float(full_score), 'options': set(correct_answers[:int(correct_options_num)]), }) student_scores = {} for student_id in range(students_count): score_sum = 0.0 answers = [set(answer.strip('()').split()[1:]) for answer in input().strip().split()] for q_index, chosen_opts in enumerate(answers): if chosen_opts == question_data[q_index]['options']: score_sum += question_data[q_index]['full_score'] student_scores[f'Student {student_id + 1}'] = round(score_sum, 1) for name, total_score in student_scores.items(): print(name, ':', total_score) ``` 这段脚本可以接收来自用户的考试数据(包括试题详情和个人作答),并通过比较两者之间的一致性来决定各人的总评等级[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不再言语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值