diff --git a/src/JavaDoubao/XtoY.java b/src/JavaDoubao/XtoY.java new file mode 100644 index 0000000..63490db --- /dev/null +++ b/src/JavaDoubao/XtoY.java @@ -0,0 +1,43 @@ +package JavaDoubao; +import java.util.ArrayList; + +public class XtoY { + + + public static class Main { + public static int solution(int xPosition, int yPosition) { + // Please write your code here + int diff = Math.abs(yPosition -xPosition); + int gone = 1; + ArrayList steps = new ArrayList<>(); + steps.add(1); + int lastStep = 1; + + while (gone < diff/2) { + lastStep += 1; + gone += lastStep; + steps.add(lastStep); + } + + if (gone*2 == diff){ + + } + + + + for (Integer steps2 : steps) { + System.out.printf("%d,",steps2); + } + System.out.println("Result"); + + return steps.size(); + } + + public static void main(String[] args) { + // You can add more test cases here +// System.out.println(solution(12, 6)); + System.out.println(solution(34, 45)); + System.out.println(solution(50, 30)); + } + } +} diff --git a/src/Main.kt b/src/Main.kt index 61c027a..f4099cf 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,4 +1,4 @@ fun main() { - val t = Test977() + val t = Test12() t.test() } \ No newline at end of file diff --git a/src/Test12.kt b/src/Test12.kt new file mode 100644 index 0000000..963841d --- /dev/null +++ b/src/Test12.kt @@ -0,0 +1,49 @@ +class Test12 { + class Solution { + val table = mapOf( + Pair(1000,"M"), + Pair(900,"CM"), + Pair(500,"D"), + Pair(400,"CD"), + Pair(100,"C"), + Pair(90,"XC"), + Pair(50,"L"), + Pair(40,"XL"), + Pair(10,"X"), + Pair(9,"IX"), + Pair(5,"V"), + Pair(4,"IV"), + Pair(1,"I") + ) + val bases = arrayOf(1000,100,10,1) + fun intToRoman(num: Int): String { + var num = num + val result = StringBuilder() + bases.forEach { base -> + var b = num / base + if (b != 0) { + if (table.containsKey(b * base)) { + result.append(table[b * base]) + } else { + if (b > 5) { + result.append(table[5 * base]) + b -= 5 + } + for (i in 0..") + var newNum = 0 + + while (cur!= 0) { + val lastDigit = cur % 10 + newNum += lastDigit * lastDigit + cur /= 10 + } + + cur = newNum + if (newNum in appeared){ + return false + }else{ + appeared.add(newNum) + } + } + return true + } + } + + fun test(){ + println(Solution().isHappy(2)) + } +} \ No newline at end of file