我如何编写下面的代码。请阅读。需要帮助asap。 [英] How do I write the code below. Please read. Need help asap.

查看:74
本文介绍了我如何编写下面的代码。请阅读。需要帮助asap。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个完整的C ++程序,在向量中查找并存储严格小于1000的所有素数,并编译一个报告,在该报告中显示在以下范围内找到的素数数量0到250,250到500,500到750 ,以及750到1000.确保测试程序并验证程序的结果。你的程序必须使用下面给出的函数_is_prime(k)和C ++ vector class。







< pre lang =c ++> #include < < span class =code-leadattribute> iostream >
#include < cmath >

使用 namespace std;

// 函数is_prime(k)如果k为prime则返回true,否则返回false。 / span>
bool is_prime( int k);


int main()
{
return 0 ;
}

// 功能实现
< span class =code-comment> //
如果k为素数,则is_prime(k)返回true,否则返回false。
bool is_prime( int k)
{
if (k< 2 return ;
for int i = 2 ; i< = static_cast< int>(sqrt(k)); i ++)
if (k%i == 0
return false ;
return true ;
}





我的尝试:



试图玩骷髅,但这对我来说很复杂,因为我是初学者

解决方案

引用:

试图玩骷髅,但这对我来说很复杂,因为我是初学者



显示你做了什么,解释问题你有,你会得到帮助。

引用:

试图玩骷髅,但它很复杂我,因为我是初学者



学习的唯一方法是通过实验,让其他人为你编写代码不会帮助你学习。



我们不做你的HomeWork。

HomeWork不会测试你乞求别人做你的工作的技能,它会让你思考并帮助你的老师检查你对cou的理解你已经采取了rses,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请展示您的代码并解释这个确切的问题,我们可能会帮忙。


您已经拥有素性测试功能,这是您任务中最困难的部分。

在请求的范围上循环,只将素数插入向量中(见vector :: push_back [ ^ ])。最终计算每个子范围内的项目。


Write a complete C++ program that finds and stores all primes numbers strictly less than 1000 in a vector and compiles a report in which it displays the number of primes found in the following ranges 0 to 250, 250 to 500, 500 to 750, and 750 to 1000. Make sure that you test your program and validate the results of your program. Your program must use the function _is_prime(k) given below and C++ vector class.



#include <iostream>
#include <cmath>

using namespace std;

// function is_prime(k) returns true if k is prime and false otherwise.
bool is_prime(int k);


int main()
{
   return 0;
}

// function implementation
// is_prime(k) returns true if k is prime and false otherwise.
bool is_prime(int k)
{
    if (k < 2) return false;
    for (int i = 2; i <= static_cast<int> (sqrt(k)); i++)
        if (k % i == 0)
            return false;
    return true;
}



What I have tried:

tried to play around with the skeleton, but it's complicated for me because im a beginner

解决方案

Quote:

tried to play around with the skeleton, but it's complicated for me because im a beginner


Show what you have done, explain the problems you have and you will get help.

Quote:

tried to play around with the skeleton, but it's complicated for me because im a beginner


The only way to learn is by experiments, and having others writing code for you will not help you to learn.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.


You already have the primality test function, that is the most difficult part of your task.
Make a loop on the requested range, inserting only prime numbers into the vector ( see vector::push_back[^]). Eventually count the items falling in each of the sub-ranges.


这篇关于我如何编写下面的代码。请阅读。需要帮助asap。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