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;
}
相关推荐
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
weilx12341 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读1 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛1 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark1 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·1 天前
C++ 萌新语法入门篇
c++·算法比赛