思维题一(C++ 题目 代码 注解)

文章主要介绍了四个编程题目,涉及素数判断、连续因子计算、分数约分以及字符串处理,展示了C++中的基本算法应用。
摘要由CSDN通过智能技术生成

 目录

 题目一:

 题目二:

题目三:

 题目四:

 题目一:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[100];//装因子
    int p = 0;//递增数组序号
    for(int i=2;i<sqrt(n)+1;i++)
    {
        if(n%i==0)
            a[p++]=i;
    }
    if(p==0)//是素数
        a[p++]=n;
    int sum=1;//乘积和
    int cnt=1;//因子个数
    int maxx=1;//记录最长连续因子数
    int yy=0;//记录最长连续因子序列的第一个数下标
    for(int i=0;i<p;i++)
    {
        sum = a[i];
        cnt=1;
        for(int j=i+1;j<p;j++)
        {
            if(a[j]==a[j-1]+1)//符合连续
            {
                sum=sum*a[j];
                if(n%sum==0)//相乘是否还是n的因子
                {
                    cnt++;
                    if(cnt>maxx)
                    {
                        maxx=cnt;
                        yy=i;
                    }
                }
                else
                {
                    break;//退出
                }
            }
            else
            {
                break;
            }
        }
    }
    cout<<maxx<<endl;
    for(int i=yy;i<yy+maxx;i++)
    {
        cout<<a[i];
        if(i!=yy+maxx-1)
            cout<<"*";
    }
    cout<<endl;
}

 题目二:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long int ll;
int main()
{
    int  n, a, b, sum_a=0, sum_b = 1, t;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        scanf("%d/%d", &a, &b);
        sum_a *= b;//sum_a 分子
        sum_a += a * sum_b;//sum_b 分母
        sum_b *= b;
        //cout << sum_a << " " << sum_b << endl;
        t = __gcd(sum_a, sum_b);//最大公约数
        //cout << t << endl;
        sum_a /= t;//约分
        sum_b /= t;
    }
    if (sum_a % sum_b == 0)
        cout << sum_a / sum_b << endl;
    else if (sum_a < sum_b)
        cout << sum_a << "/" << sum_b << endl;
    else
        cout << sum_a / sum_b << " " << sum_a % sum_b << "/" << sum_b << endl;
}

题目三:

#include<iostream>
#include<cstring>
using namespace std;
int a[1100];
int main()
{
    int n;
    cin>>n;
    memset(a,0,sizeof(a));
    while(n--)
    {
        int k;
        cin>>k;
        while(k--)
        {
            int x;
            cin>>x;
            a[x]+=1;
        }
    }
    int flag,maxflag=0;
    for(int i=1;i<=1000;i++)
    {
        if(maxflag<=a[i])
        {
           flag=i;
           maxflag=a[i];
        }
    }
    cout<<flag<<" "<<maxflag<<endl;
}

 题目四:

#include<iostream>
using namespace std;
int main()
{
    string s;
    string name2,name14;
    int cnt=0;
    while(cin>>s)
    {
        if(s==".")
            break;
        cnt++;
        if(cnt==2)
            name2=s;
        if(cnt==14)
            name14=s;
    }
    if(cnt<2)
        cout<<"Momo... No one is for you ..."<<endl;
    else if(cnt>=2&&cnt<14)
        cout<<name2<<" is the only one for you..."<<endl;
    else if(cnt>=14)
        cout<<name2<<" and "<<name14<<" are inviting you to dinner..."<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值