[백준] 1065 한수
#include <iostream>
#include<algorithm>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
int cnt = 0;
for (int x = 1; x <= n; ++x) {
vector<int> digit;
int tmp = x;
while (tmp != 0) {
digit.push_back(tmp % 10);
tmp /= 10;
}
if (digit.size() < 3) cnt++;
else {
bool flag = true;
int sub = digit[0] - digit[1];
for (int i = 0; i < digit.size() -1; ++i) {
if ((digit[i] - digit[i + 1]) != sub) {
flag = false;
break;
}
}
if (flag) cnt++;
}
}
cout << cnt;
return 0;
}
3년 전 코드
#include <stdio.h>
#pragma warning (disable:4996)
int main() {
int n;
scanf("%d", &n);
int cnt;
int digit[3];
if (n < 100) cnt = n;
else{
cnt = 99;
for (int i = 100; i <= n; i++) {
if (i == 1000)break;
int k = i;
digit[0]= (k % 10);
k /= 10;
digit[1] = (k % 10);
k /= 10;
digit[2] = (k % 10);
if ((digit[0] - digit[1]) == (digit[1] - digit[2])) cnt++;
}
}
printf("%d", cnt);
return 0;
}