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;
}

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

题目三: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.

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

相关推荐
海兰5 分钟前
【实用程序】电商销售分析仪表盘 — 从零搭建一个AI参与的全栈数据洞察系统
人工智能·学习·算法
凡人叶枫21 分钟前
Effective C++ 条款28:避免使用 handles 指向对象内部
linux·服务器·开发语言·c++·嵌入式开发
zwenqiyu40 分钟前
P5283 [十二省联考 2019] 异或粽子题解
c++·学习·算法
wayz1140 分钟前
Momentum:TSI(真实强度指数)技术指标详解
算法·金融·数据分析·量化交易·特征工程
Queenie_Charlie40 分钟前
哈夫曼树
数据结构·c++·哈夫曼树
万事大吉CC1 小时前
Python 笔试输入模板总结
python·算法
lihao lihao1 小时前
Linux信号
开发语言·c++·算法
大白话_NOI2 小时前
【洛谷 P2249】查找(深基 13. 例 1)+ 详细分析
c++·算法
吠品2 小时前
C++实现m行n列带边框的长方形输出
算法
智者知已应修善业2 小时前
【51单片机2个外部中断显示中断历时,初始化8左移3位共阳数码管】2024-6-6
c++·经验分享·笔记·算法·51单片机