daramG 2022. 3. 27. 19:57

15596번 문제 : 정수 N개의 합

문제 :

 

소스코드 :

#include <iostream>
#include <vector>
using namespace std;

long long sum(vector<int> &a) {
    long long ans = 0;
    for(int i=0; i<a.size(); i++) {
        ans += a.at(i);
    }
    return ans;
}

 

 

 

4673번 문제 : 셀프 넘버

문제 :

 

 

소스코드 :

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int noSelf(int num) {
    vector<string> v;
    int sum = num;
    string s = to_string(num);
    string n;
    for(int i=0; i<s.length(); i++) {
        n = s[i];
        v.push_back(n);
    }
    for(int i=0; i<v.size(); i++) {
        sum += stoi(v.at(i));
    }
    return sum;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int number[10001] = {false};
    int nn;
    
    for(int i=1; i<=10000; i++) {
        nn = noSelf(i);
        if (nn <= 10000) number[nn] = true;
    }
    for(int i=1; i<=10000; i++) {
        if(number[i] == false) cout << i <<"\n";
    }
}

 

 

 

1065번 문제 : 한수

문제 :

 

소스코드 :

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int hansoo(int num) {
    vector<string> v;
    string s = to_string(num);
    string n;
    bool hs = false;
    for(int i=0; i<s.length(); i++) {
        n = s.at(i);
        v.push_back(n);
    }
    if( stoi(v.at(0)) - stoi(v.at(1)) == stoi(v.at(1)) - stoi(v.at(2)) ) {
        hs = true;
    }
    else {
        hs = false;
    }
    return hs;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int c;
    int count = 0;
    cin >> c;
    
    for(int i=1; i<=c; i++) {
        if (i <= 99) {
            count++;
        }
        else if(i > 99 && i < 1000) {
            if (hansoo(i) == true) {
                count++;
            }
        }
    }
    cout << count;
}

 

 

함수를 사용할 수 있는지 작성해보라는 간단한 문제들이였다.