zoukankan      html  css  js  c++  java
  • 018 求完数(初识数组指针)

    #include<iostream>
    using namespace std;

    int main()
    {
      int shu;                                                       //变量数字
      cin>>shu; 
      int *zhen_yinzi=new int[shu];                       //不知道变量因子有多少个,所以用指针指向   
      int index=0;                                                //个数索引的辅助变量
      int sum=0;
      zhen_yinzi[0]=1;                                         //所有数字都有真因子1

      for(int i=2;i<shu;i++)                                  //循环查找,真因子记入数组
      {
        if(shu%i==0)   
        {
          index ++;
          zhen_yinzi[index]=i;
        }
      }

      for(int j=0;j<=index;j++)                             //真因子求和
      {
        sum+=zhen_yinzi[j];
      }
      delete[] zhen_yinzi;                                      //释放内存
      zhen_yinzi=NULL;

      if(sum==shu)                                              //判断
        cout<<shu<<"是完数"<<endl;
      else
        cout<<shu<<"不是完数"<<endl;
      return 0;
    }

     

    来自侯晓琴《c++程序设计经典300例》

  • 相关阅读:
    LeetCode【125. 验证回文串】
    LeetCode【122. 买卖股票的最佳时机 II】
    LeetCode【121. 买卖股票的最佳时机】
    LeetCode【119. 杨辉三角 II】
    LeetCode【118. 杨辉三角】
    LeetCode【112. 路径总和】
    PAT1024
    PAT1020
    PAT1018
    PAT1017
  • 原文地址:https://www.cnblogs.com/butta/p/6386827.html
Copyright © 2011-2022 走看看