• 杭电_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++.
  • 相关阅读:
    Windows自动更新所需要连接的网站列表
    DFX 9.303 for QQMusic 2010
    在VPC 2007 SP1中安装Ubuntu 10.04 desktop (完成)
    穷人把钱存入银行,实际上是补贴富人。
    清理Windows右下角图标
    阿里镜像pull 加速器
    k8s 安装flannel网络插件
    k8s pull.sh
    kubeadm1.10.00 安装k8s集群
    虚拟机vmware centos7 扩展磁盘空间
  • 原文地址:https://www.cnblogs.com/chuanlong/p/2728893.html
Copyright © 2020-2023  润新知