《基本控制结构》-小组作业 (11~20)

⒒求100----200之间的所有能被3以及7整除的自然数的平方根 之和。

#include <iostream>
#include <cmath>

using namespace std;

#define FACTOR 3 * 7

int main (void)
{
    double sum;

    for (int i = 5; i < 10; i ++)
    {
        int res = i * FACTOR;

        if (res > 200)
        {
            break;
        }

        sum += sqrt (res);
    }

    cout << "The sum of number is: " << sum << endl;

    system ("pause");
    return 0;
}

⒓求级数a=2/1 + 3/2 + 5/3 +8/5 +13/8+21/13…的前30项之和。

#include <iostream>
#include <cstdio>

using namespace std;

int main (void)
{
    int Numerator = 2;
    int Denominator = 1;
    int temp = Numerator;
    double sum;

    for (int i = 0; i < 30; i ++)
    {
        if (i != 29)
        {
            printf ("%d/%d + ", Numerator, Denominator);
        }
        else
        {
            printf ("%d/%d \n", Numerator, Denominator);
        }

        sum += double (Numerator / Denominator);

        temp = Numerator;
        Numerator += Denominator;
        Denominator = temp;
    }

    cout << "The sum of numbers is: " << sum << endl;

    system ("pause");
    return 0;
}

 

⒔求数列41+n(n –1)最小非素数项的序号(n > 2)

#include <iostream>
#include <cstdio>

using namespace std;

int An (int n);
bool isPrime (int x);

int main (void)
{
    cout << "The series is An = 41 + n * (n - 1)" << endl;

    int n = 1;

    while (1)
    {
        if (isPrime (An (n)) == false)
        {
            cout << "The minimum number is: " << n << endl;

            break;
        }

        n ++;
    }

    printf ("While n = %d, the result of series is not prime! \n", n);

    system ("pause");
    return 0;
}

int
An (int n)
{
    return 41 + n * (n - 1);
}

bool
isPrime (int x)
{
    for (int i = 2; i * i <= x; i ++)
    {
        if (x % i == 0)
        {
            return false;
        }
    }

    return true;
}

 

 ⒕求满足以下条件的数: 该数大于100小于1000; 其个位数与十位数的平方和等其百位数的立方

#include <iostream>
#include <cstdio>

using namespace std;

int main (void)
{
    int a, b, c;

    for (a = 1; a < 10; a ++)
    {
        for (b = 0; b < 10; b ++)
        {
            for (c = 1; c < 10; c ++)
            {
                if (b * b + c * c == a * a * a)
                {
                    printf ("The number %d%d%d match condition! \n", a, b, c);
                }
            }
        }
    }

    system ("pause");
    return 0;
}

 ⒖求级数e=1+1/1! +1/2! +1/3!+…    要求:求n项(n由键盘输入)或最后一项小于10-6结束。

#include <iostream>
#include <cstdio>

using namespace std;

#define EPS 1.0e-6

int main (void)
{
    int factorial = 1;
    double sum = 1.0;

    cout << "e = 1 + ";

    for (int i = 2; 1.0 / factorial * i > EPS; i ++)
    {
        sum += 1.0 / factorial;
        printf ("1/%d! ", i - 1);

        if (1.0 / (factorial * (i + 1)) > EPS)
        {
            printf ("+ ");
        }

        factorial = i * factorial;
    }

    cout << "\nThe sum is: " << sum << endl;

    system ("pause");
    return 0;
}

⒗输入一个小于21亿的正整数,判断其是否为回文数,并输出 提示。所谓回文数是高低位相等、次高低位相等,依此类推。如: 1245421是回文,数3773是回文数,12345不是回文数。

#include <iostream>
#include <cstdio>

using namespace std;

bool isPalindrome (long int x);

int main (void)
{
    long int x;
    cin >> x;

    if (isPalindrome (x) == true)
    {
        cout << "The number is Palindrome!" << endl;
    }
    else
    {
        cout << "The number is not Palindrome!" << endl;
    }

    system ("pause");
    return 0;
}

 bool
 isPalindrome (long int x)
 {
    if (x < 0 || (x % 10 == 0 && x != 0))
    {
        return false;
    }

    int revertedNumber = 0;

    while (x > revertedNumber)
    {
        revertedNumber = revertedNumber * 10 + x % 10;
        x /= 10;
    }

    return x == revertedNumber || x == revertedNumber / 10;
}

 

⒘求10000内的最大五个素数之和。

#include <iostream>

using namespace std;

bool isPrime (int x);

#define MAX 5

int main (void)
{
    int index = 0;
    int sum = 0;

    for (int i = 10000; i >= 0; i --)
    {
        if (isPrime (i) == true)
        {
            sum += i;
            index ++;

            printf ("The %d prime is: %d \n" ,index, i);
        }

        if (index == MAX)
        {
            break;
        }
    }

    cout << "The sum is: " << sum << endl;

    system ("pause");
    return 0;
}

bool
isPrime (int x)
{
    for (int i = 2; i * i < x; i ++)
    {
        if (x % i == 0)
        {
            return false;
        }
    }

    return true;
}

 

  ⒙统计4位正整数中前两位数组成的数是素数,后两位组成的数 能被3整除的数的个数并输出满足条件的数。如1324,13是素数, 24能被3整除。两位数的首位不能是0,如1303不满足条件

#include <iostream>

using namespace std;

bool isPrime (int x);

int main (void)
{
    int index = 1;
    for (int i = 10; i < 100; i ++)
    {
        for (int j = 10; j < 100; j ++)
        {
            if (isPrime (i) == true && j % 3 == 0)
            {
                printf ("The %3d prime is: %d \n" , index, i * 100 + j);

                index ++;
            }
        }
    }

    system ("pause");
    return 0;
}

bool
isPrime (int x)
{
    for (int i = 2; i * i < x; i ++)
    {
        if (x % i == 0)
        {
            return false;
        }
    }

    return true;
}

⒚编程输出1~1000中所有个位数是7的素数,统计其个数,并 求满足该条件数的和,输出结果。

#include <iostream>

using namespace std;

bool isPrime (int x);

int main (void)
{
    int index = 1;
    int sum = 0;

    for (int i = 1; i < 10001; i ++)
    {
        if (i % 10 == 7 && isPrime (i) == true)
        {
            sum += i;

            printf ("The %3d number is : %4d \n", index ++, i);
        }
    }

    cout << "The sum is: " << sum << endl;
    system ("pause");
    return 0;
}

bool
isPrime (int x)
{
    for (int i = 2; i * i < x; i ++)
    {
        if (x % i == 0)
        {
            return false;
        }
    }

    return true;
}

 

 ⒛编写程序求

#include <iostream>
#include <iomanip>

using namespace std;

#define EPS 1.0e-6

int main (void)
{
    double sum = 1.0;
    cout << "y = 1 +";

    for (int n = 2; 1.0 / ((n - 2.0) * (n - 1.0)) > EPS; n ++)
    {
        printf ("1/%d*%d ", n, n - 1);

        if (1.0 / (n * (n - 1)) > EPS)
        {
            cout << "+ ";
        }

        sum += 1.0 / (n * (n - 1.0));
    }

    cout << "\nThe sum is: " << setprecision(6) << sum;

    system ("pause");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘的企鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值