Last active
July 12, 2016 08:36
-
-
Save dstyle0210/118fac430c78c3220e5220197c3d8717 to your computer and use it in GitHub Desktop.
[코딩도장] 넥슨 입사문제 중에서
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 코딩도장 공부 | |
| // http://codingdojang.com/scode/365 | |
| var max = 5000; | |
| // 숫자들의 합 구하기. | |
| var total = 0; | |
| (function(){ | |
| for(i=0;i<max;i++){ | |
| total += i; | |
| }; | |
| })(); | |
| console.log("total : "+total); | |
| // 제네레이터의 합구하기 | |
| var dTotal = 0; | |
| var dArr = []; | |
| function generator(num){ | |
| var tal = 0; | |
| var cut = (num+"").split(""); | |
| for(idx in cut){ | |
| tal += Number(cut[idx]) | |
| }; | |
| tal = tal + num; | |
| return tal; | |
| }; | |
| for(i=1;i<max;i++){ | |
| var g = generator(i); | |
| if(max<g){break;} // MAX값 보다 제네레이터가 클 이유는 없다. | |
| if(dArr.indexOf(g)== -1){ // 숫자가 달라도, 제네레이터가 같을수 있기 때문에, 중복제거처리. | |
| dArr.push(g); | |
| dTotal += g; | |
| }; | |
| }; | |
| console.log("dTotal : "+dTotal); | |
| console.log("결과 : "+(total-dTotal)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment