Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. #include "Header.h"
  2.  
  3.  
  4.  
  5. int Problems0() { //틀 느낌으로 만든거 이거 복사해서 타이밍 조금만 합시다
  6. int nIdx = 0;
  7. int ret = 0;
  8. cout << "P0 - test\nBlabla : ";
  9. //cin >> nIdx;
  10. cout << "\nAns0 : " << ret << endl;
  11. return ret;
  12. }
  13. int Problems1() { //3이나 5의 배수 합 더하기
  14. int nRange = 0;
  15. cout << "P1 - Sum of naturals divisible by 3 and 5\nEnter Range : ";
  16. cin >> nRange;
  17. while (!ExceptInput(nRange)) {
  18. cout << "wrong Range("<<nRange<<")\nEnter Range : " << endl;
  19. cin >> nRange;
  20. }
  21.  
  22.  
  23. int ret = 0;
  24. for (int i = 1; i < nRange +1; i++) {
  25. if (i % 3 == 0) {
  26. ret += i;
  27. }
  28. else if (i % 5 == 0) {
  29. ret += i;
  30. }
  31. }
  32. cout << "Range : " << nRange << "\nAns1 : " << ret << endl;
  33. return ret;
  34. }
  35. int Problems2() { //GCD
  36. int n1,n2;
  37. int ret = 0;
  38. cout << "P2 - Greatest common divisor\nInput num1 : ";
  39. cin >> n1;
  40. while (!ExceptInput(n1))
  41. {
  42. cout << "wrong Range(" << n1 << ")\nEnter num1 : " << endl;
  43. cin >> n1;
  44. }
  45. cout << "Input num2 : ";
  46. cin >> n2;
  47. while (!ExceptInput(n2))
  48. {
  49. cout << "wrong Range(" << n2 << ")\nEnter num2 : " << endl;
  50. cin >> n2;
  51. }
  52. cout << "\nAns2 : " << GCD(n1,n2) << endl;
  53. return GCD(n1, n2);
  54. }
  55. int Problems3() { // LCM
  56. int n1, n2;
  57. int ret = 0;
  58. cout << "P3 - Least common multiple\nInput num1 : ";
  59. cin >> n1;
  60. while (!ExceptInput(n1))
  61. {
  62. cout << "wrong Range(" << n1 << ")\nEnter num1 : " << endl;
  63. cin >> n1;
  64. }
  65. cout << "Input num2 : ";
  66. cin >> n2;
  67. while (!ExceptInput(n2))
  68. {
  69. cout << "wrong Range(" << n2 << ")\nEnter num2 : " << endl;
  70. cin >> n2;
  71. }
  72. cout << "\nAns3 : " << LCM(n1, n2) << endl;
  73. return LCM(n1, n2);
  74. }
  75. int Problems4() { //주어진 범위 내의 가장 큰 소수
  76. int nRange = 0;
  77. cout << "P4 - Largest prime smaller than givin number\nEnter Range : ";
  78. cin >> nRange;
  79. while (!ExceptInput(nRange)) {
  80. cout << "wrong Range(" << nRange << ")\nEnter Range : " << endl;
  81. cin >> nRange;
  82. }
  83. int ret = 0;
  84. for (int i = nRange; i >0; i--) {
  85. if (IsPrime(i)) {
  86. ret = i;
  87. break;
  88. }
  89. }
  90. cout << "Range : " << nRange << "\nAns4 : " << ret << endl;
  91. return ret;
  92. }
  93. vector<st2int> Problems5() { //쎾씨프라임페어
  94. int nRange = 0;
  95. cout << "P5 - Sexy prime pairs\nEnter Range : ";
  96. cin >> nRange;
  97. while (!ExceptInput(nRange)) {
  98. cout << "wrong Range(" << nRange << ")\nEnter Range : " << endl;
  99. cin >> nRange;
  100. }
  101. vector<st2int> ret;
  102. for (int i = 1; i <= nRange; i++) {
  103. if ((IsPrime(i)) && (IsPrime(i + 6))) {
  104. st2int data;
  105. data.a = i;
  106. data.b = i+6;
  107. ret.push_back(data);
  108. }
  109. }
  110. for (int i = 0; i < ret.size(); i++) {
  111. cout << "Idx : " << i + 1 << " a = " << ret[i].a << " b = " << ret[i].b << endl;
  112. }
  113. return ret;
  114. }
  115. vector<st2int> Problems6() { //과잉수와 과잉수의 합 출력
  116. int nRange = 0;
  117. cout << "P6 - Abundant numbers\nEnter Range : ";
  118. cin >> nRange;
  119. while (!ExceptInput(nRange)) {
  120. cout << "wrong Range(" << nRange << ")\nEnter Range : " << endl;
  121. cin >> nRange;
  122. }
  123. vector<st2int> ret;
  124. for (int i = 1; i < nRange; i++) {
  125. if ((i * 2) < (AddVectorINT(FindDivisor(i)))) {
  126. //a = 과잉수, b = 과잉수의 합
  127. st2int data;
  128. data.a = i;
  129. data.b = AddVectorINT(FindDivisor(i)) - i;
  130. cout << "Abundant num = " << data.a << " Sum of Abundant num = " << data.b << " Abundance = " << data.b - data.a << endl;
  131. ret.push_back(data);
  132. }
  133. }
  134. return ret;
  135. }
  136. vector<st2int> Problems7() { //100,000보다 작은 친화수 구하기
  137. int nIdx = 0;
  138. vector<st2int> ret;
  139. vector<int> vSumofDivisor;
  140. cout << "P7 - Amicable numbers\n";
  141. vSumofDivisor.push_back(0);
  142. for (int i = 1; i <= 100000; i++)
  143. {
  144. vSumofDivisor.push_back(AddVectorINT(FindDivisor(i))-(i));
  145. }
  146. for (int i = 2; i <= 100000; i++) {
  147. if (vSumofDivisor[i] < 100000) {
  148. if ((vSumofDivisor[i] != i) && (vSumofDivisor[vSumofDivisor[i]] != vSumofDivisor[i])) {
  149. if (i == vSumofDivisor[vSumofDivisor[i]]) {
  150. st2int data;
  151. data.a = i;
  152. data.b = vSumofDivisor[i];
  153. if (data.a < data.b) {
  154. ret.push_back(data);
  155. cout << "Num1 : " << i << "\nSum of Num1 : " << vSumofDivisor[i] << "\nNum2 : " << vSumofDivisor[i] << "\nSum of Num2 : " << vSumofDivisor[vSumofDivisor[i]] << endl << endl;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. return ret;
  162. }
  163.  
  164. vector<int> Problems8() { //
  165. vector<int> ret;
  166. int idx=0;
  167. cout << "P8 - Armstrong numbers\n";
  168. CString str;
  169. for (int i = 100; i < 1000; i++)
  170. {
  171. if (IsArmstrong(i)) {
  172. idx++;
  173. ret.push_back(i);
  174. cout << "Index : " << idx<<"\nNum = " <<i<< endl;
  175. }
  176. }
  177. return ret;
  178. }
  179. vector<int> Problems9() { //
  180. vector<int> ret;
  181. int iInputNum = 0;
  182. cout << "P9 - Prime factors of a number\nEnter Num : ";
  183. cin >> iInputNum;
  184. while (!ExceptInput(iInputNum)) {
  185. cout << "wrong Input(" << iInputNum << ")\nEnter Number : ";
  186. cin >> iInputNum;
  187. }
  188. vector<int> ans;
  189. ans = PrimeFactor(iInputNum);
  190. cout << "Prime factor : " << iInputNum<<" = ";
  191. for (int i = 0; i < ans.size(); i++) {
  192. cout << ans[i] << " ";
  193. }
  194. cout << endl;
  195. return ret;
  196. }
  197. vector<int> Problems10() { //
  198. vector<int> ret;
  199. vector<int> vOut;
  200. int input;
  201. cout << "P10 - Gray code\nEnter 5bit Num(ex. 11111, 10101) : ";
  202. cin >> input;
  203. if (!ExceptInput5bit(input, vOut)) {
  204. cout << "wrong Input(" << input << ")\nex.1111, 10101\nEnter Number : ";
  205. cin >> input;
  206. }
  207. ret = GrayCode(vOut);
  208. cout << "P10 Ans : ";
  209. for (int i = 0; i < ret.size(); i++) {
  210. cout << ret[i];
  211. }
  212. cout << endl;
  213. return ret;
  214. }
  215. vector<char> Problems11() { //
  216. vector<char> ret;
  217. int input;
  218. cout << "P11 - Converting numerical values to Roman\nEnter Num(under 1000) : ";
  219. cin >> input;
  220. if ((input>1001)||(input<0)) {
  221. cout << "wrong Input(" << input << ")\n1 to 1000\nEnter Number : ";
  222. cin >> input;
  223. }
  224. ret = ConvertInttoRoman(input);
  225. cout << "P11 Input : "<<input<<" -> Ans : ";
  226. for (int i = 0; i < ret.size(); i++) {
  227. cout << ret[i];
  228. }
  229. cout << endl;
  230. return ret;
  231. }
  232. st2int Problems12() { //우박수
  233. st2int ret;
  234. cout << "P12 - Largest Collatz sequence\n";
  235. map<int ,unsigned long long> data;
  236. int million = 1000000;
  237. for (int i = 0; i < million; i++) {
  238. unsigned long long value;
  239. value = CalCollatz(i+1);
  240. data.insert(make_pair(i+1,value));
  241. }
  242.  
  243. int max = 0;
  244. for (auto i = data.begin(); i != data.end(); i++) {
  245. if (i->second > max) {
  246. max = i->second;
  247. ret.a = i->first;
  248. ret.b = i->second;
  249. }
  250. }
  251. cout << "P12 Num : " << ret.a << " , Count : " << ret.b << endl;
  252. return ret;
  253. }
  254. void Problems13() { //
  255. cout << "P13 - Computing the value of Pi\n";
  256.  
  257. //for (int i = 0; i < 10; i++)
  258. //{
  259. // printf("Computing Pi < %2d > : %.6f\n", i + 1, ComputePi());
  260. // cout << "Sleep 1sec..." << endl;
  261. // Sleep(1000);
  262. //}
  263.  
  264. //int count = 0;
  265. //double EPS = 0.0000001;
  266. //while (true)
  267. //{
  268. // count++;
  269. // double ans = ComputePi();
  270. // if ((ans < 3.141592 + EPS) && (ans > 3.141592 - EPS)) {
  271. // printf("Count = %d , Ans = %.6f",count,ans);
  272. // break;
  273. // }
  274. //}
  275.  
  276. random_device rd;
  277. auto seed_data = array<int, mt19937::state_size>{};
  278. generate(begin(seed_data), end(seed_data), ref(rd));
  279. seed_seq seq(begin(seed_data), end(seed_data));
  280. auto eng = mt19937{ seq };
  281. auto dist = uniform_real_distribution<>(0, 1);
  282. double EPS = 0.0000001;
  283. for (auto j = 0; j < 10; j++) {
  284. double ans = compute_pi(eng, dist);
  285. if ((ans < 3.141592 + EPS) && (ans > 3.141592 - EPS)) {
  286. cout << ans << endl;
  287. }
  288. }
  289.  
  290. return;
  291. }
  292. BOOL Problems14() { //
  293. BOOL ret = FALSE;
  294. string str;
  295. cout << "P14 - Validating ISBNs\nEnter ISBN : ";
  296. str.clear();
  297. cin >> str;
  298. if (str.size()!=10) {
  299. cout << "wrong Input(" << str << ")\nEnter ISBN : ";
  300. cin >> str;
  301. }
  302.  
  303. ret = ValidateISBN10(str);
  304.  
  305. cout << "P14 ISBN : " << str;
  306. if (ret) cout << " , Ans = TRUE" << endl;
  307. else cout << " , Ans = FALSE" << endl;
  308. return ret;
  309. }
  310. void main()
  311. {
  312.  
  313. while (true)
  314. {
  315. int nPNum;
  316. cout << "Enter Problems Num : ";
  317. cin >> nPNum;
  318. switch (nPNum)
  319. {
  320. case 1:
  321. {
  322. Problems1();
  323. break;
  324. }
  325. case 2:
  326. {
  327. Problems2();
  328. break;
  329. }
  330. case 3:
  331. {
  332. Problems3();
  333. break;
  334. }
  335. case 4:
  336. {
  337. Problems4();
  338. break;
  339. }
  340. case 5:
  341. {
  342. Problems5();
  343. break;
  344. }
  345. case 6:
  346. {
  347. Problems6();
  348. break;
  349. }
  350. case 7:
  351. {
  352. Problems7();
  353. break;
  354. }
  355. case 8:
  356. {
  357. Problems8();
  358. break;
  359. }
  360. case 9:
  361. {
  362. Problems9();
  363. break;
  364. }
  365. case 10:
  366. {
  367. Problems10();
  368. break;
  369. }
  370. case 11:
  371. {
  372. Problems11();
  373. break;
  374. }
  375. case 12:
  376. {
  377. Problems12();
  378. break;
  379. }
  380. case 13:
  381. {
  382. Problems13();
  383. break;
  384. }
  385. case 14:
  386. {
  387. Problems14();
  388. break;
  389. }
  390. default:
  391. {
  392. return;
  393. }
  394. }
  395. }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement