c ++零基础可视化——数组

c ++零基础可视化

数组

一些知识:
  1. 关于给数组赋值,一个函数为memset,其在cplusplus.com中的描述如下:

    c 复制代码
    void * memset ( void * ptr, int value, size_t num );

    Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

    ptr 指向的内存块的前字节 设置为指定 (解释为 an unsigned char )。

    parameter参数
    • ptr

      Pointer to the block of memory to fill. 指向要填充的内存块的指针。

    • value

      Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value . 要设置的值。该值作为 an int 传递,但该函数使用此值unsigned char 转换填充内存块。

    • num 编号

      Number of bytes to be set to the value . 要设置为该值 的字节数。 size_t is an unsigned integral type. size_t 是无符号整型。

  2. 关于给数组赋值,一个函数为fill,其在cplusplus.com中它的描述如下:

c 复制代码
template <class ForwardIterator, class T>  void fill (ForwardIterator first, ForwardIterator last, const T& val);

Assigns val to all the elements in the range [first,last).

将 val 分配给范围 [first,last) 中的所有元素。

Parameters 参数

  • first, last 第一个、最后一个

    Forward iterators to the initial and final positions in a sequence of elements that support being assigned a value of type T. The range filled is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. 将迭代器转发到支持为 type T 分配值的元素序列中的初始和最终位置。填充的范围是 [first,last),它包含 first 和 last 之间的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。

  • val

    Value to assign to the elements in the filled range. 要分配给填充区域中元素的值。

    例子:
    c 复制代码
    // fill algorithm example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::fill
    #include <vector>       // std::vector
    
    int main () {
      std::vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0
    
      std::fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
      std::fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0
    
      std::cout << "myvector contains:";
      for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        std::cout << ' ' << *it;
      std::cout << '\n';
    
      return 0;
    }
题目一https://www.luogu.com.cn/problem/B2020
c 复制代码
#include <bits/stdc++.h>

using namespace std;

using LL = long long;

#define endl "\n"

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    vector<int> nums(5);
    for(auto &u : nums) {
        cin >> u;
    }
    int sum = 0;
    for(int i = 0; i < 5; i ++) {
        sum += nums[i] % 3;
        nums[i] -= nums[i] % 3;
        nums[(i + 4) % 5] += nums[i] / 3;
        nums[(i + 1) % 5] += nums[i] / 3;
        nums[i] /= 3;
    }

    for(auto &u : nums) {
        cout << u << " ";
    }
    cout << endl;
    cout << sum << endl;

    return 0;
}

此处五个人围成一个圈,可以用取模运算。

关于对加法的理解,一个人的编号加上2就是前进两个人。例如编号为0的人加上2得到编号为2的人。总共有五个人, 所以加上五即为0+5=5,即又回到了自己的位置。若编号特别大则对5取模又可以将编号控制在0到4。

关于对减法的理解,一个人的编号减去1就是相当于加上了n-1个人。最后加上取模操作可将编号控制在0到4。

题目二https://www.luogu.com.cn/problem/B2064
c 复制代码
#include <bits/stdc++.h>

using namespace std;

using LL = long long;

#define endl "\n"

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int _;
    cin >> _;
    int nums[31] = {0};
    nums[1] = 1;
    nums[2] = 1;
    for(int i = 3; i <= 30; i ++) {
        nums[i] = nums[i - 1] + nums[i - 2];
    }
    while(_ --) {
        int x;
        cin >> x;
        cout << nums[x] << endl;
    }
    return 0;
}

对数组的理解:递推求解并存储答案。从微小的状态开始求取整个过程并用数组进行存储。每次计算出nums[i],即nums[i] = 计算的公式。

题目三:https://www.luogu.com.cn/problem/B2079
c 复制代码
#include <bits/stdc++.h>

using namespace std;

using LL = long long;

#define endl "\n"

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    
    int n;
    cin >> n;
    double fact[16] = {1};
    double e = 1;
    for(int i = 1; i <= n; i ++) {
        fact[i] = i * fact[i - 1];
        e += 1 / fact[i];
    }

    cout << fixed << setprecision(10) << e << endl;
    return 0;
}

对数组的理解:和上面的方法相同,采用递推的方法求解阶乘并将其存储在数组中。

关于以下代码,是我学到的新方法:

c 复制代码
#include <bits/stdc++.h>

using namespace std;

using LL = long long;

#define endl "\n"

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    
    int n;
    cin >> n;
    double invfact[16] = {1}, e = 1;
    for(int i = 1; i < 16; i ++) {
        invfact[i] = invfact[i - 1] / i;
    }
    e += accumulate(invfact + 1, invfact + n + 1, 0.0);
    cout << fixed << setprecision(10) << e << endl;
    return 0;
}

其中的accumulate函数极其精妙。

cplusplus.com中的描述:

c 复制代码
template <class InputIterator, class T>   T accumulate (InputIterator first, InputIterator last, T init);
template <class InputIterator, class T, class BinaryOperation>   T accumulate (InputIterator first, InputIterator last, T init, BinaryOperation binary_op);

Returns the result of accumulating all the values in the range [first,last) to init.

返回将 [first,last] 范围内的所有值累加到 init

The default operation is to add the elements up, but a different operation can be specified as binary_op.

默认操作是将元素相加,但可以将不同的操作指定为 binary_op

first, last 第一个、最后一个

Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
将迭代器输入到序列中的初始和最终位置。使用的范围是 [first,last),它包含 first 和 last 之间的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。

init 初始化

Initial value for the accumulator.

累加器的初始值。

binary_op

Binary operation taking an element of type T as first argument and an element in the range as second, and which returns a value that can be assigned to type T.

将 type T 的元素作为第一个参数,将 range 中的元素作为第二个参数的二进制运算,并返回一个可分配给 type T 的值。

This can either be a function pointer or a function object.

这可以是函数指针或函数对象。

The operation shall not modify the elements passed as its arguments.

该操作不得修改作为其参数传递的元素。

相关推荐
kobesdu1 分钟前
当算法跑不通时:3D激光SLAM工程实践中的隐藏陷阱与全链路排查
算法·3d
我不是懒洋洋3 分钟前
大语言模型(LLM)入门:从Transformer到ChatGPT
c语言·开发语言·c++
金创想3 分钟前
积木移动题目分析及解题思路——木块问题(1)
c++·算法·字符串·c·刷题·信息学奥赛·积木
研究点啥好呢8 分钟前
小鹏汽车 机器人运动规划算法工程师 面试题精选:10道高频考题+答案解析
算法·机器人·汽车
小许同学记录成长12 分钟前
原始 IQ 数据时频图生成
python·算法
小小测试开发16 分钟前
OpenAI 模型攻克离散几何 80 年难题:Erdős 单位距离猜想被 AI 证明
人工智能·算法·机器学习
moonsims17 分钟前
从“传感器融合”升级为“多机器人约束融合系统”-Factor Graph 多约束融合
人工智能·算法
Dfreedom.22 分钟前
模型剪枝完全指南:从理论到实践,打造高效深度学习模型
人工智能·算法·机器学习·剪枝·模型加速
BestOrNothing_201530 分钟前
C++零基础到工程实战(5.2.4):指针与引用在函数传参、返回值与效率优化中的应用
c++·指针·引用·const·函数参数
L_090733 分钟前
【C++】面向对象三大特性之多态
开发语言·c++