1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package Suss; //Given two integers, which can be positive and negative, //find the sum of all the numbers between including them too and return it. //If both numbers are equal return a or b. // //Note! a and b are not ordered! // //Example: //GetSum(1, 0) == 1 // 1 + 0 = 1 //GetSum(1, 2) == 3 // 1 + 2 = 3 //GetSum(0, 1) == 1 // 0 + 1 = 1 //GetSum(1, 1) == 1 // 1 Since both are same //GetSum(-1, 0) == -1 // -1 + 0 = -1 //GetSum(-1, 2) == 2 // -1 + 0 + 1 + 2 = 2 //Waiting for the Feedback! Thanks! public class CodeWar_2 { public static void main(String[] args) { int a = -50; int b = 0; int c = 0; int min=0; int max=0; if(a > b){ max = a; min = b; } else if(b>a){ max = b; min = a; } else{ if(a==0 & b==0) { System.out.println(0); // return a; } System.out.println(1); // return a; } while(true){ System.out.println(min); c=c+min; if(min==max) break; min++; } System.out.println("결과 : "+c); } } |
code2
2017. 7. 21. 16:11