ZeroJudge I025: 真因數和 (小 n)

題目敘述

每筆資料只有一個正整數N,N不會超過65535。要求輸出N所有因數 (不包含N自己) 的和。

例如:6的因數有1、2、3、6 --> 1+2+3 = 6。


範例輸入 #1

6

範例輸出 #1

6


範例輸入 #2

12

範例輸出 #2

16


解題思路

使用For迴圈從1跑到N-1 (因為不能包含N自己),因為數字不大所以可以直接一個一個判斷所有N的因數並將所有因數加在一起,最後輸出因數和即可。

解題程式碼如下 (僅供參考):

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    int ans = 0;
    for (int i = 1; i<N; i++)
    {
        if (N % i == 0) ans += i;
    }
    cout << ans << "\n";
}

留言

這個網誌中的熱門文章

ZeroJudge M933: 邏輯電路

ZeroJudge A148: You Cannot Pass?!

ZeroJudge A263: 日期差幾天