YTU OJ 2774: Prepare for CET6

博客围绕帮助刘同学解决统计文章中不同单词总数的问题展开,给出了题目描述,包括输入为多行文章,以#结束,输出为不同单词总数,还提及有AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

Hard to force the CET4&6 is imminent, which makes most of the students a headache. This does not, Liu is trying his best to prepare for the CET6, trying to do a variety of previous year’s problem. But the most helpless for Liu is a quick read of this topic, so he thought of a thing does not make sense is that the statistical article the total number in different words. Here your task is to help Liu solve this problem.

输入

Multiple sets of data, each row is an article. Each article is consisted by lowercase letters and spaces, no punctuation, encounter # when the input end.

输出

Each group output only an integer, which alone make the trip, the integer representing the total number of different words in an article.

样例输入

you are my friend
can you can a can as a canner can can a can
#

样例输出

4
5

【AC代码】:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String s = sc.nextLine();
			if (s.equals("#"))
				break;
			String c[] = new String[100];
			int x = 0, y = 0, k = 0;
			for (int i = 0; i < s.length(); i++)
				if (s.charAt(i) == ' ') {
					y = i;
					c[k] = s.substring(x, y);
					x = y + 1;
					k++;
				}
				c[k] = s.substring(x, s.length());//将输入的一行字符串按空格分开
			int n = 0;//记录不同单词总个数
			int p[] = new int[k + 1];
			for (int i = 0; i <= k; i++) 
				for (int j = 0; j < i; j++) 
					if (c[j].equals(c[i])) 
						p[i] = 1;//若单词已出现再次出现则记为1
			for (int i = 0; i <= k; i++)
				if (p[i] != 1)//单词未重复出现过总个数加一
					n++;
			System.out.println(n);
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值