Go ashore_18

1.输入俩个正整数a和b,输出a和b之间的全部素数(每5个换一行)

 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>
#include<cmath> 
using namespace std;
int main()
{
    int a,b,i,j,cnt = 0;
    cin>>a>>b;
    for(i = a;i < b;i ++ )
    {
        for(j = 2;j < sqrt(i);j ++ )
            if(i%j==0) break;
        if(j>sqrt(i))
        {
            cout<<i<<" ";
            cnt++;
            if(cnt==5)
            {
                cout<<endl;
                cnt = 0;
            }
        }
    }
    return 0;
}

2.输入一组正整数,判断是否为回文数,是则输出。输入-1时结束

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int main()
{
    long long m,n,s = 0;
    cin>>n;
    while(n!=-1)
    {
        m = n;
        while(m>0)
        {
            s = s*10+m%10;
            m/=10;
        }
        if(s==n)
            cout<<"Yes!"<<n<<endl;
        else
            cout<<"No"<<endl;
        cin>>n;
        s = 0;
    }
    return 0;
}

3.输入一个长度不大于100的字符串(有空格),输出串中包含的单词个数。若不存在单词则输出0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int main()
{
    bool isword = false;
    int sum = 0;
    string s;getline(cin,s);
    for(int i = 0;i < s.length();i ++ )
    {
        if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
            isword = true;
        else if(s[i]==' '&&isword)
        {
            isword = false;
            sum++;
        }
        else
            isword = false;
    }
    if(isword) sum++;
    cout<<sum;
    return 0;
}

4.输入整数n,再输入n*n方阵中的全部数据,计算每列元素最大值和并输出

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

5.输入一个9进制数字串,将其转化为19进制并输出。

 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
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    string nine;cin>>nine;
    int len = nine.length()-1,ten = 0;
    char nineteen[50];
    for(int i = len,k = 0;i >= 0;i--,k++)
    {
        int tmp = nine[i]-'0';
        ten = ten + tmp * pow(9,k);
    }
    int p = 0;
    while(ten!=0)
    {
        int tmp = ten%19;
        ten/=19;
        if(tmp<10)
            nineteen[p++] = tmp+'0';
        else
            nineteen[p++] = tmp+'a'-10;

    }
    for(int j = p-1;j>=0;j--)
        cout<<nineteen[j];
    return 0;
}

6.输入小李出生日期,计算他从出生到今天共经历了多少个2月29日,输出计算结果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
bool flag(int a)
{
    if(a%4==0&&a%100!=0||a%400==0)
        return true;
    return false;
}
int main()
{
    int y,m,d,cnt = 0;
    cin>>y>>m>>d;
    for(int i = y;i < 2022;i++)
        if(flag(i)) cnt++;
    if(flag(y)&&m>=3) cnt--;
    cout<<cnt;
    return 0;
}

7.某班50位同学,依次输入同学姓名和生日。生日格式为yyyy/mm/dd。按照年龄从大到小的顺序输出每位同学的姓名

 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
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
using namespace std;
struct stu
{
    int year,month,day;
    char name[50];
};
int main()
{
    stu s[100],tmp;
    int i,j,n;cin>>n;
    for(i = 0;i < n;i ++ )
        cin>>s[i].name>>s[i].year>>s[i].month>>s[i].day;
    for(i = 0;i < n;i ++ )
        for(j = 0;j < n-i-1;j ++ )
            if(s[j].day>s[j+1].day)
            {
                tmp = s[j];
                s[j] = s[j+1];
                s[j+1] = tmp;
            }
    for(i = 0;i < n;i ++ )
        for(j = 0;j < n-i-1;j ++ )
            if(s[j].month>s[j+1].month)
            {
                tmp = s[j];
                s[j] = s[j+1];
                s[j+1] = tmp;
            }
    for(i = 0;i < n;i ++ )
        for(j = 0;j < n-i-1;j ++ )
            if(s[j].year>s[j+1].year)
            {
                tmp = s[j];
                s[j] = s[j+1];
                s[j+1] = tmp;
            }
    for(i = 0;i < n;i ++ )
        cout<<s[i].name<<" "<<s[i].year<<" "<<s[i].month<<" "<<s[i].day<<endl;
    return 0;
}

8.汉诺塔问题

游戏中有三根柱子,第一根柱子上摞着n片圆盘,小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘,如何将圆盘全部移到第三根柱子。输入汉诺塔层数,输出操作步骤。三根柱子依次为ABC

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
void hanoi(int n,char a,char b,char c)
{
    if(n==1)
        cout<<"move"<<" : "<<a<<" - "<<c<<endl;
    else
    {
        hanoi(n-1,a,c,b);
        cout<<"move"<<" : "<<a<<" - "<<c<<endl;
        hanoi(n-1,b,a,c);
    }
}
int main()
{
    int n;cin>>n;
    hanoi(n,'A','B','C');
    return 0;
}

9.输入正整数n,输出1到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
31
32
33
34
35
36
37
#include<iostream>
using namespace std;
int main()
{
    int n,num = 1,flag = 1;cin>>n;
    int r = 0,c = 0;
    int a[30][30] = {0};
    bool dir = true;
    while(flag!=n+1)
    {
        for(int i = 0;i < num&&flag!=n+1;i ++ )
        {
            if(i!=num-1&&dir)
                a[r--][c++] = flag++;
            else if(i==num-1&&dir)
                a[r][c++] = flag++;
            else if(i!=num-1&&! dir)
                a[r++][c--] = flag++;
            else if(i==num-1&&!dir)
                a[r++][c] = flag++;
        }
        dir = !dir;
        num++;
    }
    int x = 0;
    for(int i = 0;i < num;i ++ )
    {
        for(int j = 0;j < num;j ++ )
            if(a[i][j]!=0)
            {
                cout<<a[i][j]<<" ";
                x++;
            }
        cout<<endl;
    }
    return 0;
}
updatedupdated2023-01-072023-01-07