| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1337 人关注过本帖
标题:老师布置的作业,不太懂这个loop guarded command怎么实现的,还有这个斐波 ...
只看楼主 加入收藏
regor
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-9-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
老师布置的作业,不太懂这个loop guarded command怎么实现的,还有这个斐波那契数怎么产生的?
Write a program/function C++ that will implement loop guarded command (LGC).

Your program/function will take 3 non-zero, positive integers, e.g., 3, 6, and 12.

The first two integers should have different values, e.g., 3 and 6, and will be used to create a list of Boolean expressions as follows:

the first Boolean expression will be defined as “x%3 == 0”, i.e., check if x%3 has 0 remainder.  
the second Boolean expression will be defined as “x%4 ==0”
the third Boolean expression will be defined as “x%5==0”
The last Boolean expression will be defined as “x%6==0”
The statement corresponding to each Boolean expression defined above will print out the Fibonacci numbers that are less than the integer used in the Boolean expression.  E.g., the statement for the second Boolean expression x%4==0 will print out “0 1 1 2 3”, for the fourth Boolean expression x%6==0 will print out “0 1 1 2 3 5”.

The third input integer to your program/function will be used as input value for variable x defined in the Boolean expression.  E.g., if the third input integer is 12, the first, second, and fourth Boolean expressions will have a true value (i.e., 12%3 = 12%4 = 12%6 = 0). The LGC will randomly, non-deterministically choose ONE of 3 corresponding statements to print out the Fibonacci numbers, as described above.

If the third input integer to your program function failed all Boolean expressions defined, your program should throw runtime errors and be terminated.
搜索更多相关主题的帖子: integer The and expression program 
2020-09-29 10:59
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:20 
既然指定了“C++”,为什么不去C++板块,而是跑到C板块来提问?

另外,题目晦涩难懂,不知道想干嘛
程序代码:
#include <iostream>
#include <random>
#include <exception>
using namespace std;

int main( void )
{
    // 输入三个正整数,前两个不得相等
    unsigned a, b, x;
    if( !(cin>>a>>b>>x) || a==0 || b==0 || x==0 || a==b )
    {
        cerr << "fuck\n";
        return 1;
    }

    // 先求符合条件的表达式的数目 count
    unsigned count = 0;
    for( unsigned i=a; i<=b; ++i )
        count += x%i==0;

    // 若数目为0,则按题目要求抛出 运行时错误
    if( count == 0 )
        throw std::runtime_error("shit");

    // 随机地、非确定性 地从符合条件的表达式挑选一个数 n
    random_device rd;
    unsigned index = uniform_int_distribution<unsigned>(0,count-1)(rd);
    unsigned n;
    for( n=a; ; ++n )
    {
        if( x%n == 0 )
        {
            if( index == 0 )
                break;
            --index;
        }
    }

    // 输出小于n的 Fibonacci 数列
    cout << '0';
    for( unsigned i=0, j=1; j<n; j=j+i,i=j-i )
        cout << ' ' << j;
    cout << endl;
}
2020-09-29 12:58
快速回复:老师布置的作业,不太懂这个loop guarded command怎么实现的,还有这个 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033551 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved