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.

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

相关推荐
虾球xz31 分钟前
游戏引擎学习第276天:调整身体动画
c++·学习·游戏引擎
虾球xz32 分钟前
游戏引擎学习第275天:将旋转和剪切传递给渲染器
c++·学习·游戏引擎
AndrewHZ37 分钟前
【图像处理基石】什么是油画感?
图像处理·人工智能·算法·图像压缩·视频处理·超分辨率·去噪算法
.格子衫.38 分钟前
015枚举之滑动窗口——算法备赛
数据结构·算法
J先生x2 小时前
【IP101】图像处理进阶:从直方图均衡化到伽马变换,全面掌握图像增强技术
图像处理·人工智能·学习·算法·计算机视觉
爱coding的橙子4 小时前
每日算法刷题 Day3 5.11:leetcode数组2道题,用时1h(有点慢)
算法·leetcode
虾球xz6 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
fpcc6 小时前
跟我学c++高级篇——模板元编程之十三处理逻辑
c++
格林威7 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
Dream it possible!8 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合