1.模拟算法

一.多项式输出

https://www.luogu.com.cn/problem/P1067

1.算法原理

2.代码实现

cpp 复制代码
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for(int i = n;i >= 0;i--)
    {
        int a;
        cin >> a;
        if(a == 0)
        {
            continue;
        }

        //1. 符号
        if(a < 0)
        {
            cout << "-";
        }
        else
        {
            if(i != n)
            {
                cout << "+";
            }
        }

        //2.数字
        a = abs(a);
        if(a != 1 || (a == 1 && i == 0))
        {
            cout << a;
        }

        //3.次数
        if(i == 0)
        {
            continue;
        }
        else if(i == 1)
        {
            cout << 'x';
        }
        else
        {
            cout << "x^" << i;
        }
    }
    return 0;
}

二.蛇形方阵

https://www.luogu.com.cn/problem/P5731

1.算法原理

方向向量的定义的顺序也是有方法的(要符合我们的顺序):

我们的pos要不越界,那么我们就要对其进行取模操作

2.代码实现

cpp 复制代码
#include <iostream>
#include <cmath>

using namespace std;

const int N = 15;

//定义 右,下,左,上 四个方向
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

int arr[N][N];

int main()
{
    int n;
    cin >> n;

    int x = 1,y = 1; //初始位置
    int cnt = 1;     //当前位置
    int pos = 0;     //当前的方向

    while(cnt <= n * n)
    {
        arr[x][y] = cnt;

        //计算下一个位置
        int a = x + dx[pos];
        int b = y + dy[pos];

        //判断是否越界
        if(a < 1 || a > n || b < 1 || b > n || arr[a][b])
        {
            pos = (pos + 1) % 4;
            a = x + dx[pos],b = y + dy[pos];
        }
        x = a,y = b;
        cnt++;
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            printf("%3d",arr[i][j]);
        }
        printf("\n");
    }


    return 0;
}

三.字符串的展开

https://www.luogu.com.cn/problem/P1098

1.算法原理

2.代码实现

cpp 复制代码
#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int p1,p2,p3,n;
string s;
string ret;

//判断是否是数字
bool isdig(char ch)
{
    return ch >= '0' && ch <= '9';
}

//判断是否是小写字母
bool islet(char ch)
{
    return ch >= 'a' && ch <= 'z';
}

//[left,right]之间的字符进行展开
void add(char left,char right)
{
    string t;
    for(char ch = left + 1;ch < right; ch++)
    {
        //p1
        char tmp = ch;
        if(p1 == 2 && islet(tmp))
        {
            tmp -= 32;
        }
        else if(p1 == 3)
        {
            tmp = '*';
        }

        //p2
        for(int i = 0;i < p2;i++)
        {
            t += tmp;
        }
    }
    //p3
    if(p3 == 2)
    {
        reverse(t.begin(),t.end());
    }
    ret += t;
}

int main()
{
    cin >> p1 >> p2 >> p3 >> s;
    n = s.size();
    for(int i = 0;i < n; i++)
    {
        char ch = s[i];
        if(s[i] != '-' || i == 0 || i == n-1)
        {
            ret += ch;
        }
        else
        {
            char left = s[i - 1],right = s[i + 1];
            //判断是否展开
            if(isdig(left) && isdig(right) && right > left ||
               islet(left) && islet(right) && right > left)
            {
                //展开
                add(left,right);
            }
            else
            {
                ret += ch;
            }
        }
    }
    cout << ret << endl;
    return 0;
}
相关推荐
我在人间贩卖青春2 小时前
C++之析构函数
c++·析构函数
野犬寒鸦2 小时前
从零起步学习并发编程 || 第六章:ReentrantLock与synchronized 的辨析及运用
java·服务器·数据库·后端·学习·算法
wenzhangli72 小时前
ooderA2UI BridgeCode 深度解析:从设计原理到 Trae Solo Skill 实践
java·开发语言·人工智能·开源
霖霖总总2 小时前
[小技巧66]当自增主键耗尽:MySQL 主键溢出问题深度解析与雪花算法替代方案
mysql·算法
rainbow68892 小时前
深入解析C++STL:map与set底层奥秘
java·数据结构·算法
灵感菇_2 小时前
Java 锁机制全面解析
java·开发语言
我在人间贩卖青春2 小时前
C++之数据类型的扩展
c++·字符串·数据类型
wazmlp0018873692 小时前
python第三次作业
开发语言·python
娇娇乔木2 小时前
模块十一--接口/抽象方法/多态--尚硅谷Javase笔记总结
java·开发语言
明月醉窗台2 小时前
qt使用笔记六之 Qt Creator、Qt Widgets、Qt Quick 详细解析
开发语言·笔记·qt