两道c++基础题,求解

求这两道题的做法,不是代写是真心不会了,感谢了
本人尝试了多种解法但是不是报错就是不输出东西,脑子一团乱麻完全绕不过来,估计代码发上来也是大改我就不发了

img

img

第一题就是随机数,rand()函数的使用。第二题需要找规律。
第一题运行结果:

img

第一题代码:

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//求反序数
int fun1(int n)
{
    int s = 0;
    while (n)
    {
        s = s * 10 + n % 10;
        n /= 10;
    }
    return s;
}
//判断n及其反序数是否是素数
bool fun2(int n)
{
    int i;
    if (n < 2)
        return false;
    for (i = 2; i < n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}


int main()
{
    int n, t1, t2;
    srand((unsigned int)time(NULL));
    cout << "请输入数据个数:";
    cin >> n; //输入n
    cout << "随机产生的" << n << "个数中的可逆素数有:" << endl;
    for (int i = 0; i < n; i++)
    {
        t1 = rand() % 491 + 10; //生成[10,500]的随机数
        t2 = fun1(t1);
        if (fun2(t1) && fun2(t2))
            cout << t1 << endl;
    }
    return 0;
}

第二题运行结果:

img

第二题代码:

#include <iostream>

using namespace std;

void putgraph(int n)
{
    int i, j;
    int total = 2 * n - 1;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j < i; j++)
            cout << " ";
        for (j = 1; j <= total - 2*(i - 1); j++)
            cout << i;
        cout << endl;
    }
}


int main()
{
    int n;
    char ch;
    while (1)
    {
        cout << "请输入图形的行数(1~9):";
        cin >> n;
        putgraph(n);
        cout << "是否继续输出图形?请输入y或n:";
        rewind(stdin);    //或者用    fflush(stdin);,高版本编译器中fflush(stdin)已经废弃
        cin >> ch;
        if (ch == 'y')
            cout << "************************************" << endl;
        else
            break;
    }
    
    return 0;
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632