백준 알고리즘 9단계 약수, 배수와 소수를 풀어볼 것이다
#include <iostream>
#include <list>
using namespace std;
int main()
{
bool check = false;
list<string> slist;
while (!check)
{
int a, b;
cin >> a >> b;
if (a == 0 && b == 0)
{
check = true;
break;
}
string s = "";
if (a % b == 0) s = "multiple";
else if (b % a == 0) s = "factor";
else s = "neither";
slist.push_back(s);
}
int size = slist.size();
for (int i = 0; i < size; i++)
{
cout << slist.front() << "\n";
slist.pop_front();
}
}
2 4
4 2
2 3
0 0
#include <iostream>
using namespace std;
int main()
{
int N, K, c =0;
cin >> N >> K;
for (int i = 1; i < N+1; i++)
{
if (N % i == 0)
c++;
if (c == K)
{
cout << i;
break;
}
}
if (c < K)
cout << 0;
}
66 6
22
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main()
{
bool check =false;
list<string> slist;
while (true)
{
int n;
cin >> n;
if (n == -1)
break;
list<int> nlist;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
nlist.push_back(i);
}
int nsize = nlist.size();
int nc = 0;
string s = to_string(n);
for (int i = 0; i < nsize; i++)
{
nc += nlist.front();
if(i ==0)
s += " = ";
else if (i <= nsize - 1)
s += " + ";
s+= to_string(nlist.front());
nlist.pop_front();
}
if (nc != n)
s = to_string(n) + " is NOT perfect.";
slist.push_back(s);
}
int ssize = slist.size();
for (int i = 0; i < ssize; i++)
{
cout << slist.front()<<"\n";
slist.pop_front();
}
}
6
12
-1
#include <iostream>
using namespace std;
int main()
{
int N, c = 0;
cin >> N;
int* narr = new int[N] {0,};
for (int i = 0; i < N; i++)
{
cin >> narr[i];
bool check = false;
for (int j = 2; j < narr[i]; j++)
{
if (narr[i] % j == 0)
{
check = true;
break;
}
}
if (narr[i]!= 1 && !check)
c++;
}
cout << c;
}
5
1 2 3 4 5
3
#include <iostream>
using namespace std;
int main()
{
int M, N;
cin >> M >> N;
int sum = 0;
int min = 0;
for (int i = M; i < N+1; i++)
{
if (i <= 1) continue;
bool check = false;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
check = true;
break;
}
}
if (!check)
{
if (min == 0)
min = i;
sum += i;
}
}
if (min == 0)
cout << -1;
else
cout << sum << "\n" << min;
}
1
10
17
2
#include <iostream>
#include <list>
using namespace std;
int main()
{
int N;
cin >> N;
if (N != 1)
{
for (int i = 2; i <= N;)
{
if (N % i == 0)
{
cout << i <<"\n";
N = N / i;
}
else
i++;
}
}
}
777
3
7
37
오문완!