C语言字符串压缩之ZSTD算法详解

目录
  • 前言
  • 一、zstd压缩与解压
  • 二、ZSTD压缩与解压性能探索
  • 三、zstd的高级用法
  • 四、总结

前言

最近项目上有大量的字符串数据需要存储到内存,并且需要储存至一定时间,于是自然而然的想到了使用字符串压缩算法对“源串”进行压缩存储。由此触发了对一些优秀压缩算法的调研。

字符串压缩,我们通常的需求有几个,一是高压缩率,二是压缩速率高,三是解压速率高。不过高压缩率与高压缩速率是鱼和熊掌的关系,不可皆得,优秀的算法一般也是采用压缩率与性能折中的方案。从压缩率、压缩速率、解压速率考虑,zstd与lz4有较好的压缩与解压性能,最终选取zstd与lz4进行调研。

zstd是facebook开源的提供高压缩比的快速压缩算法(参考https://github.com/facebook/zstd),很想了解一下它在压缩与解压方面的实际表现。

一、zstd压缩与解压

ZSTD_compress属于ZSTD的Simple API范畴,只有压缩级别可以设置。

ZSTD_compress函数原型如下:

size_t ZSTD_compress(void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel)

ZSTD_decompress函数原型如下:

size_t ZSTD_decompress( void* dst, size_t dstCapacity, const void* src, size_t compressedSize);  我们先来看看zstd的压缩与解压缩示例。

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <malloc.h>
#include <zstd.h>
#include <iostream>

using namespace std;

int main()
{
    // compress
    size_t com_space_size;
    size_t peppa_pig_text_size;

    char *com_ptr = NULL;
    char peppa_pig_buf[2048] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining. Can we go out to play?Daddy: Alright, run along you two.Narrator: Peppa loves jumping in muddy puddles.Peppa: I love muddy puddles.Mummy: Peppa. If you jumping in muddy puddles, you must wear your boots.Peppa: Sorry, Mummy.Narrator: George likes to jump in muddy puddles, too.Peppa: George. If you jump in muddy puddles, you must wear your boots.Narrator: Peppa likes to look after her little brother, George.Peppa: George, let's find some more pud dles.Narrator: Peppa and George are having a lot of fun. Peppa has found a lttle puddle. George hasfound a big puddle.Peppa: Look, George. There's a really big puddle.Narrator: George wants to jump into the big puddle first.Peppa: Stop, George. | must check if it's safe for you. Good. It is safe for you. Sorry, George. It'sonly mud.Narrator: Peppa and George love jumping in muddy puddles.Peppa: Come on, George. Let's go and show Daddy.Daddy: Goodness me.Peppa: Daddy. Daddy. Guess what we' ve been doing.Daddy: Let me think... Have you been wa tching television?Peppa: No. No. Daddy.Daddy: Have you just had a bath?Peppa: No. No.Daddy: | know. You've been jumping in muddy puddles.Peppa: Yes. Yes. Daddy. We've been jumping in muddy puddles.Daddy: Ho. Ho. And look at the mess you're in.Peppa: Oooh....Daddy: Oh, well, it's only mud. Let's clean up quickly before Mummy sees the mess.Peppa: Daddy, when we've cleaned up, will you and Mummy Come and play, too?Daddy: Yes, we can all play in the garden.Narrator: Peppa and George are wearing their boots. Mummy and Daddy are wearing their boots.Peppa loves jumping up and down in muddy puddles. Everyone loves jumping up and down inmuddy puddles.Mummy: Oh, Daddy pig, look at the mess you're in. .Peppa: It's only mud.";

    peppa_pig_text_size = strlen(peppa_pig_buf);
    com_space_size= ZSTD_compressBound(peppa_pig_text_size);
    com_ptr = (char *)malloc(com_space_size);
    if(NULL == com_ptr) {
        cout << "compress malloc failed" << endl;
        return -1;
    }

    size_t com_size;
    com_size = ZSTD_compress(com_ptr, com_space_size, peppa_pig_buf, peppa_pig_text_size, ZSTD_fast);
    cout << "peppa pig text size:" << peppa_pig_text_size << endl;
    cout << "compress text size:" << com_size << endl;
    cout << "compress ratio:" << (float)peppa_pig_text_size / (float)com_size << endl << endl;

    // decompress
    char* decom_ptr = NULL;
    unsigned long long decom_buf_size;
    decom_buf_size = ZSTD_getFrameContentSize(com_ptr, com_size);

    decom_ptr = (char *)malloc((size_t)decom_buf_size);
    if(NULL == decom_ptr) {
        cout << "decompress malloc failed" << endl;
        return -1;
    }

    size_t decom_size;
    decom_size = ZSTD_decompress(decom_ptr, decom_buf_size, com_ptr, com_size);
    cout << "decompress text size:" << decom_size << endl;

    if(strncmp(peppa_pig_buf, decom_ptr, peppa_pig_text_size)) {
        cout << "decompress text is not equal peppa pig text" << endl;
    }

    free(com_ptr);
    free(decom_ptr);
    return 0;
}

执行结果:

从结果可以发现,压缩之前的peppa pig文本长度为1827,压缩后的文本长度为759,压缩率为2.4,解压后的长度与压缩前相等。

另外,上文提到可以调整ZSTD_compress函数的压缩级别,zstd的默认级别为ZSTD_CLEVEL_DEFAULT = 3,最小值为0,最大值为ZSTD_MAX_CLEVEL = 22。另外也提供一些策略设置,例如 ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2。压缩级别越高,压缩率越高,但是压缩速率越低。

二、ZSTD压缩与解压性能探索

上面探索了zstd的基础压缩与解压方法,接下来再摸索一下zstd的压缩与解压缩性能。

测试方法是,使用ZSTD_compress连续压缩同一段文本并持续10秒,最后得到每一秒的平均压缩速率。测试压缩性能的代码示例如下:

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <malloc.h>
#include <zstd.h>
#include <iostream>

using namespace std;

int main()
{
    int cnt = 0;

    size_t com_size;
    size_t com_space_size;
    size_t peppa_pig_text_size;

    char *com_ptr = NULL;
    char peppa_pig_buf[2048] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining. Can we go out to play?Daddy: Alright, run along you two.Narrator: Peppa loves jumping in muddy puddles.Peppa: I love muddy puddles.Mummy: Peppa. If you jumping in muddy puddles, you must wear your boots.Peppa: Sorry, Mummy.Narrator: George likes to jump in muddy puddles, too.Peppa: George. If you jump in muddy puddles, you must wear your boots.Narrator: Peppa likes to look after her little brother, George.Peppa: George, let's find some more pud dles.Narrator: Peppa and George are having a lot of fun. Peppa has found a lttle puddle. George hasfound a big puddle.Peppa: Look, George. There's a really big puddle.Narrator: George wants to jump into the big puddle first.Peppa: Stop, George. | must check if it's safe for you. Good. It is safe for you. Sorry, George. It'sonly mud.Narrator: Peppa and George love jumping in muddy puddles.Peppa: Come on, George. Let's go and show Daddy.Daddy: Goodness me.Peppa: Daddy. Daddy. Guess what we' ve been doing.Daddy: Let me think... Have you been wa tching television?Peppa: No. No. Daddy.Daddy: Have you just had a bath?Peppa: No. No.Daddy: | know. You've been jumping in muddy puddles.Peppa: Yes. Yes. Daddy. We've been jumping in muddy puddles.Daddy: Ho. Ho. And look at the mess you're in.Peppa: Oooh....Daddy: Oh, well, it's only mud. Let's clean up quickly before Mummy sees the mess.Peppa: Daddy, when we've cleaned up, will you and Mummy Come and play, too?Daddy: Yes, we can all play in the garden.Narrator: Peppa and George are wearing their boots. Mummy and Daddy are wearing their boots.Peppa loves jumping up and down in muddy puddles. Everyone loves jumping up and down inmuddy puddles.Mummy: Oh, Daddy pig, look at the mess you're in. .Peppa: It's only mud.";

    timeval st, et;

    peppa_pig_text_size = strlen(peppa_pig_buf);
    com_space_size= ZSTD_compressBound(peppa_pig_text_size);

    gettimeofday(&st, NULL);
    while(1) {

        com_ptr = (char *)malloc(com_space_size);
        com_size = ZSTD_compress(com_ptr, com_space_size, peppa_pig_buf, peppa_pig_text_size, ZSTD_fast);

        free(com_ptr);
        cnt++;

        gettimeofday(&et, NULL);
        if(et.tv_sec - st.tv_sec >= 10) {
            break;
        }
    }

    cout << "compress per second:" << cnt/10 << " times" << endl;
    return 0;
}

执行结果:

结果显示ZSTD的压缩性能大概在每秒6-7万次左右,这个结果其实并不是太理想。需要说明的是压缩性能与待压缩文本的长度、字符内容也是有关系的。

我们再来探索一下ZSTD的解压缩性能。与上面的测试方法类似,先对本文进行压缩,然后连续解压同一段被压缩过的数据并持续10秒,最后得到每一秒的平均解压速率。测试解压性能的代码示例如下:

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <malloc.h>
#include <zstd.h>
#include <iostream>

using namespace std;

int main()
{
    int cnt = 0;

    size_t com_size;
    size_t com_space_size;
    size_t peppa_pig_text_size;

    timeval st, et;

    char *com_ptr = NULL;
    char peppa_pig_buf[2048] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining. Can we go out to play?Daddy: Alright, run along you two.Narrator: Peppa loves jumping in muddy puddles.Peppa: I love muddy puddles.Mummy: Peppa. If you jumping in muddy puddles, you must wear your boots.Peppa: Sorry, Mummy.Narrator: George likes to jump in muddy puddles, too.Peppa: George. If you jump in muddy puddles, you must wear your boots.Narrator: Peppa likes to look after her little brother, George.Peppa: George, let's find some more pud dles.Narrator: Peppa and George are having a lot of fun. Peppa has found a lttle puddle. George hasfound a big puddle.Peppa: Look, George. There's a really big puddle.Narrator: George wants to jump into the big puddle first.Peppa: Stop, George. | must check if it's safe for you. Good. It is safe for you. Sorry, George. It'sonly mud.Narrator: Peppa and George love jumping in muddy puddles.Peppa: Come on, George. Let's go and show Daddy.Daddy: Goodness me.Peppa: Daddy. Daddy. Guess what we' ve been doing.Daddy: Let me think... Have you been wa tching television?Peppa: No. No. Daddy.Daddy: Have you just had a bath?Peppa: No. No.Daddy: | know. You've been jumping in muddy puddles.Peppa: Yes. Yes. Daddy. We've been jumping in muddy puddles.Daddy: Ho. Ho. And look at the mess you're in.Peppa: Oooh....Daddy: Oh, well, it's only mud. Let's clean up quickly before Mummy sees the mess.Peppa: Daddy, when we've cleaned up, will you and Mummy Come and play, too?Daddy: Yes, we can all play in the garden.Narrator: Peppa and George are wearing their boots. Mummy and Daddy are wearing their boots.Peppa loves jumping up and down in muddy puddles. Everyone loves jumping up and down inmuddy puddles.Mummy: Oh, Daddy pig, look at the mess you're in. .Peppa: It's only mud.";

    size_t decom_size;
    char* decom_ptr = NULL;
    unsigned long long decom_buf_size;

    peppa_pig_text_size = strlen(peppa_pig_buf);
    com_space_size= ZSTD_compressBound(peppa_pig_text_size);
    com_ptr = (char *)malloc(com_space_size);

    com_size = ZSTD_compress(com_ptr, com_space_size, peppa_pig_buf, peppa_pig_text_size, 1);

    gettimeofday(&st, NULL);
    decom_buf_size = ZSTD_getFrameContentSize(com_ptr, com_size);

    while(1) {

        decom_ptr = (char *)malloc((size_t)decom_buf_size);

        decom_size = ZSTD_decompress(decom_ptr, decom_buf_size, com_ptr, com_size);
        if(decom_size != peppa_pig_text_size) {

            cout << "decompress error" << endl;
            break;
        }

        free(decom_ptr);

        cnt++;
        gettimeofday(&et, NULL);
        if(et.tv_sec - st.tv_sec >= 10) {
            break;
        }
    }

    cout << "decompress per second:" << cnt/10 << " times" << endl;

    free(com_ptr);
    return 0;
}

执行结果:

结果显示ZSTD的解压缩性能大概在每秒12万次左右,解压性能比压缩性能高。

三、zstd的高级用法

zstd提供了一个名为PZSTD的压缩和解压工具。PZSTD(parallel zstd),并行压缩的zstd,是一个使用多线程对待压缩文本进行切片分段,且进行并行压缩的命令行工具。

其实高版本(v1.4.0及以上)的zstd也提供了指定多线程对文本进行并行压缩的相关API接口,也就是本小节要介绍的zstd高级API用法。下面我们再来探索一下zstd的多线程压缩使用方法。

多线程并行压缩的两个关键API,一个是参数设置API,另一个是压缩API。

参数设置API的原型是:

size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value)

