Go ashore_16

1.函数int MySum(int m,int n);功能是求俩个正整数参数m到n之间(包括m,n)所有偶数和

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std; 
int MySum(int m,int n)
{
    int sum = 0;
    for(int i = m;i <= n;i ++ )
    {
        if(i%2==0)
            sum += i;
    }
    return sum;
}
int main()
{
    int m,n;
    cin>>m>>n;
    cout<<MySum(m,n);
    return 0;
}

2.求e的值,e≈1+1/1!+1/2!+1/3!+1/4!+...最后一项的值小于1e-6

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
double jiecheng(int n)
{
    int res = 1;
    for(int i = 1;i <= n;i ++ )
        res*=i;
    return res;
}
int main()
{
    double e;
    int n = 1;
    while((1/jiecheng(n))>=1e-6)
    {
        e+=1/jiecheng(n);
        n++;
    }
    e+=1/jiecheng(n);
    cout<<e;
    return 0;
}

3.打印n层杨辉三角

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
int main()
{
    int n;cin>>n;
    int a[20][20];
    for(int i = 0;i < n;i ++ )
        for(int j = 0;j <= i;j ++ )
        {
            if(j==i)
                a[i][j] = 1;
            else if(j==0)
                a[i][j] = 1;
            else
                a[i][j] = a[i-1][j]+a[i-1][j-1];
        }
    for(int i = 0;i < n;i ++ )
    {
        for(int j = 0;j <= i;j ++ )
            cout<<a[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}

4.对m*n矩阵处理,执行函数后使其每行元素大小按照升序排列

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;
int main()
{
    int m,n;cin>>m>>n;
    int a[m][n];
    for(int i = 0;i < m;i ++ )
        for(int j = 0;j < n;j ++ )
            cin>>a[i][j];
    int k = 0,t;
    while(k<m)
    {
        for(int i = 0;i < m;i ++ )
            for(int j = 0;j < n-1;j ++ )
                if(a[i][j]>a[i][j+1])
                {
                    t = a[i][j];
                    a[i][j] = a[i][j+1];
                    a[i][j+1] = t;
                }
        k++;
    }
    for(int i = 0;i < m;i ++ )
    {
        for(int j = 0;j < n;j ++ )
            cout<<a[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}

5.将正整数参数n,由低位到高位倒序输出(递归)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
void daoxv(long long n)
{
    cout<<n%10;
    if(n/10!=0)
    {
        n/=10;
        daoxv(n);
    }
}
int main()
{
    long long n;cin>>n;
    daoxv(n);
    return 0;
}

6.从键盘输入一个字符串,内有数字和非数字字符,统计字符串中有多少个整数,并依次输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
int main()
{
    string s;cin>>s;
    int num = 0;
    for(int i = 0;i < s.length();i++)
    {
        if((s[i]-'0')>=0&&(s[i]-'0')<=9)
            cout<<s[i];
        if(((s[i+1]-'0')<0||(s[i+1]-'0')>9)&&(s[i]-'0')>=0&&(s[i]-'0')<=9)
        {
            cout<<" ";
            num++;
        }
    }
    cout<<endl<<num;
    return 0;
}
updatedupdated2023-01-072023-01-07