[15596번] 1. 정수 N개의 합
C++
#include <vector>
long long sum(std::vector<int> &a, int n) {
long long sum = 0;
for (auto x : a) {
sum += x;
}
return sum;
}
Python
def solve(a):
ans = 0
for x in a:
ans += x
return ans
[4673번] 2. 셀프 넘버
C++
#include <iostream>
#include <set>
#include <string>
int kaprekar(int x);
int main()
{
std::set<int> num_set, kaprekar_gen;
for (int i = 1; i <= 10000; ++i) {
num_set.insert(i);
}
for (int i : num_set) {
int k = kaprekar(i);
kaprekar_gen.insert(k);
}
for (int i : kaprekar_gen) {
num_set.erase(i);
}
for (int i : num_set) {
std::cout << i << "\n";
}
return 0;
}
int kaprekar(int num) {
int sum = num;
for (char x : std::to_string(num)) {
int int_x = x - '0';
sum += int_x;
}
return sum;
}
Python
def Kaprekar(x):
sum = x
for i in str(x):
sum += int(i)
return sum
num_range = set(range(1, 10001))
Kaprekar_gen = set()
for i in range(1, 10000):
num = Kaprekar(i)
Kaprekar_gen.add(num)
answer = sorted(num_range-Kaprekar_gen)
for i in answer:
print(i)
[1065번] 3. 한수
C++
#include <iostream>
int main()
{
int n, cnt = 0;
std::cin >> n;
int a, b, c;
for (auto i = 1; i <= n; ++i){
if (i < 100) {
cnt++;
}
else {
a = i / 100;
b = i %100 / 10;
c = i % 10;
if (b - a == c - b) {
cnt++;
}
}
}
std::cout << cnt;
return 0;
}
Python
x = int(input())
cnt = 0
for i in range(1, x+1):
if i < 100:
cnt +=1
else:
a = i // 100
b = i % 100 // 10
c = i % 10
if b-a == c - b:
cnt +=1
print(cnt)