压缩API的原型是:

size_t ZSTD_compress2(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)

下面给出zstd并行压缩的示例demo,通过ZSTD_CCtx_setParameter设置线程数为3,即指定宏ZSTD_c_nbWorkers为3,通过ZSTD_compress2压缩相关文本。另外,为了展示zstd确实使用了多线程,需要先读取一个非常大的文件,作为zstd的压缩文本源,尽量使zstd运行较长时间。

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <malloc.h>
#include <zstd.h>
#include <iostream>

using namespace std;

int main()
{
    size_t com_size;
    size_t com_space_size;

    FILE *fp = NULL;
    unsigned int file_len;

    char *com_ptr = NULL;
    char *file_text_ptr = NULL;

    fp = fopen("xxxxxx", "r");
    if(NULL == fp){
         cout << "file open failed" << endl;
         return -1;
    }

    fseek(fp, 0, SEEK_END);
    file_len = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    cout << "file length:" << file_len << endl;

    // malloc space for file content
    file_text_ptr = (char *)malloc(file_len);
    if(NULL == file_text_ptr) {
        cout << "malloc failed" << endl;
        return -1;
    }

    // malloc space for compress space
    com_space_size = ZSTD_compressBound(file_len);
    com_ptr = (char *)malloc(com_space_size);
    if(NULL == com_ptr) {
        cout << "malloc failed" << endl;
        return -1;
    }

    // read text from source file
    fread(file_text_ptr, 1, file_len, fp);
    fclose(fp);

    ZSTD_CCtx* cctx;
    cctx = ZSTD_createCCtx();

    // set multi-thread parameter
    ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 3);
    ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, ZSTD_btlazy2);

    com_size = ZSTD_compress2(cctx, com_ptr, com_space_size, file_text_ptr, file_len);

    free(com_ptr);
    free(file_text_ptr);
    return 0;
}

