补、C++第九次作业
2016-06-17 15:50
435 查看
//test3 3: 编写一个函数,统计出一维数组中大于等于所有元素平均值的元素个数并返回它。 #include<iostream.h> #include<fstream.h> int Count(double a[], int n) { /**********Program**********/ double sum=0,pro; for(int i=0;i<10;i++) { sum+=a[i]; } pro=sum/10; int count=0; for( i=0;i<10;i++) { if(!(a[i]>=pro)) continue; count++; } return count; /********** End **********/ } void main() { int c; double a[10] = {34,54,75,86,53,45,34,45,34,45}; c=Count(a,10); cout<<" 数组中大于等于所有元素平均值的元素个数为:" <<c; void wwjt(int); wwjt(c); } void wwjt(int o) { fstream myfile; myfile.open(" out.txt" ,ios::out); myfile<<o<<endl; myfile.close(); }
心得体会:利用for循环实现对数组的控制
//test4 找出1-200之间的完数及个数 n(完数是一个数的因子之和是这个数本身。例如6=1+2+3)
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
void writeinfile(int n);
void main()
{
int m,n,k,s,l;//m范围,n完数的个数,k所有因子之和
/**********Program**********/
n=0;
for(m=2;m<=200;m++)
{
k=0;
for(s=1;s<m;s++)
{
if(m%s==0)
k+=s;
}
if(!(k==m))
continue;
cout<<m<<endl;
n++;
}
cout<<"完数的个数为"<<n<<endl;
/********** End **********/
writeinfile(n);
}
void writeinfile(int n)
{
fstream myfile;
myfile.open(" f.txt" ,ios::out);
myfile<<n<<endl;
myfile.close();
}
心得体会:利用for循环嵌套寻找实数的因子
相关文章推荐
- C++函数类型
- 补、C++第八次作业
- 二进制数相加或两个数相加(C++)
- 标准C++中的string类的用法总结
- Additive Number
- 设计模式之建造者模式(C++实现)
- 合并两个已排序的链表
- 大数相加
- C++中map的基本操作和使用;
- 购买和销售股票最佳时机(C++)
- C++之TR1::function
- 一个C++类的注释:
- 算法导论第十六章-贪心算法-Cpp代码实现
- 树的操作(C++)
- C/C++二级指针的三种模型(一)
- C++STL各种实现版本介绍
- c语言的算术隐士转换
- 字符串的存储
- Bulb Switcher
- (转)用Eclipse进行C++开发时Bianry not found的问题解决