zoukankan      html  css  js  c++  java
  • C++刷称号——2707: 素数与要素

    Description
    从键盘输入的随机整数n,如果n不是质数,然后计算n所有的因素(不含1)。例如,对于16,出口2,4,8;否则输出“It is a prime number.”
    推断素数和需求因素已完成功能。

    Input
    随机整数n

    Output
    该数的因子或素数标志。

    Sample Input
    16
    Sample Output
    2
    4
    8

    /* Copyright (c) 2014, 烟台大学计算机学院
     * All rights reserved.
     * 文件名:test.cpp
     * 作者:陈丹妮
     * 完毕日期:2015年 6 月 1 日
     * 版 本 号:v1.0
     */
    #include <iostream>
    #include <cmath>
    using namespace std;
    int is_prime(int n)
    {
        int r;
        if(n==1)
            return 0;
        else
        {
            for (r=2; r<=sqrt(n); ++r)
                if(n%r==0)
                    break;
            if(r>sqrt(n))
                return 1;
        }
    }
    void yinzi(int n)
    {
        int i;
        for(i=2;i<n;i++)
            if(n%i==0)
            cout<<i<<endl;
    }
    int main()
    {
        int flag,n;
        int is_prime(int);
        cin>>n;
        flag=is_prime(n);
        void yinzi(int n);
        if(flag==1)
            cout<<"It is a prime number."<<endl;
        else
            yinzi(n);
        return 0;
    }
    


    学习心得:认为自己这样练一些小的题目。是很实用的。曾经不清楚的函数知识。在做这些小题目的过程中,已经攻克了不少呢。努力付出一定的回报,只有坚持自己的,直到收获季节的到来。继续努力吧!!

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    0593. Valid Square (M)
    0832. Flipping an Image (E)
    1026. Maximum Difference Between Node and Ancestor (M)
    0563. Binary Tree Tilt (E)
    0445. Add Two Numbers II (M)
    1283. Find the Smallest Divisor Given a Threshold (M)
    C Primer Plus note9
    C Primer Plus note8
    C Primer Plus note7
    C Primer Plus note6
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4855570.html
Copyright © 2011-2022 走看看