Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Header.h"
- int Problems0() { //틀 느낌으로 만든거 이거 복사해서 타이밍 조금만 합시다
- int nIdx = 0;
- int ret = 0;
- cout << "P0 - test\nBlabla : ";
- //cin >> nIdx;
- cout << "\nAns0 : " << ret << endl;
- return ret;
- }
- int Problems1() { //3이나 5의 배수 합 더하기
- int nRange = 0;
- cout << "P1 - Sum of naturals divisible by 3 and 5\nEnter Range : ";
- cin >> nRange;
- while (!ExceptInput(nRange)) {
- cout << "wrong Range("<<nRange<<")\nEnter Range : " << endl;
- cin >> nRange;
- }
- int ret = 0;
- for (int i = 1; i < nRange +1; i++) {
- if (i % 3 == 0) {
- ret += i;
- }
- else if (i % 5 == 0) {
- ret += i;
- }
- }
- cout << "Range : " << nRange << "\nAns1 : " << ret << endl;
- return ret;
- }
- int Problems2() { //GCD
- int n1,n2;
- int ret = 0;
- cout << "P2 - Greatest common divisor\nInput num1 : ";
- cin >> n1;
- while (!ExceptInput(n1))
- {
- cout << "wrong Range(" << n1 << ")\nEnter num1 : " << endl;
- cin >> n1;
- }
- cout << "Input num2 : ";
- cin >> n2;
- while (!ExceptInput(n2))
- {
- cout << "wrong Range(" << n2 << ")\nEnter num2 : " << endl;
- cin >> n2;
- }
- cout << "\nAns2 : " << GCD(n1,n2) << endl;
- return GCD(n1, n2);
- }
- int Problems3() { // LCM
- int n1, n2;
- int ret = 0;
- cout << "P3 - Least common multiple\nInput num1 : ";
- cin >> n1;
- while (!ExceptInput(n1))
- {
- cout << "wrong Range(" << n1 << ")\nEnter num1 : " << endl;
- cin >> n1;
- }
- cout << "Input num2 : ";
- cin >> n2;
- while (!ExceptInput(n2))
- {
- cout << "wrong Range(" << n2 << ")\nEnter num2 : " << endl;
- cin >> n2;
- }
- cout << "\nAns3 : " << LCM(n1, n2) << endl;
- return LCM(n1, n2);
- }
- int Problems4() { //주어진 범위 내의 가장 큰 소수
- int nRange = 0;
- cout << "P4 - Largest prime smaller than givin number\nEnter Range : ";
- cin >> nRange;
- while (!ExceptInput(nRange)) {
- cout << "wrong Range(" << nRange << ")\nEnter Range : " << endl;
- cin >> nRange;
- }
- int ret = 0;
- for (int i = nRange; i >0; i--) {
- if (IsPrime(i)) {
- ret = i;
- break;
- }
- }
- cout << "Range : " << nRange << "\nAns4 : " << ret << endl;
- return ret;
- }
- vector<st2int> Problems5() { //쎾씨프라임페어
- int nRange = 0;
- cout << "P5 - Sexy prime pairs\nEnter Range : ";
- cin >> nRange;
- while (!ExceptInput(nRange)) {
- cout << "wrong Range(" << nRange << ")\nEnter Range : " << endl;
- cin >> nRange;
- }
- vector<st2int> ret;
- for (int i = 1; i <= nRange; i++) {
- if ((IsPrime(i)) && (IsPrime(i + 6))) {
- st2int data;
- data.a = i;
- data.b = i+6;
- ret.push_back(data);
- }
- }
- for (int i = 0; i < ret.size(); i++) {
- cout << "Idx : " << i + 1 << " a = " << ret[i].a << " b = " << ret[i].b << endl;
- }
- return ret;
- }
- vector<st2int> Problems6() { //과잉수와 과잉수의 합 출력
- int nRange = 0;
- cout << "P6 - Abundant numbers\nEnter Range : ";
- cin >> nRange;
- while (!ExceptInput(nRange)) {
- cout << "wrong Range(" << nRange << ")\nEnter Range : " << endl;
- cin >> nRange;
- }
- vector<st2int> ret;
- for (int i = 1; i < nRange; i++) {
- if ((i * 2) < (AddVectorINT(FindDivisor(i)))) {
- //a = 과잉수, b = 과잉수의 합
- st2int data;
- data.a = i;
- data.b = AddVectorINT(FindDivisor(i)) - i;
- cout << "Abundant num = " << data.a << " Sum of Abundant num = " << data.b << " Abundance = " << data.b - data.a << endl;
- ret.push_back(data);
- }
- }
- return ret;
- }
- vector<st2int> Problems7() { //100,000보다 작은 친화수 구하기
- int nIdx = 0;
- vector<st2int> ret;
- vector<int> vSumofDivisor;
- cout << "P7 - Amicable numbers\n";
- vSumofDivisor.push_back(0);
- for (int i = 1; i <= 100000; i++)
- {
- vSumofDivisor.push_back(AddVectorINT(FindDivisor(i))-(i));
- }
- for (int i = 2; i <= 100000; i++) {
- if (vSumofDivisor[i] < 100000) {
- if ((vSumofDivisor[i] != i) && (vSumofDivisor[vSumofDivisor[i]] != vSumofDivisor[i])) {
- if (i == vSumofDivisor[vSumofDivisor[i]]) {
- st2int data;
- data.a = i;
- data.b = vSumofDivisor[i];
- if (data.a < data.b) {
- ret.push_back(data);
- cout << "Num1 : " << i << "\nSum of Num1 : " << vSumofDivisor[i] << "\nNum2 : " << vSumofDivisor[i] << "\nSum of Num2 : " << vSumofDivisor[vSumofDivisor[i]] << endl << endl;
- }
- }
- }
- }
- }
- return ret;
- }
- vector<int> Problems8() { //
- vector<int> ret;
- int idx=0;
- cout << "P8 - Armstrong numbers\n";
- CString str;
- for (int i = 100; i < 1000; i++)
- {
- if (IsArmstrong(i)) {
- idx++;
- ret.push_back(i);
- cout << "Index : " << idx<<"\nNum = " <<i<< endl;
- }
- }
- return ret;
- }
- vector<int> Problems9() { //
- vector<int> ret;
- int iInputNum = 0;
- cout << "P9 - Prime factors of a number\nEnter Num : ";
- cin >> iInputNum;
- while (!ExceptInput(iInputNum)) {
- cout << "wrong Input(" << iInputNum << ")\nEnter Number : ";
- cin >> iInputNum;
- }
- vector<int> ans;
- ans = PrimeFactor(iInputNum);
- cout << "Prime factor : " << iInputNum<<" = ";
- for (int i = 0; i < ans.size(); i++) {
- cout << ans[i] << " ";
- }
- cout << endl;
- return ret;
- }
- vector<int> Problems10() { //
- vector<int> ret;
- vector<int> vOut;
- int input;
- cout << "P10 - Gray code\nEnter 5bit Num(ex. 11111, 10101) : ";
- cin >> input;
- if (!ExceptInput5bit(input, vOut)) {
- cout << "wrong Input(" << input << ")\nex.1111, 10101\nEnter Number : ";
- cin >> input;
- }
- ret = GrayCode(vOut);
- cout << "P10 Ans : ";
- for (int i = 0; i < ret.size(); i++) {
- cout << ret[i];
- }
- cout << endl;
- return ret;
- }
- vector<char> Problems11() { //
- vector<char> ret;
- int input;
- cout << "P11 - Converting numerical values to Roman\nEnter Num(under 1000) : ";
- cin >> input;
- if ((input>1001)||(input<0)) {
- cout << "wrong Input(" << input << ")\n1 to 1000\nEnter Number : ";
- cin >> input;
- }
- ret = ConvertInttoRoman(input);
- cout << "P11 Input : "<<input<<" -> Ans : ";
- for (int i = 0; i < ret.size(); i++) {
- cout << ret[i];
- }
- cout << endl;
- return ret;
- }
- st2int Problems12() { //우박수
- st2int ret;
- cout << "P12 - Largest Collatz sequence\n";
- map<int ,unsigned long long> data;
- int million = 1000000;
- for (int i = 0; i < million; i++) {
- unsigned long long value;
- value = CalCollatz(i+1);
- data.insert(make_pair(i+1,value));
- }
- int max = 0;
- for (auto i = data.begin(); i != data.end(); i++) {
- if (i->second > max) {
- max = i->second;
- ret.a = i->first;
- ret.b = i->second;
- }
- }
- cout << "P12 Num : " << ret.a << " , Count : " << ret.b << endl;
- return ret;
- }
- void Problems13() { //
- cout << "P13 - Computing the value of Pi\n";
- //for (int i = 0; i < 10; i++)
- //{
- // printf("Computing Pi < %2d > : %.6f\n", i + 1, ComputePi());
- // cout << "Sleep 1sec..." << endl;
- // Sleep(1000);
- //}
- //int count = 0;
- //double EPS = 0.0000001;
- //while (true)
- //{
- // count++;
- // double ans = ComputePi();
- // if ((ans < 3.141592 + EPS) && (ans > 3.141592 - EPS)) {
- // printf("Count = %d , Ans = %.6f",count,ans);
- // break;
- // }
- //}
- random_device rd;
- auto seed_data = array<int, mt19937::state_size>{};
- generate(begin(seed_data), end(seed_data), ref(rd));
- seed_seq seq(begin(seed_data), end(seed_data));
- auto eng = mt19937{ seq };
- auto dist = uniform_real_distribution<>(0, 1);
- double EPS = 0.0000001;
- for (auto j = 0; j < 10; j++) {
- double ans = compute_pi(eng, dist);
- if ((ans < 3.141592 + EPS) && (ans > 3.141592 - EPS)) {
- cout << ans << endl;
- }
- }
- return;
- }
- BOOL Problems14() { //
- BOOL ret = FALSE;
- string str;
- cout << "P14 - Validating ISBNs\nEnter ISBN : ";
- str.clear();
- cin >> str;
- if (str.size()!=10) {
- cout << "wrong Input(" << str << ")\nEnter ISBN : ";
- cin >> str;
- }
- ret = ValidateISBN10(str);
- cout << "P14 ISBN : " << str;
- if (ret) cout << " , Ans = TRUE" << endl;
- else cout << " , Ans = FALSE" << endl;
- return ret;
- }
- void main()
- {
- while (true)
- {
- int nPNum;
- cout << "Enter Problems Num : ";
- cin >> nPNum;
- switch (nPNum)
- {
- case 1:
- {
- Problems1();
- break;
- }
- case 2:
- {
- Problems2();
- break;
- }
- case 3:
- {
- Problems3();
- break;
- }
- case 4:
- {
- Problems4();
- break;
- }
- case 5:
- {
- Problems5();
- break;
- }
- case 6:
- {
- Problems6();
- break;
- }
- case 7:
- {
- Problems7();
- break;
- }
- case 8:
- {
- Problems8();
- break;
- }
- case 9:
- {
- Problems9();
- break;
- }
- case 10:
- {
- Problems10();
- break;
- }
- case 11:
- {
- Problems11();
- break;
- }
- case 12:
- {
- Problems12();
- break;
- }
- case 13:
- {
- Problems13();
- break;
- }
- case 14:
- {
- Problems14();
- break;
- }
- default:
- {
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement