zoukankan      html  css  js  c++  java
  • 第十六周项目3-指针引出奇数因子

    编写函数 int fun(int x, int *pp)。其功能是,求出x的所有奇数因子,并按照从小到大的顺序放在pp指向的内存中,函数返回值为这些整数的个数。若x的值为30,数组中的数为1,3,5,15,函数返回4。

    /*
    * Copyright (c) 2014,烟台大学计算机学院
    * All right reserved.
    * 作者:邵帅
    * 文件:demo.cpp
    * 完成时间:2014年12月10日
    * 版本号:v1.0
    */
    #include<iostream>
    using namespace std;
    int fun(int x,int *pp);
    int main()
     {
         int a[50],x,n;
         cin>>x;
         n=fun(x,a);
         cout<<n<<endl;
         for(int i=0; i<n; i++)
             cout<<a[i]<<" ";
         cout<<endl;
         return 0;
     }
     int fun(int x,int *pp)
     {
         int i,m=0;
         for (i=1;i<x;i+=2)
         {
             if (x%i==0)
             {
                 pp[m]=i;m++;
             }
         }
         return m;
     }
    

    运行结果:



    再来看一下求奇数因子。

     int fun(int x,int *pp)
     {
         int i,m=0;
         for (i=1;i<x;i+=2)
         {
             if (x%i==0)
             {
                 pp[m]=i;m++;
             }
         }
         return m;
     }

    @ Mayuko

  • 相关阅读:
    hdu 2546 饭卡
    poj 2262 Goldbach's Conjecture
    poj 1287 Networking
    poj 2377 Bad Cowtractors
    poj 1789 Truck History
    poj 2349 Arctic Network
    poj 1258 Agri-Net
    acdream 20140730 D题
    hdu 1012 素数判定
    hdu 2098 分拆素数和
  • 原文地址:https://www.cnblogs.com/mayuko/p/4567593.html
Copyright © 2011-2022 走看看