Last active
February 12, 2021 14:12
-
-
Save reishoku/ea5975b9158656c0a5aefa0a66a0589f to your computer and use it in GitHub Desktop.
C++のstd::listを使って素因数分解
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
// 2021-02-12. std::listで素因数分解プログラムを書きたい衝動に襲われたので書いた。 | |
// g++ source.cpp -o prog -lm -std=gnu++14 | |
#include <iostream> | |
#include <list> | |
#include <cmath> | |
using namespace std; | |
int main(void){ | |
int number = 0; | |
cout << "input number (> 0): "; | |
cin >> number; | |
list<int> l; | |
int i=2; | |
while(true){ | |
if(number == 1){ | |
break; | |
} | |
if(number%i==0){ | |
l.push_back(i); | |
number = number / i; | |
}else{ | |
i++; | |
continue; | |
} | |
} | |
//} | |
for(auto j = l.begin(); j != l.end(); j++){ | |
cout << *j << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment