exercism————TwelveDays

这篇博客详细介绍了如何解决exercism中的TwelveDays问题,提供了两种不同的解题思路。解法一侧重于寻找一般规律,虽然可读性相对较低,但在大规模问题中更具优势。解法二利用String的join方法和Arrays的copyOfRange方法,实现简洁高效。
摘要由CSDN通过智能技术生成

题目:

在这里插入图片描述

解法1:

package exercism;

public class TwelveDays {

	void songLyrics() {
		String[] s1 = {"first","second","third","forth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth"};
		String[] s2 = {"a Partridge in a Pear Tree. ","two Turtle Doves, ","three French Hens, ","four Calling Birds, ","five Gold Rings, ","six Geese-a-Laying, ","seven Swans-a-Swimming, ","eight Maids-a-Milking, "," nine Ladies Dancing, "," ten Lords-a-Leaping, "," eleven Pipers Piping, ","twelve Drummers Drumming, "};

		for (int i = 1; i <= s1.length; i++) {
			System.out.print("On the " + s1[i-1] + " day of Christmas my true love gave to me: ");
			for (int j = i; j >=1; j--) {
				System.out.print(j == 1 && i != 1? "and " + s2[j - 1] : s2[j - 1]);
			}
			System.out.println();
		}
	}
	
}

解法二

import java.util.Arrays;

class TwelveDays {
	public static final String[] verses = {
		"On the first day of Christmas my true love gave to me, a Partridge in a Pear Tree.\n",
		"On the second day of Christmas my true love gave to me, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the third day of Christmas my true love gave to me, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the fourth day of Christmas my true love gave to me, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the fifth day of Christmas my true love gave to me, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the sixth day of Christmas my true love gave to me, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the seventh day of Christmas my true love gave to me, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the eighth day of Christmas my true love gave to me, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the ninth day of Christmas my true love gave to me, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the tenth day of Christmas my true love gave to me, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the eleventh day of Christmas my true love gave to me, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
		"On the twelfth day of Christmas my true love gave to me, twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n"
	};

	String verse(int verseNumber) {
		return verses[verseNumber - 1];
	}

	String verses(int startVerse, int endVerse) {
		return String.join("\n", Arrays.copyOfRange(verses, startVerse - 1, endVerse));
	}

	String sing() {
		return String.join("\n", verses);
	}
}

总结

解法一
寻找一般规律,将规律表述出来显得更加简练,但可读性稍逊解法二,但如果是面对大宗规模,解法二可能就显得比较繁赘

解法二:
使用到了String类的join方法和Arrays类的copyofRange方法,显得更加简练

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值