Problem 42

问题描述:

The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.

Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?

 

解决问题:

 

这个没什么难度。

我们先找到对应关系。

使用数组就够了。

 

 

public class Problem42 {
	
	public static int Len = 0;

	public static String[] readNames(){
		List<String> names = new ArrayList<String>();
		
		StringBuffer text = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new FileReader("f:/words.txt"));
			String line ;
			while((line=br.readLine())!=null){
				text.append(line);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String regex = "\"(.*?)\"";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(text.toString());
		
		System.out.println("Number:"+matcher.groupCount());
		
		long total = 0;
		int index = 0;
		while(matcher.find()){
			String name = matcher.group(1);
			names.add(name);
			if(name.length()>Len)
				Len = name.length();
		}
		
		System.out.println(names);
		
		String[] result =(String[]) names.toArray(new String[0]);    

		return result;
	}
	
	public static int count_word(String word){
		
		int total = 0;
		for(int i=0; i<word.length(); i++){
			total+=word.charAt(i)-'A'+1;
		}
		
		return total;
	}
	
	public static void main(String[] args){
		String[] words = readNames();
		int sum = 0; 
		int MAX = Len*26+1;
		boolean[] numbers = new boolean[MAX];
		
		Arrays.fill(numbers, false);
		int index =1;
		int triangle ;
		System.out.println("Len:"+numbers.length);
		do{
			triangle  = (index*(index+1))/2;
			System.out.println("triangle:"+triangle);
			numbers[triangle] = true;
			index++;
		}while(triangle <MAX);
		for(int i=0; i<words.length; i++){
			int value = count_word(words[i]);
			if(numbers[value]){
				sum++;
			}
		}
		
		System.out.println(sum);
	}
}

 

在Python开发过程中,遇到与“scanning”相关的错误通常是指代码解析阶段出现的语法问题,尤其是在处理字符串时容易触发类似 `SyntaxError: EOL while scanning string literal` 的报错。这类错误表明解释器在扫描字符串时遇到了意外的行尾(EOL),即字符串未被正确闭合。 ### 常见原因及解决方案 - **未闭合的引号** Python中的字符串必须由成对的单引号 `'` 或双引号 `"` 包裹。如果遗漏了闭合引号,会导致解释器无法识别字符串边界,从而报错。 **示例错误代码:** ```python message = "Hello, world # 缺少右引号 ``` **修正方式:** ```python message = "Hello, world" ``` - **跨行字符串未使用三重引号** 如果字符串内容跨越多行,必须使用三重引号(`'''` 或 `"""`)来定义;否则,解释器会认为每行结束处为字符串结尾,从而引发错误。 **示例错误代码:** ```python text = "This is a long string that spans multiple lines." # 缺少三重引号 ``` **修正方式:** ```python text = """This is a long string that spans multiple lines.""" ``` - **转义字符使用不当** 若字符串中包含特殊字符(如引号本身),需使用反斜杠 `\` 进行转义。若忘记转义或误用反斜杠,也可能导致字符串扫描失败。 **示例错误代码:** ```python quote = "He said, "Hello!"" # 内部引号未转义 ``` **修正方式:** ```python quote = "He said, \"Hello!\"" ``` - **字符串拼接格式错误** 在拼接多段字符串时,若引号使用不一致或拼接方式错误,也可能造成字符串扫描异常。 **示例错误代码:** ```python result = 'The value is ' + 42 # 数字未转换为字符串 ``` **修正方式:** ```python result = 'The value is ' + str(42) ``` ### 调试建议 1. 定位报错行:查看报错信息中提示的具体代码行,重点关注该行的字符串定义。 2. 检查引号匹配:确保所有字符串都被正确闭合,且左右引号类型一致。 3. 使用IDE辅助:现代编辑器(如 VS Code、PyCharm)能自动高亮字符串范围,有助于快速发现语法错误。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值