[2021夏季学期编程训练题四]解题思路及参考代码

由 B1ue1nWh1te 发布

更新日志

[2021年9月5日] 更新了难度1和2的题解。

[2021年9月14日] 更新了难度3和4的题解,动态规划的题目借用CSDN上的一个优秀博主(Karltan)的代码,学习了。

前言

文章内容为作者个人学习心得,解题思路及参考代码不一定是最优的,如发现有不正确的地方或更优的解法,欢迎批评指正或讨论交流,联系方式可以在页面下方找到。

1.在霍格沃茨找零钱

1

temp = input().split()
v1, v2 = temp[0].split("."), temp[1].split(".")
for i in range(3):
    v1[i] = int(v1[i])
    v2[i] = int(v2[i])
s1 = (v1[0] * 17 + v1[1]) * 29 + v1[2]
s2 = (v2[0] * 17 + v2[1]) * 29 + v2[2]
c = s2 - s1
ac = abs(c)
g = ac // (17 * 29)
s = (ac - g * 17 * 29) // 29
k = ac - g * 17 * 29 - s * 29
if c < 0:
    print("-{}.{}.{}".format(g, s, k))
else:
    print("{}.{}.{}".format(g, s, k))

2.最简单的计算机

2

def A():
    global M1, R1
    R1 = M1


def B():
    global M2, R2
    R2 = M2


def C():
    global M1, R3
    M1 = R3


def D():
    global M2, R3
    M2 = R3


def E():
    global R1, R2, R3
    R3 = R1 + R2


def F():
    global R1, R2, R3
    R3 = R1 - R2


try:
    while True:
        M1, M2 = input().split()
        M1, M2 = int(M1), int(M2)
        R1, R2, R3 = 0, 0, 0
        Commands = input()
        for i in Commands:
            eval(i)()  # 调用名字i对应的函数
        print(f"{M1},{M2}")
except EOFError:
    pass

3.相同生日

3

n = int(input())
data = []
for i in range(n):
    temp = input().split()
    date = int(temp[1]) * 100 + int(temp[2])  # 处理日期
    flag = 0
    for j in range(len(data)):
        if date == data[j][0]:
            data[j].append(temp[0])  # 将生日相同的学号存入同一个列表
            flag = 1
            break
    if flag == 0:
        l = [date, temp[0]]
        data.append(l)
data.sort(key=lambda x: x[0])  # 按日期大小排序
for i in data:
    d = i[0] % 100  # 提取日
    m = (i[0] - d) // 100  # 提取月
    i.pop(0)  # 弹出
    print("{} {} {}".format(m, d, " ".join(i)))

4.日历问题

4

此题解来自CSDN。

#include <iostream>
#include <string>
using namespace std;

bool checkleap(int n) //这个函数检查n年是不是闰年
{
    //被400整除 或 被4整除且不被100整除,则为闰年
    if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
        return true;
    return false; //否则为平年
}

int Day_Of_Year(int year)
{
    if (checkleap(year))
        return 366; //如果是闰年,返回366天
    return 365;     //否则只有365天
}

int Day_Of_Month(int year, int month) //根据年数以及月数返回该月的天数
{
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        return 31;       //1,3,5,7,8,10,12月都有31天
    else if (month == 2) //如果是二月
    {
        if (checkleap(year))
            return 29; //如果是闰年,返回29
        else
            return 28; //否则返回28
    }
    else
        return 30; //其他的月份都返回30
}

string weekdays(int n) //根据总共的天数返回处在周几
{                      //可以直接试出来
    string ret;
    if (n % 7 == 1)
        ret = "Saturday";
    else if (n % 7 == 2)
        ret = "Sunday";
    else if (n % 7 == 3)
        ret = "Monday";
    else if (n % 7 == 4)
        ret = "Tuesday";
    else if (n % 7 == 5)
        ret = "Wednesday";
    else if (n % 7 == 6)
        ret = "Thursday";
    else
        ret = "Friday";
    return ret;
}

void print(int year, int month, int day, int n) //打印函数
{
    if (month < 10) //如果月份数小于10,那么需要输出一个前导0
    {
        //天数小于10同理
        if (day < 10)
            cout << year << "-0" << month << "-0" << day << " " << weekdays(n) << endl;
        else
            cout << year << "-0" << month << "-" << day << " " << weekdays(n) << endl;
    }
    else
    {
        if (day < 10)
            cout << year << "-" << month << "-0" << day << " " << weekdays(n) << endl;
        else
            cout << year << "-" << month << "-" << day << " " << weekdays(n) << endl;
    }
}

