摘自P172 -175 第五章练习

P172 5.44 蒙特卡洛模拟实验

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main() {
    srand(time(0));

    double x, y;
    int n = 0;

    for(int i = 0; i < 1000000; ++i) {
        // 正确生成[-1, 1]区间内的随机double数
        x = ((double)rand() / RAND_MAX) * 2.0 - 1.0;
        y = ((double)rand() / RAND_MAX) * 2.0 - 1.0;

        // 分别检查x和y是否满足条件
        if (-1.0 <= x && x <= 0.0) {
            n++;
        } else if (0.0 <= y && y <= 1.0 - x) { // 注意这里的条件是否符合你的实际需求
            n++;
        }
    }

    cout << n << endl;
    return 0;
}

5.45

//输出整数1-7内两个数字的所有可能组合,并输出总组合数
#include <iostream>

using namespace std;

int main()
{
    int n = 0;
    for(int i = 1;i <= 7;i++)
    {
        for(int j = i + 1;j <= 7;j++)
        {
            cout << i << " " << j << endl;
            n++;
        }
    }
    cout << "The total number of all combinations is " << n << endl;
    return 0;
}

5.47

//计算方差和标准差
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;


int main()
{
    double a,b,c,d,e,f,g,h,i,j;
    cout << "Enter ten numbers: ";
    cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j;
    cout << "The mean is " << setprecision(2) << fixed << (a+b+c+d+e+f+g+h+i+j)/10 << endl;
    double sum = a*a+b*b+c*c+d*d+e*e+f*f+g*g+h*h+i*i+j*j;
    double s=sqrt((sum-(a+b+c+d+e+f+g+h+i+j)*(a+b+c+d+e+f+g+h+i+j)/10.0)/9.0);
    cout << "The standard deviation is " << setprecision(5) << fixed << s  << endl;
    return 0;
}

5.48

//计算字符串中大写字母的个数
#include <iostream>

using namespace std;

int main()
{
    string s;
    cout << "Enter a string: ";
    getline(cin,s);
    int n=0;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]>='A'&&s[i]<='Z')
        {
            n++;
        }
    }
    cout << "The number of uppercase letters is " << n << endl;
    return 0;
}

5.49

//找出两个字符串的最长公共前缀
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1, s2;
    cout << "Enter s1: ";
    getline(cin, s1);
    cout << "Enter s2: ";
    getline(cin, s2);

    int s = min(s1.size(), s2.size()); // 应该是min,因为我们寻找公共前缀
    string s0 = ""; // 初始化为空字符串

    for(int i = 0; i < s; i++)
    {
        if(s1[i] == s2[i])
        {
            s0 += s1[i]; // 当字符相同时,将其添加到s0
        }
        else
        {
            break; // 一旦发现不匹配的字符,立即跳出循环
        }
    }
    if(s0.size()>0)
    cout << "The common prefix is: " << s0 << endl; // 直接输出s0,无需额外循环
    else
    cout << s1 << " and " << s2 << " have no common prefix" << endl;

    return 0;
}

5.50

//倒置字符串
#include <iostream>

using namespace std;

int main()
{
    string s;
    cout << "Enter a string: ";
    cin >> s;
    string rs="";
    for(int i=s.size()-1;i>=0;i--)
    {
        rs+=s[i];
    }
    cout << "The reversed string is " << rs << endl;
    return 0;
}

5.52

//输出奇数下标的字符
#include <iostream>

using namespace std;

int main()
{
    string s;
    cout << "Enter a string: ";
    getline(cin,s);
    for(int i=0;i<s.size();i++)
    {
        if(i%2==0)
        {
            continue;
        }
        else
            cout << s[i];
    }
    return 0;
}

5.53

//判断元音辅音个数
#include <iostream>

using namespace std;

int main()
{
    string s;
    cout << "Enter a string: ";
    getline(cin,s);
    int vn=0;
    int cn=0;
    for(int i=0; i<s.size(); i++)
    {

        if(s[i] >= 'a'&& s[i] <= 'z'||s[i] >= 'A' && s[i] <= 'Z')
        {
            if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
            {
                vn++;
            }
            else
                cn++;
        }
    }
    cout << "The number of vowels is " << vn << endl;
    cout << "The number of consonants is " << cn << endl;

    return 0;
}

5.57

//判断密码合法性(8个字符,只含字母和数字,数字至少有两个)
#include <iostream>

using namespace std;

int main()
{
    string s;
    cout << "Please input your password: ";
    cin >> s;
    int n1=0;
    int n2=0;
    if(s.size() < 8)
    {
        cout << "invalid password" << endl;
    }
    else
    {
        for(int i=0;i<s.size();i++)
        {
            if(s[i] >= '0'&&  s[i] <= '9')
            {
                n1++;
            }
            else if(s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z')
            {
                n2++;
            }
        }
        if(n1+n2!=s.size())
        {
            cout << "invalid password" << endl;
        }
        else
        {
            if(n1 < 2)
            {
                cout << "invalid password" << endl;
            }
            else
                cout << "valid password" << endl;
        }
    }
    return 0;
}

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值