鱼C论坛

 找回密码
 立即注册
查看: 1598|回复: 2

[已解决]C++种的range如何用?

[复制链接]
发表于 2019-9-10 04:01:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我们老师让我们输入一个最小的数字a,然后输入一个最大的数字b,然后分别判断在range[a,b]中有多少个素数,然后输出:
输出的格式如下:

There are xxxx primes in the range[a,b]
我有个问题就是我不知道这个range【】是怎么用,我用cout输出这个range电脑一直说我未定义,求求大神们帮帮我
#include <iostream>
using namespace std;
int main() {
    bool flag = true;
    int a, b, i, j, times = 0;
    cout << "please enter a positive integer as the minimum:" << '\n';
    cin >> a;
    cout << "please enter a positive integer as the maximum:" << '\n';
    cin >> b;
    for (i = a; i <= b; i++)        // read the numbers from a to b
    {
        for (j = 2; j <= i / 2; j++)
        {
            if (i % j == 0)            // judging each number if it is not a prime
            {
                flag = false;
            }
        }
        if (flag)
        {
            times += 1;             // if it is prime, counting
        } else {
            times = times;
            flag = true;     //return true so as to judge another number
        }
    }
    cout << "There are " << times << " primes in the " <<range[a,b]<<'\n';
return 0;
}
最佳答案
2019-9-10 08:56:46
本帖最后由 superbe 于 2019-9-10 08:58 编辑

#include <iostream>

using namespace std;

int main() {
        bool flag = true;
        int a, b, i, j, times = 0;
        cout << "please enter a positive integer as the minimum:" << '\n';
        cin >> a;
        cout << "please enter a positive integer as the maximum:" << '\n';
        cin >> b;
        for (i = a; i <= b; i++)        // read the numbers from a to b
        {
                for (j = 2; j <= i / 2; j++)
                {
                        if (i % j == 0)            // judging each number if it is not a prime
                        {
                                flag = false;
                                break;        //添加这行
                        }
                }
                if (flag)
                {
                        times += 1;             // if it is prime, counting
                } else {
                        //times = times;        //去掉这行
                        flag = true;     //return true so as to judge another number
                }
        }
        cout << "There are " << times << " primes in the " << "range[" << a << "," << b << "].\n";

        return 0;
}

range是“范围”的意思,range[a,b]表示"从a到b的范围",除了a,b原样输出就可以了。range加双引号就是按字符串输出,否则就被当作变量而前面确实没定义range这么个变量。
另外代码中有两处小修改(红色注释行),不改结果也没错,但是多做了无用功。

最下面一行就是我要输出的格式,但是我一打上range就提示未定义这个变量

最下面一行就是我要输出的格式,但是我一打上range就提示未定义这个变量
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-10 08:56:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 superbe 于 2019-9-10 08:58 编辑

#include <iostream>

using namespace std;

int main() {
        bool flag = true;
        int a, b, i, j, times = 0;
        cout << "please enter a positive integer as the minimum:" << '\n';
        cin >> a;
        cout << "please enter a positive integer as the maximum:" << '\n';
        cin >> b;
        for (i = a; i <= b; i++)        // read the numbers from a to b
        {
                for (j = 2; j <= i / 2; j++)
                {
                        if (i % j == 0)            // judging each number if it is not a prime
                        {
                                flag = false;
                                break;        //添加这行
                        }
                }
                if (flag)
                {
                        times += 1;             // if it is prime, counting
                } else {
                        //times = times;        //去掉这行
                        flag = true;     //return true so as to judge another number
                }
        }
        cout << "There are " << times << " primes in the " << "range[" << a << "," << b << "].\n";

        return 0;
}

range是“范围”的意思,range[a,b]表示"从a到b的范围",除了a,b原样输出就可以了。range加双引号就是按字符串输出,否则就被当作变量而前面确实没定义range这么个变量。
另外代码中有两处小修改(红色注释行),不改结果也没错,但是多做了无用功。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-10 09:40:32 | 显示全部楼层
superbe 发表于 2019-9-10 08:56
#include

using namespace std;

谢谢你啦!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-4 09:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表