杭电_ACM_找新朋友

Problem Description
新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大于1的公约数,否则都是新朋友,现在会长想知道究竟有几个新朋友?请你编程序帮会长计算出来。
 
Input
第一行是测试数据的组数CN(Case number,1<CN<10000),接着有CN行正整数N(1<n<32768),表示会员人数。
 
Output
对于每一个N,输出一行新朋友的人数,这样共有CN行输出。
 
Sample Input
2
25608
24027
 
Sample Output
7680
16016
 
View Code
#include <stdio.h>
#include <string>
#define MAXN 32768
int a[MAXN + 5];
int main()
{
    int i, j, k, CN, num, count;
    scanf("%d", &CN);
    while (CN--)
    {
        //initialize the array
        memset(a, 0, sizeof(a));
        scanf("%d", &num);
        //if the number have common divisor with the input, make the value = 1
        for (j = 2; j < num; j++)        
            if (num % j == 0)
                for (k = j; k < num; k += j)
                    a[k] = 1;
        count = 0;
        //numbers of zero is numbers of new friends, pay attention to k = 1
        for (k = 1; k < num; k++)
            if (a[k] == 0)
                count++;
        printf("%d\n", count);
    }
}
Pay attention
firstly, get your ideas into shape, then coding
secondly, the algorithm is different with the before algorithm, the algorithm must handle every number by input, then get the result. And the before algorithm handle the array, then according to the input to get the result.
thirdly, if you use the memset(), you must add the #include , and submit by C++.
原文地址:https://www.cnblogs.com/chuanlong/p/2728893.html