int main()
{
    int n;
    while (cin >> n, n != -1)
    {
        n++; //包括开始这一天
        int sum = n, year = 2000, month = 1, day;
        //sum-天数,year-年份从2000开始,month-月份,从1开始,day-日期
        //for(year=2000;sum>0;year++) sum-=Day_Of_Year(year);
        while (sum > 0)
            sum -= Day_Of_Year(year++); //如果当前剩余天数大于0,就将总天数减去当前年份的天数
        year--;                         //运行至此说明sum<0,多减了一年
        sum += Day_Of_Year(year);       //将这多减的一年加回来
        //for(month=1;sum>0;month++) sum-=Day_Of_Month(year,month);
        while (sum > 0)
            sum -= Day_Of_Month(year, month++); //然后一样的步骤开始算月份
        month--;
        sum += Day_Of_Month(year, month);
        day = sum;                  //最后剩下的就是日期了
        print(year, month, day, n); //输出
    }
    return 0;
}

5.小希的数表

5

此题解来自CSDN。

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 10010;
int n;
int sum[N]; //储存一开始输入的和
int a[N];   //储存最后算出来的每个数
int st[N];  //标记每个和出现了多少次

int main()
{
    while (cin >> n, n) //多case
    {
        int m = n * (n - 1) / 2;    //和的个数
        for (int i = 0; i < m; i++) //输入和
            cin >> sum[i];
        for (int k = 1; k <= sum[0]; k++) //假设数列的第一个数
        {
            a[0] = k;                   //假设这个数为1,2,...,sum[0]
            int j = 1;                  //a[0]已经确定,所以j从1开始
            memset(st, 0, sizeof(st));  //将存储个数的数组初始化
            for (int i = 0; i < m; i++) //用st数组来标记,以供下面判断
                st[sum[i]]++;
            for (int i = 0; i < m; i++)
            {
                if (st[sum[i]]) //这步很重要,判断sum[i]是否已经由两个数组成过
                {
                    a[j] = sum[i] - a[0]; //假设另一个数
                    int t;
                    for (t = 0; t < j; t++) //遍历已经找到的数
                    {
                        if (st[a[j] + a[t]])   //如果a[j]+a[t]可以组成sum[]数列的一个数
                            st[a[j] + a[t]]--; //这个数的个数就减一
                        else                   //若组不成,则证明找的这个数不对
                            break;
                    }
                    if (t == j) //从t找到j一直没有冲突,说明符合题意
                        j++;
                    else
                        break;
                }
            }
            if (j >= n) //找到满足要求的数列
                break;
        }
        for (int i = 0; i < n; i++)
            cout << a[i] << " ";
        cout << endl;
    }
    return 0;
}

6.数塔

6

#include <iostream>
using namespace std;

const int N = 110;
int a[N][N], b[N][N];
int incremental[N][N];
//a数组用于求出最大路径长度
//b数组用于输出路径
//incremental数组用于存储增量

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            cin >> a[i][j];
            b[i][j] = a[i][j];
        }
    }

    for (int i = n - 2; i >= 0; i--) //从倒数第二层开始
    {
        for (int j = 0; j <= i; j++)
        {
            if (a[i + 1][j] > a[i + 1][j + 1]) //若是左>右,那么直接往左走,增量为0
            {
                a[i][j] += a[i + 1][j];
            }
            else //否则说明左<右,往右走,增量为1
            {
                a[i][j] += a[i + 1][j + 1];
                incremental[i][j] = 1;
            }
        }
    }
    cout << a[0][0] << endl;    //顶点位置就是最大路径值
    int j = 0;                  //从第一个开始
    for (int i = 0; i < n; i++) //遍历每一层
    {
        cout << b[i][j] << " "; //输出路径上的数字
        j += incremental[i][j]; //加上路径上的增量这样就实现了路径的输出
    }
    cout << endl;
    return 0;
}

7.斯诺克台球

7-1

7-2

7-3

7-4

这题直接在网上找的,建议复制代码直接提交,硬写意义不大。

