求两个基础的C/C++程序
因本人在学Python,还未涉及C/C++ ,临时抱佛脚,望各位大大伸出援助小手本帖最后由 人造人 于 2019-7-3 18:46 编辑
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
bool is_prime(size_t n)
{
if(n == 1)
return false;
size_t m = sqrt(n);
for(size_t i = 2; i <= m; ++i)
{
if(n % i == 0)
return false;
}
return true;
}
int main()
{
std::vector<size_t> prime;
for(size_t i = 100; i <= 200; ++i)
{
if(is_prime(i))
prime.push_back(i);
}
std::cout << std::accumulate(prime.begin(), prime.end(), 0) / prime.size() << std::endl;
return 0;
}
人造人 发表于 2019-7-3 18:44
感谢 #include <iostream>
class Box
{
public:
Box(float p, float q, float r): a(p), b(q), c(r), volume(0), area(0) {}
void getvolume() {this->volume = this->a * this->b * this->c;}
void getarea() {this->area = 2 * (this->a * this->b + this->b * this->c + this->c * this->a);}
void disp() {std::cout << "volume: " << this->volume << ", area: " << area << std::endl;}
private:
float a, b, c;
float volume, area;
};
int main()
{
Box box(4, 5, 6);
box.getvolume();
box.getarea();
box.disp();
return 0;
}
漂亮
页:
[1]