运行上述demo,可见zstd确实启动了3个线程对文本进行了并行压缩。且设置的线程数越多,压缩时间越短,这里就不详细展示了,读者可以自行实验。

需要说明的是,zstd当前默认编译单线程的库文件,要实现多线程的API调用,需要在make的时候指定编译参数ZSTD_MULTITHREAD。

另外,zstd还支持线程池的方式,线程池的函数原型:

POOL_ctx* ZSTD_createThreadPool(size_t numThreads)

线程池可以避免在多次、连续压缩场景时频繁的去创建线程、撤销线程产生的非必要开销,使得算力主要开销在文本压缩方面。

四、总结

本篇分享了zstd压缩与解压缩使用的基本方法,对压缩与解压的性能进行了摸底,最后探索了zstd多线程压缩的使用方法。

从压缩测试来看,zstd的压缩比其实已经比较好了,比原文所占用空间缩小了一半以上,当然压缩比也跟待压缩文本的内容有关。

从性能执行结果来看,zstd的压缩与解压性能表现比较勉强,我认为zstd在鱼(性能)和熊掌(压缩比)之间更偏向熊掌一些,不过对一些性能要求不太高的,但是要高压缩比的场景是比较符合的。

多线程并行压缩,在有大文本需要连续多次压缩的场景下,结合线程池可以很好的提升压缩速率。

