zoj 1284 - 行者无疆 始于足下 - 行走,思考,在路上

zoj 1284

xiaohanyu posted @ Thu, 20 May 2010 10:25:49 +0800 in Algorithm with tags ZOJ c++ Algorithm , 2805 readers

简单题。题目大意就是判断一个数是否为Perfect number。可是我竟然三次才ac,原因是没有考虑到数字为1的时候。

求真因数和的时候可以考虑质数判断的算法,循环到[tex]\sqrt{n}[/tex]就行了。代码很长很菜:

#include <stdio.h>
#include <math.h>

int get_sum(int number)
{
    int i;
    int sum = 0;

    for (i = 1; i < sqrt(number); ++i)
    {
        if (number % i == 0)
        {
            sum += i + number / i;
        }
    }
    if ((int)sqrt(number) * (int)sqrt(number) == number)
    {
        sum += sqrt(number);
    }

    sum -= number;
    
    return sum;
}

int main(int argc, char *argv[])
{
    int number;

    printf ("PERFECTION OUTPUT\n");
    
    while (scanf("%d", &number) && number)
    {
        int sum = get_sum(number);
        
        if (sum < number)
        {
            printf ("%5.0d  DEFICIENT\n", number);
        }
        else if (sum == number)
        {
            printf ("%5.0d  PERFECT\n", number);
        }
        else
            printf ("%5.0d  ABUNDANT\n", number);
    }

    printf ("END OF OUTPUT\n");
    
    return 0;
}

Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee