CodeWars
1.Breaking chocolate problem
- Difficulty:7
Description:
Your task is to split the chocolate bar of given dimension n x m into small squares. Each square is of size 1x1 and unbreakable. Implement a function that will return minimum number of breaks needed.
For example if you are given a chocolate bar of size 2 x 1 you can split it to single squares in just one break, but for size 3 x 1 you must do two breaks.
If input data is invalid you should return 0 (as in no breaks are needed if we do not have any chocolate to split). Input will always be a non-negative integer.
数学题,找出通项公式就行了 n*m-1,特判一下0
public class Chocolate{
public static int breakChocolate(int n, int m) {
if(m == 0 || n == 0)
return 0;
else
return n * m -1;
}
}
2.Multiples of 3 and 5
- Difficulty:6
Description:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once.
import java.util.ArrayList;
public class Solution {
public int solution(int number) {
ArrayList<Integer> list = new ArrayList<Integer>();
int sum = 0;
for (int i = 1; i < number; i++) {
if (i % 3 == 0 || i % 5 == 0) {
list.add(i);
}
}
for(Integer num:list){
sum += num;
}
return sum;
}
}
3.Vasya - Clerk
- Difficulty:6
Description:
The new “Avengers” movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollars bill. A “Avengers” ticket costs 25 dollars.
Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.
Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
Return YES, if Vasya can sell a ticket to each person and give the change. Otherwise return NO.
Examples:
// *** Java *** Line.Tickets(new int[] {25, 25, 50}) // => YES Line.Tickets(new int []{25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars
题目有个隐含意思,因为是按照排队的人来售票,也就是说我们不能对给定的队列进行排序,因此只能根据当前拥有的纸币类型来作出回报。
同时我们可以发现100块是无法拆散还给别人的,因此100块只会越来越多,而25块是最灵活的,它能组成50与100,这说明我们在找钱的时候,必须先将大面额的钱交出去,若无法满足了,再去找小的,这样才是最优的。
public class Line {
public static String Tickets(int[] peopleInLine)
{
int a = 0;
int b = 0;
for (int i = 0; i < peopleInLine.length; i++) {
if (peopleInLine[i] == 25){
a++;
}else if (peopleInLine[i] == 50 && a > 0){
b++;
a--;
}else if (peopleInLine[i] == 100 && a > 0 && b >0){
a--;
b--;
}else if (peopleInLine[i] == 100 && a >= 3){
a-=3;
}else {
return "NO";
}
}
return "YES";
}
}
上述代码a代表25元的数量,b代表50元的数量,可以看到我们是优先交出50的。
4.Get the Middle Character
- Difficulty:7
Description:
You are going to be given a word. Your job is to return the middle character of the word. If the word’s length is odd, return the middle character. If the word’s length is even, return the middle 2 characters.
Examples:
Kata.getMiddle("test") should return "es" Kata.getMiddle("testing") should return "t" Kata.getMiddle("middle") should return "dd" Kata.getMiddle("A") should return "A"
Input
A word (string) of length 0 < str < 1000
Output
The middle character(s) of the word represented as a string.
利用java的String.substring(int start,int end)
方法便可轻松解决
class Kata {
public static String getMiddle(String word) {
if (word.length() % 2 == 0){
return word.substring(word.length()/2 - 1 , word.length() /2 + 1);
}else {
return word.substring(word.length() / 2,word.length() / 2 +1);
}
}
}
5.Mumbling
- Difficulty:7
Description:
This time no story, no theory. The examples below show you how to write function accum:
Examples:
Accumul.accum("abcd"); // "A-Bb-Ccc-Dddd" Accumul.accum("RqaEzty"); // "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy" Accumul.accum("cwAt"); // "C-Ww-Aaa-Tttt"
The parameter of accum is a string which includes only letters from a..z and A..Z.
public class Accumul {
public static String accum(String s) {
char[] temp = s.toLowerCase().toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < temp.length; i++) {
sb.append((temp[i]+"").toUpperCase());
for (int j = 0; j < i; j++) {
sb.append(temp[i]);
}
if (i < temp.length-1){
sb.append("-");
}
}
return sb.toString();
}
}