以上就是C语言字符串压缩之ZSTD算法详解的详细内容,更多关于C语言字符串压缩的资料请关注我们其它相关文章!

(0)

相关推荐

  • C语言中压缩字符串的简单算法小结

    应用中,经常需要将字符串压缩成一个整数,即字符串散列.比如下面这些问题: (1)搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串的长度为1-255字节.请找出最热门的10个检索串. (2)有一个1G大小的一个文件,里面每一行是一个词,词的大小不超过16字节,内存限制大小是1M.返回频数最高的100个词. (3)有10个文件,每个文件1G,每个文件的每一行存放的都是用户的query,每个文件的query都可能重复.要求你按照query的频度排序. (4)给定a.b两个文件

  • C语言实现短字符串压缩的三种方法详解

    目录 前言 一.通用算法的短字符压缩 二.短字符串压缩 (1)Smaz (2)Shoco (3)Unisox2 三.总结 前言 上一篇探索了LZ4的压缩和解压性能,以及对LZ4和ZSTD的压缩.解压性能进行了横向对比.文末的最后也给了一个彩蛋:任意长度的字符串都可以被ZSTD.LZ4之类的压缩算压缩得很好吗? 本篇我们就来一探究竟. 一.通用算法的短字符压缩 开门见山,我们使用一段比较短的文本:Narrator: It is raining today. So, Peppa and George

  • C语言字符串原地压缩实现方法

    本文实例讲述了C语言字符串原地压缩的实现方法,对于学习字符串操作的算法设计有不错的借鉴价值.分享给大家供大家参考.具体方法如下: 字符串原地压缩示例: "eeeeeaaaff"压缩为"e5a3f2" 具体功能代码如下: /* * Copyright (c) 2011 alexingcool. All Rights Reserved. */ #include <iostream> #include <iterator> #include <

  • C语言字符串快速压缩算法代码

    通过键盘输入一串小写字母(a~z)组成的字符串. 请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串. 压缩规则: 1.仅压缩连续重复出现的字符.比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc". 2.压缩字段的格式为"字符重复的次数+字符".例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz". 示例 输入:"cccddec

  • C语言字符串压缩之ZSTD算法详解

    目录 前言 一.zstd压缩与解压 二.ZSTD压缩与解压性能探索 三.zstd的高级用法 四.总结 前言 最近项目上有大量的字符串数据需要存储到内存,并且需要储存至一定时间,于是自然而然的想到了使用字符串压缩算法对“源串”进行压缩存储.由此触发了对一些优秀压缩算法的调研. 字符串压缩,我们通常的需求有几个,一是高压缩率,二是压缩速率高,三是解压速率高.不过高压缩率与高压缩速率是鱼和熊掌的关系,不可皆得,优秀的算法一般也是采用压缩率与性能折中的方案.从压缩率.压缩速率.解压速率考虑,zstd与l

  • Java C++题解leetcode字符串轮转KMP算法详解

    目录 题目要求 思路一:双指针(模拟) Java C++ 思路二:子串 手写KMP Java dp C++ dp 调API Java C++ 总结 题目要求 思路一:双指针(模拟) Java class Solution { public boolean isFlipedString(String s1, String s2) { if (s1.length() != s2.length()) return false; int n = s1.length(); if (n == 0) retu

  • zlib库压缩和解压字符串STL string的实例详解

    zlib库压缩和解压字符串STL string的实例详解 场景 1.一般在使用文本json传输数据, 数据量特别大时,传输的过程就特别耗时, 因为带宽或者socket的缓存是有限制的, 数据量越大, 传输时间就越长. 网站一般使用gzip来压缩成二进制. 说明 1.zlib库可以实现gzip和zip方式的压缩, 这里只介绍zip方式的二进制压缩, 压缩比还是比较可观的, 一般写客户端程序已足够. 2.修改了一下zpipe.c的实现, 其实就是把读文件改为读字符串, 写文件改为写字符串即可. 例子

  • Java语言实现快速幂取模算法详解

    快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算过程中最麻烦的就是我们的5^1003这个过程 缺点1:在我们在之后计算指数的过程中,计算的数字不都拿得增大,非常的占用我们的计算资源(主要是时间,还有空间) 缺点2:我们计算的中间过程数字大的恐怖,我们现有的计算机是没有办法记录这么长的数据的,所以说我们必须要想一个更加高效的方法来解决这个问题 当我们计算AB%C的时候,最便捷的方法就是调用Ma

  • C语言中冒泡排序算法详解

    目录 一.算法描述 二.算法分析 三.完整代码 总结 一.算法描述 比较相邻两个元素,如果第一个比第二个大则交换两个值.遍历所有的元素,每一次都会将未排序序列中最大的元素放在后面.假设数组有 n 个元素,那么需要遍历 n - 1 次,因为剩下的一个元素一定是最小的,无需再遍历一次.因此需要两层循环,第一层是遍历次数,第二层是遍历未排序数组. 动图如下: 黄色部分表示已排好序的数组,蓝色部分表示未排序数组 核心代码如下: /** * @brief 冒泡排序 * * @param arr 待排序的数

  • C语言实现扫雷小游戏完整算法详解(附完整代码)

    目录 前言 1.算法基本思路 2.算法详解 1.初始化数组与打印数组 2.设置雷 3.排查与标记 4.CountMine函数计算周围雷的个数 5.ExpandMine函数递归展开周围所有安全区域 3.完整代码!!! 总结 前言 扫雷是一个常见小游戏,那么如何用C语言实现扫雷呢?学习了二维数组之后,我们可将扫雷的网格区域存储为二维数组,从而使用C语言实现扫雷. 1.算法基本思路 首先,用一个二维数组存储雷的分布,雷的分布在游戏期间从始至终不变,下文称为mine数组.用另一个二维数组存储排查出的雷的

  • C语言直接选择排序算法详解

    目录 1. 直接选择排序介绍 1.1 定义 1.2 基本原理 1.3 时间复杂度 1.4 空间复杂度 1.5 优缺点 2. 代码实现 2.1 代码设计 2.2 代码实现 1. 直接选择排序介绍 1.1 定义 直接选择排序是指每次都从剩余数据中选出最大或者最小的,将其排在已经排好的有序表后面. 1.2 基本原理 每次从无序表中选择最小(或最大)元素,将其作为首元素,知道所有元素排完为止.将一个有n个元素的数组从小到大排序,第一次从R[0] ~ R[n-1]中选取最小值,与R[0]交换,第二次从R[

  • Js面试算法详解

    素数 Q:你将如何验证一个素数? A:一个素数只能被它自己和1整除.所以,我将运行一个while循环并加1.(看代码示例,如果你无法理解,那这不是你的菜.先回去学习javaScript基础知识然后再回来吧.) 方法1 function isPrime(n){ var divisor = 2; while (n > divisor){ if(n % divisor == 0){ return false; } else divisor++; } return true; } isPrime(137

  • Kotlin 语言中调用 JavaScript 方法实例详解

    Kotlin 语言中调用 JavaScript 方法实例详解 Kotlin 已被设计为能够与 Java 平台轻松互操作.它将 Java 类视为 Kotlin 类,并且 Java 也将 Kotlin 类视为 Java 类.但是,JavaScript 是一种动态类型语言,这意味着它不会在编译期检查类型.你可以通过动态类型在 Kotlin 中自由地与 JavaScript 交流,但是如果你想要 Kotlin 类型系统的全部威力 ,你可以为 JavaScript 库创建 Kotlin 头文件. 内联 J

  • C语言实现“幸运数”的实例详解

    C语言实现"幸运数"的实例详解 1.题目: 标题:幸运数 幸运数是波兰数学家乌拉姆命名的.它采用与生成素数类似的"筛法"生成. 首先从1开始写出自然数1,2,3,4,5,6,-. 1 就是第一个幸运数. 我们从2这个数开始.把所有序号能被2整除的项删除,变为: 1 _ 3 _ 5 _ 7 _ 9 -. 把它们缩紧,重新记序,为: 1 3 5 7 9 -. .这时,3为第2个幸运数,然后把所有能被3整除的序号位置的数删去.注意,是序号位置,不是那个数本身能否被3整除!

随机推荐