#include <iostream>
#include <map>
using namespace std;
struct people
{
    int n;
    int a[102];
    int sum;
    int dangan[102];
    int sss;
};
map<char, int> aa;
map<char, int> bb;
map<char, int> dd;
char cc[7] = {'r', 'y', 'g', 'c', 'b', 'p', 'B'};
int main()
{
    aa['r'] = 1;
    aa['y'] = 2;
    aa['g'] = 3;
    aa['c'] = 4;
    aa['b'] = 5;
    aa['p'] = 6;
    aa['B'] = 7;
    int t;
    cin >> t;
    getchar();
    people A, B;
    A.n = t;
    B.n = t;
    string x, y, X;
    int m = 0, k = 0, p = 0, hong = 15, huang = 1, lve = 1, cafe = 1, lan = 1, fen = 1, hei = 1;

    for (p = 0; p < t; p++)
    {
        hong = 15;
        huang = 1;
        lve = 1;
        cafe = 1;
        lan = 1;
        fen = 1;
        hei = 1;
        m = 0; //m表示第几个人
        A.a[p] = 0;
        A.sum = 0;
        A.dangan[p] = 0;
        A.sss = 0;
        B.a[p] = 0;
        B.sum = 0;
        B.dangan[p] = 0;
        B.sss = 0;
        x = "";
        y = "";
        k = 0; //k表示第几轮
        while (getline(cin, X) && X != "-1")
        {
            x = "";
            y = "";
            if (X.find(" ") != string::npos)
            {
                x = x + X.substr(0, X.find(" "));
                y = y + X.substr(X.find(" ") + 1);
            }
            else
            {
                x = x + X;
                y = "";
            }

            dd.clear();
            dd['r'] = 0;
            dd['y'] = 0;
            dd['g'] = 0;
            dd['c'] = 0;
            dd['b'] = 0;
            dd['p'] = 0;
            dd['B'] = 0;
            for (int i = 0; i < x.length(); i++)
            {
                dd[x[i]]++;
            }
            int l = 0;
            for (int i = 0; i < 7; i++)
            {
                if (dd[cc[i]] != 0)
                    l++;
            }

            bb.clear();
            bb['r'] = 0;
            bb['y'] = 0;
            bb['g'] = 0;
            bb['c'] = 0;
            bb['b'] = 0;
            bb['p'] = 0;
            bb['B'] = 0;
            for (int i = 0; i < y.length(); i++)
            {
                if (y[i] > '9' && y[i] != 'r')
                {
                    bb[y[i]]++;
                }
                else if (y[i] == 'r' && (y[i + 2] > '9' || i + 2 == y.length()) && (y[i + 1] >= '1' && y[i + 1] <= '9'))
                {
                    bb[y[i]] = bb[y[i]] + (y[i + 1] - '0');
                    i++;
                }
                else if (y[i] == 'r' && (y[i + 2] >= '0' && y[i + 2] <= '9') && (y[i + 1] >= '1' && y[i + 1] <= '9'))
                {
                    bb[y[i]] = bb[y[i]] + (y[i + 1] - '0') * 10 + (y[i + 2] - '0');
                    i = i + 2;
                }
            }
            int q = 0;
            for (int i = 0; i < 7; i++)
            {
                if (bb[cc[i]] != 0)
                    q++;
            }

            if (x == "NULL") //x为空
            {
                k = 0;
                if (m % 2 == 0)
                {
                    B.a[p] = B.a[p] + 4;
                    m++;
                }
                else
                {
                    A.a[p] = A.a[p] + 4;
                    m++;
                }
            }
            else
            {
                if (hong > 0 && k % 2 == 0 && l == 1 && x[0] == 'r' && q == 1 && y[0] == 'r') //第一轮不犯规
                {
                    k = 1;
                    if (m % 2 == 0)
                    {
                        A.a[p] += bb['r'];
                        A.sum += bb['r'];
                    }
                    else
                    {
                        B.a[p] += bb['r'];
                        B.sum += bb['r'];
                    }
                }
                else if (hong >= 0 && k % 2 != 0 && x.length() == 1 && x[0] != 'r' && q == 1 && y[0] == x[0]) //第二轮不犯规
                {
                    k = 0;
                    if (m % 2 == 0)
                    {
                        A.a[p] += aa[y[0]];
                        A.sum += aa[y[0]];
                    }
                    else
                    {
                        B.a[p] += aa[y[0]];
                        B.sum += aa[y[0]];
                    }
                }
                else if (k % 2 == 0 && x[0] == 'r' && hong > 0 && l == 1 && y.length() == 0) //y为空
                {
                    if (m % 2 == 0)
                    {
                        if (A.sum >= 100)
                            A.dangan[p]++;
                        A.sum = 0;
                    }
                    else
                    {
                        if (B.sum >= 100)
                            B.dangan[p]++;
                        B.sum = 0;
                    }
                    m++;
                    k = 0;
                }
                else if (k % 2 != 0 && x[0] != 'r' && hong >= 0 && l == 1 && q == 0) //y为空
                {
                    if (m % 2 == 0)
                    {
                        if (A.sum >= 100)
                            A.dangan[p]++;
                        A.sum = 0;
                    }
                    else
                    {
                        if (B.sum >= 100)
                            B.dangan[p]++;
                        B.sum = 0;
                    }
                    m++;
                    k = 0;
                }
                else if (k % 2 == 0 && hong <= 0)
                {
                    if (huang == 1)
                    {
                        if (x.length() == 1 && y.length() == 1 && x[0] == 'y' && y[0] == 'y')
                        {
                            if (m % 2 == 0)
                            {
                                A.a[p] += aa[y[0]];
                                A.sum += aa[y[0]];
                            }
                            else
                            {
                                B.a[p] += aa[y[0]];
                                B.sum += aa[y[0]];
                            }
                            huang = 0;
                        }
                        else if (x.length() == 1 && y.length() == 0 && x[0] == 'y')
                        {
                            if (m % 2 == 0)
                            {
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                            m++;
                        }
                        else
                        {
                            k = 0;
                            if (m % 2 == 0)
                            {
                                //if(bb['r']>7) B.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    B.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    B.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    B.a[p] += 5;
                                else
                                    B.a[p] += 4;
                                m++;
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                //if(bb['r']>7) A.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    A.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    A.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    A.a[p] += 5;
                                else
                                    A.a[p] += 4;
                                m++;
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                        }
                    }

                    else if (huang == 0 && lve == 1)
                    {
                        if (x.length() == 1 && y.length() == 1 && x[0] == 'g' && y[0] == 'g')
                        {
                            if (m % 2 == 0)
                            {
                                A.a[p] += aa[y[0]];
                                A.sum += aa[y[0]];
                            }
                            else
                            {
                                B.a[p] += aa[y[0]];
                                B.sum += aa[y[0]];
                            }
                            lve = 0;
                        }
                        else if (x.length() == 1 && y.length() == 0 && x[0] == 'g')
                        {
                            if (m % 2 == 0)
                            {
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                            m++;
                        }
                        else
                        {
                            if (m % 2 == 0)
                            {
                                //if(bb['r']>7) B.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    B.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    B.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    B.a[p] += 5;
                                else
                                    B.a[p] += 4;
                                m++;
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                //if(bb['r']>7) A.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    A.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    A.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    A.a[p] += 5;
                                else
                                    A.a[p] += 4;
                                m++;
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                        }
                    }

                    else if (huang == 0 && lve == 0 && cafe == 1)
                    {
                        if (x.length() == 1 && y.length() == 1 && x[0] == 'c' && y[0] == 'c')
                        {
                            if (m % 2 == 0)
                            {
                                A.a[p] += aa[y[0]];
                                A.sum += aa[y[0]];
                            }
                            else
                            {
                                B.a[p] += aa[y[0]];
                                B.sum += aa[y[0]];
                            }
                            cafe = 0;
                        }
                        else if (x.length() == 1 && y.length() == 0 && x[0] == 'c')
                        {
                            if (m % 2 == 0)
                            {
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                            m++;
                        }
                        else
                        {
                            if (m % 2 == 0)
                            {
                                //if(bb['r']>7) B.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    B.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    B.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    B.a[p] += 5;
                                else
                                    B.a[p] += 4;
                                m++;
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                //if(bb['r']>7) A.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    A.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    A.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    A.a[p] += 5;
                                else
                                    A.a[p] += 4;
                                m++;
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                        }
                    }

                    else if (lan == 1 && cafe == 0 && huang == 0 && lve == 0)
                    {
                        if (x.length() == 1 && y.length() == 1 && x[0] == 'b' && y[0] == 'b')
                        {
                            if (m % 2 == 0)
                            {
                                A.a[p] += aa[y[0]];
                                A.sum += aa[y[0]];
                            }
                            else
                            {
                                B.a[p] += aa[y[0]];
                                B.sum += aa[y[0]];
                            }
                            lan = 0;
                        }
                        else if (x.length() == 1 && y.length() == 0 && x[0] == 'b')
                        {
                            if (m % 2 == 0)
                            {
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                            m++;
                        }
                        else
                        {
                            k = 0;
                            if (m % 2 == 0)
                            {
                                //if(bb['r']>7) B.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    B.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    B.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    B.a[p] += 5;
                                else
                                    B.a[p] += 4;
                                m++;
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                //if(bb['r']>7) A.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    A.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    A.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    A.a[p] += 5;
                                else
                                    A.a[p] += 4;
                                m++;
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                        }
                    }

                    else if (fen == 1 && lan == 0 && cafe == 0 && huang == 0 && lve == 0)
                    {
                        if (x.length() == 1 && y.length() == 1 && x[0] == 'p' && y[0] == 'p')
                        {
                            if (m % 2 == 0)
                            {
                                A.a[p] += aa[y[0]];
                                A.sum += aa[y[0]];
                            }
                            else
                            {
                                B.a[p] += aa[y[0]];
                                B.sum += aa[y[0]];
                            }
                            fen = 0;
                        }
                        else if (x.length() == 1 && y.length() == 0 && x[0] == 'p')
                        {
                            if (m % 2 == 0)
                            {
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                            m++;
                        }
                        else
                        {
                            if (m % 2 == 0)
                            {
                                //if(bb['r']>7) B.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    B.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    B.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    B.a[p] += 5;
                                else
                                    B.a[p] += 4;
                                m++;
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                //if(bb['r']>7) A.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    A.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    A.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    A.a[p] += 5;
                                else
                                    A.a[p] += 4;
                                m++;
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                        }
                    }

                    else if (hei == 1 && fen == 0 && lan == 0 && cafe == 0 && huang == 0 && lve == 0)
                    {
                        if (x.length() == 1 && y.length() == 1 && x[0] == 'B' && y[0] == 'B')
                        {
                            if (m % 2 == 0)
                            {
                                A.a[p] += aa[y[0]];
                                A.sum += aa[y[0]];
                            }
                            else
                            {
                                B.a[p] += aa[y[0]];
                                B.sum += aa[y[0]];
                            }
                            hei = 0;
                        }
                        else if (x.length() == 1 && y.length() == 0 && x[0] == 'B')
                        {
                            if (m % 2 == 0)
                            {
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                            m++;
                        }
                        else
                        {
                            if (m % 2 == 0)
                            {
                                //if(bb['r']>7) B.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    B.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    B.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    B.a[p] += 5;
                                else
                                    B.a[p] += 4;
                                m++;
                                if (A.sum >= 100)
                                    A.dangan[p]++;
                                A.sum = 0;
                            }
                            else
                            {
                                //if(bb['r']>7) A.a[p]+=bb['r'];
                                if (bb['B'] == 1 || dd['B'] == 1)
                                    A.a[p] += 7;
                                else if (bb['p'] == 1 || dd['p'] == 1)
                                    A.a[p] += 6;
                                else if (bb['b'] == 1 || dd['b'] == 1)
                                    A.a[p] += 5;
                                else
                                    A.a[p] += 4;
                                m++;
                                if (B.sum >= 100)
                                    B.dangan[p]++;
                                B.sum = 0;
                            }
                        }
                    }
                }
                else //犯规
                {
                    k = 0;
                    if (m % 2 == 0)
                    {
                        //if(bb['r']>7) B.a[p]+=bb['r'];
                        if (bb['B'] == 1 || dd['B'] == 1)
                            B.a[p] += 7;
                        else if (bb['p'] == 1 || dd['p'] == 1)
                            B.a[p] += 6;
                        else if (bb['b'] == 1 || dd['b'] == 1)
                            B.a[p] += 5;
                        else
                            B.a[p] += 4;
                        m++;
                        if (A.sum >= 100)
                            A.dangan[p]++;
                        A.sum = 0;
                    }
                    else
                    {
                        //if(bb['r']>7) A.a[p]+=bb['r'];
                        if (bb['B'] == 1 || dd['B'] == 1)
                            A.a[p] += 7;
                        else if (bb['p'] == 1 || dd['p'] == 1)
                            A.a[p] += 6;
                        else if (bb['b'] == 1 || dd['b'] == 1)
                            A.a[p] += 5;
                        else
                            A.a[p] += 4;
                        m++;
                        if (B.sum >= 100)
                            B.dangan[p]++;
                        B.sum = 0;
                    }
                }
            }
            hong = hong - bb['r'];
        }
        cout << A.a[p] << ":" << B.a[p] << endl;
        if (A.sum >= 100)
            A.dangan[p]++;
        if (B.sum >= 100)
            B.dangan[p]++;
    }
    for (int i = 0; i < t; i++)
    {
        if (A.dangan[i] != 0)
            A.sss++;
        if (B.dangan[i] != 0)
            B.sss++;
    }
    cout << A.sss << ":" << B.sss;
    return 0;
}

8.最少钱币数

8

此题解来自CSDN。

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main()
{
    int m, n;
    while (cin >> m, m)
    {
        int f[m + 1]; //凑i元需要的硬币数的最小值
        cin >> n;
        int w[n];
        for (int i = 0; i < n; i++)
            cin >> w[i];
        for (int i = 0; i <= m; i++)
            f[i] = 999999;
        f[0] = 0;
        for (int i = 1; i <= m; i++)    //遍历所有的钱数
            for (int j = 0; j < n; j++) //遍历所有的硬币
                if (i >= w[j])          //如果钱数大于硬币面值
                    f[i] = min(f[i], f[i - w[j]] + 1);
        if (f[m] == 999999)
            cout << "Impossible" << endl;
        else
            cout << f[m] << endl;
    }
    return 0;
}

9.相等的多项式

9-1

9-2

此题解来自CSDN。

#include <iostream>
using namespace std;

const int N = 25;
int n;
int a[N], b[N], aex[N];
//a数组存每个式子的常数
//b数组存的是输入的最后的结果(下标对应次数项的系数)
//aex数组存的是最后算出来的结果(下标对应次数项的系数)

int main()
{
    while (cin >> n, n)
    {
        for (int i = 1; i <= n; i++) //下标从1开始,因为后续有下标-1的操作
            cin >> a[i];
        for (int i = n - 1; i >= 0; i--)
            cin >> b[i];
        b[n] = 1;      //最后一项的次数是1
        aex[0] = a[1]; //次数为0的项的系数是第一项的常数(假设只有一个式子)
        aex[1] = 1;    //次数为1的项的系数是1(假设只有一个式子)
        for (int i = 2; i <= n; i++)
        {
            aex[i] = 1;                              //每次将最高次数项的系数置为1
            for (int j = i - 1; j > 0; j--)          //遍历所有已计算的项
                aex[j] = aex[j] * a[i] + aex[j - 1]; //次数为j的项 = 次数为j的项*另一项的常数 + 次数为j-1的项*1
            aex[0] *= a[i];                          //最后的常数项也乘上这个常数
        }
        int sum = 0;
        for (int i = 0; i < n; i++)
            if (aex[i] == b[i]) //如果对应次数项的系数相同
                sum++;
        if (sum == n)
            cout << 'Y' << endl; //若有n个都相同,说明整个都相同
        else
            cout << 'N' << endl;
    }
    return 0;
}

10.选美比赛

10

n = int(input())
scores = input().split()
l = list(set(scores))  # 去除重复的分数
l.sort(key=lambda x: int(x), reverse=True)  # 排名
temp = []
for i in scores:
    temp.append(str(l.index(i) + 1))  # 找到某个分数对应的排名
print("{}".format(" ".join(temp)))

11.蛇行矩阵

11

n = int(input())
head = 1
matrix = []
for i in range(n):  # 第一列的规律
    head += i
    matrix.append([head])
for i in range(n):  # 行的规律
    foot = 2 + i
    while foot < n + 1:
        temp = matrix[i][-1] + foot
        matrix[i].append(temp)
        foot += 1
for i in matrix:
    for j in range(len(i)):
        i[j] = str(i[j])
    print(" ".join(i))

12.疫情期间

12-1

12-2

此题解来自CSDN。

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int f[n + 1][3]; //f[i][j]表示到第i天为止且第i天做了j的最小休息天数
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            f[i][j] = 999;
        }
    }
    f[0][0] = f[0][1] = f[0][2] = 0; //如果只有0天,不管怎么样都没得休息
    int x;
    for (int i = 1; i <= n; i++) //由于有下标-1的操作,所以我们从1开始
    {
        cin >> x;
        f[i][0] = min(f[i - 1][0], min(f[i - 1][1], f[i - 1][2])) + 1; //今天休息的话,昨天干什么都可以,这种情况必定存在,不需要判断
        if (x == 1 || x == 3)                                          //如果今天可以编程
            f[i][1] = min(f[i - 1][0], f[i - 1][2]);                   //昨天可以休息和健身
        if (x == 2 || x == 3)                                          //如果今天可以健身
            f[i][2] = min(f[i - 1][0], f[i - 1][1]);                   //昨天可以休息和编程
    }
    cout << min(f[n][0], min(f[n][1], f[n][2])) << endl; //第n天可以有三种情况,输出其中的最小值
    return 0;
}

13.7,还是7

13

n = int(input())
for i in range(7, n + 1):
    if i % 7 == 0 or '7' in str(i):
        print(i)

14.组个最小数

14

l = input().split()
l2 = []  # 存非0的数
zero = 0  # 0的个数
for i in range(len(l)):
    l[i] = int(l[i])
    if l[i] != 0:
        l2.append(l[i])
    else:
        zero += 1
m = min(l2)  # 找出最小的数当首位
l2.pop(l2.index(m))  # 弹出
l2.sort()  # 剩下的数从小到大排序
num = str(m) + '0' * zero  # 首位与n个0拼接
for i in l2:
    num += str(i)
print(num)

15.字频统计

15

while True:
    n = int(input())
    if n == 0:
        break
    s = input()
    t1 = s.count("ab")
    t2 = s.count("ba")
    print(t1 - t2)

16.逆序数

16

#include <iostream>
using namespace std;

int main()
{
    int n;
    int sum;
    while (cin >> n, n)
    {
        sum = 0;
        int a[n];
        for (int i = 0; i < n; i++)
            cin >> a[i];
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                if (a[i] > a[j])
                    sum++;
        cout << sum << endl;
    }
    return 0;
}

17.最小钱币数

17

n = int(input())
a = n // 100
b = (n - a * 100) // 20
c = (n - a * 100 - b * 20) // 10
d = (n - a * 100 - b * 20 - c * 10) // 5
e = n - a * 100 - b * 20 - c * 10 - d * 5
print(a + b + c + d + e)

18.身份证校验

18

w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
c = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
while True:
    n = input()
    if n == "-1":
        break
    s = 0
    for i in range(17):
        s += int(n[i]) * w[i]
    y = s % 11
    k = c[y]
    if n[-1] == k:
        print(1)
    else:
        print(0)

19.最长连续递增子序列

19

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int num[n];
    int max = 0;
    for (int i = 0; i < n; i++)
        cin >> num[i];
    for (int i = 0; i < n; i++)
    {
        int m = 1;
        for (int j = i + 1; j < n; j++)
        {
            if (num[j] > num[j - 1])
                m++;
            else
                break;
        }
        if (max < m)
            max = m;
    }
    cout << max << endl;
    return 0;
}

20.恺撒Caesar密码

20

def Caesar_Decrypt(Text, Move=5):
    UpperCaseWord = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    ]
    LowerCaseWord = [
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    ]
    DecryptedText = ""
    for i in Text:
        if i in UpperCaseWord:
            DecryptedText = DecryptedText + chr((ord(i) - ord('A') - int(Move)) % 26 + ord('A'))
        elif i in LowerCaseWord:
            DecryptedText = DecryptedText + chr((ord(i) - ord('a') - int(Move)) % 26 + ord('a'))
        else:
            DecryptedText = DecryptedText + i
    return DecryptedText


while True:
    start = input()
    if start == "ENDOFINPUT":
        break
    text = input()
    end = input()
    print(Caesar_Decrypt(text))

21.回文串

21

Python的代码是对的,但是交到服务器上会出错,无法输出,用C++的代码可以正常运行(在CSDN找的)。

def isneed(s):
    if s == s[::-1]:
        return True
    else:
        return False


while True:
    n = input().strip()
    if n == "0":
        break
    s = input()
    if isneed(s):
        print("0")
    else:
        for i in range(1, len(s)):
            temp = s + s[:i][::-1]
            if isneed(temp):
                print(str(i))
                break

暂无留言

发表留言


知 乎 推 特 QQ Mail Github Xayah 贰猹的小窝 St.Lee