C语言的实现的程序算法,算法:C语言实现(Algorithms In C)的学习

这是chapter 1的Introduce部分

原文:Readers of thisbook are expected to have at least a year's experience programming in high andlow-level languages. Also, some exposure to elementary algorithms on simpledata structures such as arrays, stacks, queues, and trees might be helpful,though this material is reviewed in some detail in Chapters 3 and 4. An elementaryacquaintance with machine organization, programming languages, and other basiccomputer science concepts is also assumed. (We’ll review such material brieflywhen appropriate, but always within the context of solving particularproblems.) A few of the application areas we deal with require knowledge ofelementary calculus. We’ll also be using some very basic material involvinglinear algebra, geometry, and discrete mathematics, but previous knowledge ofthese topics is not necessary.

理解:阅读这本书的读者最好是有一年以上的高级和低级语言的编程经验。懂得如,数组,堆,队列和树的这些基本的数据结构。要熟悉计算机结构,编程语言和其它一些基础的计算机概念。在某些地方会要到微积分,线性代数,几何学和离散数学的知识。

原文:In writing acomputer program, one is generally implementing a method of solving a problemthat has been devised previously. This method is often independent of theparticular computer to be used: it’s equally appropriate for many computers. Inany case, it is the method, not the computer program itself, which must bestudied to learn how the problem is being attacked.

The term algorithm is used in computer science to describe a problem-solvingmethod suitable for implementation as computer programs. Algorithms are the “stuff”of computer science: they are central objects of study in many, if not most,areas of the field.

理解:算法就是解决问题的方法,是计算机科学的原料

原文:Most algorithmsof interest involve complicated methods of organizing the data involved in thecomputation. Objects created in this way are calleddata structures, and they also are central objects of study incomputer science. Thus algorithms and data structures go hand in hand; in thisbook we take the view that data structures exist as the byproducts or end-productsof algorithms, and thus need to be studied in order to understand thealgorithms. Simple algorithms can use simple data structures. We will study theproperties of many data structures in this book; indeed, it might well havebeen called Algorithms and Data structures in this book; indeed, it might wellhave been called Algorithmsand Data Structures in C.

理解:数据结构指在利用算法解决问题的过程中的数据的组织方式,研究数据结构是为了更好的研究算法。这本书改名为《算法与数据结构的C语言实现》更好。

原文:When avery large computer program is to be developed, a great deal of effort must gointo understanding and defining the problem to be solved, managing itscomplexity, and decomposing it into smaller subtasks that can be easilyimplemented. It is often true that many of the algorithms required after thedecomposition are trivial to implement. However, in most cases there are a fewalgorithms whose choice is critical because most of the system resources willbe spent running those algorithms. In this book we will study a variety offundamental algorithms basic to large programs in many applications areas.

理解:对于复杂的问题,采取的解决方式通常是将问题细化成很多的小问题,这些小问题的算法往往更容易实现,所以本书讨论的是各种各样基本的算法。

原文:The sharing ofprograms in computer systems is becoming more widespread, so that while seriouscomputer user will use a large fraction of the algorithms in this book, theymay need to implement only a somewhat smaller fraction of them. However, implementingsimple versions of basic algorithms helps us to understand them better and thususe advanced versions more effectively. Also, mechanisms for sharing softwareon many computer systems often make it difficult to tailor standard programs toperform effectively on specific tasks, so that the opportunity to re-implementbasic algorithms frequently arises.

理解:还是在说明基本算法的重要性和实用性

原文:Computer programsare often over-optimized. It may not be worthwhile to take pains to ensure thatan implementation is the most efficient possible unless an algorithm is to beused for a very large task or is to be used many times. Otherwise, a careful,relatively simple implementation will suffice: one can have some confidence that it will work, and it is likely to run perhaps five or ten times slowerthan the best possible version, which means that it may run for an extra fewseconds. By contrast, the proper choice of algorithm in the first place can makea difference of a factor of a hundred or a thousand or more, which mighttranslate to minutes, hours, or even more in running time. In this book, weconcentrate on the simplest reasonable implementations of the best algorithms.

理解:大多数人可能这样觉得——程序不必过分优化,除非程序很繁琐或者使用的次数很频繁;一个一般的算法就能实现功能,只是比最好的算法慢5倍10倍而已。作者的建议一开始就用合适的算法,这样程序运行的时候效率就更好了,因此在这本里他用最简单地方式讲述最好的算法怎样合理的实现的。也就是说通俗的告诉你为什么这是最优算法。

原文:Often severaldifferent algorithms (or implementations) are available to solve the sameproblem. The choice of the very best algorithm for a particular task can be a very complicated process, often involving sophisticated mathematical analysis. Thebranch of computer science which studies such questions is called analysis of algorithms. Many of the algorithms that we will study have been shown throughanalysis to have very good performance, while others are simply known to workwell through experience. We will not dwell on comparative performance issues:our goal is to learn some reasonable algorithms for important tasks. But oneshould not use an algorithm without having some idea of what resources it mightconsume, so we will be aware of how our algorithms might be expected toperform.

理解:从多种可行性的算法中选出最优算法很伤脑筋,需要你精通数学。但是懂得算法分析让我们更好理解算法是怎么实现的。

总结:1. 有哪些基础之后,可以更好的学习这门课

2. 什么是算法和数据结构

3. 学习最优的基本算法,并要知道是怎么实现的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
算法:C语言实现(第1-4部分)基础知识、数据结构、排序及搜索(原书第3版)》细腻讲解计算机算法C语言实现。全书分为四部分,共16章。包括基本算法分析原理,基本数据结构、抽象数据结构、递归和树等数据结构知识,选择排序、插入排序、冒泡排序、希尔排序、快速排序方法、归并和归并排序方法、优先队列与堆排序方法、基数排序方法以及特殊用途的排序方法,并比较了各种排序方法的性能特征,在进一步讲解符号表、树等抽象数据类型的基础上,重点讨论散列方法、基数搜索以及外部搜索方法。书中提供了用C语言描述的完整算法程序,并且配有丰富的插图和练习,还包含大量简洁的实现将理论和实践成功地相结合,这些实现均可用在真实应用上。 《算法:C语言实现(第5部分)图算法(原书第3版)》是深入论述算法的三卷本教程《算法C语言实现》(第3版)中的第二卷——图算法。作者在这次修订中重写了许多内容,增加了数千个新练习、数百个新图表、数十个新程序,并对图表和程序做了详尽的注释说明。新版中不仅涵盖了新的主题,而且还提供了对许多经典算法的更充分的解释,包括图的性质、图搜索、有向图、最小生成树、最短路径和网。《算法:C语言实现(第5部分)图算法(原书第3版)》涵盖了足够的基本内容及较详细的图算法高级主题,既可单独用作数据结构与算法课程的教材,也可与第一卷(第1~4部分)结合使用。
C语言实现ECC加密算法需要使用一些数学库来完成椭圆曲线运算,比较复杂。这里提供一个基于OpenSSL库的示例代码,用于生成密钥对和加密解密数据: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/evp.h> #include <openssl/ec.h> #include <openssl/ecdsa.h> #include <openssl/bn.h> #include <openssl/rand.h> #define BUFSIZE 1024 int main() { int rc = 0; int curve_nid = NID_secp256k1; char *message = "Hello, world!"; size_t message_len = strlen(message); // 初始化OpenSSL库 OpenSSL_add_all_algorithms(); RAND_poll(); // 生成密钥对 EC_KEY *eckey = NULL; eckey = EC_KEY_new_by_curve_name(curve_nid); if (!eckey) { printf("Error: EC_KEY_new_by_curve_name\n"); return -1; } if (!EC_KEY_generate_key(eckey)) { printf("Error: EC_KEY_generate_key\n"); return -1; } // 显示公钥和私钥 EC_POINT *pub_key = NULL; pub_key = EC_KEY_get0_public_key(eckey); BIGNUM *priv_key = NULL; priv_key = EC_KEY_get0_private_key(eckey); char *priv_key_hex = NULL; priv_key_hex = BN_bn2hex(priv_key); printf("Private Key: %s\n", priv_key_hex); char *pub_key_hex = NULL; pub_key_hex = EC_POINT_point2hex(EC_KEY_get0_group(eckey), pub_key, POINT_CONVERSION_COMPRESSED, NULL); printf("Public Key: %s\n", pub_key_hex); // 加密解密数据 EVP_PKEY *privkey = EVP_PKEY_new(); if (!privkey) { printf("Error: EVP_PKEY_new\n"); return -1; } if (!EVP_PKEY_set1_EC_KEY(privkey, eckey)) { printf("Error: EVP_PKEY_set1_EC_KEY\n"); return -1; } EVP_PKEY *pubkey = EVP_PKEY_new(); if (!pubkey) { printf("Error: EVP_PKEY_new\n"); return -1; } if (!EVP_PKEY_set1_EC_KEY(pubkey, eckey)) { printf("Error: EVP_PKEY_set1_EC_KEY\n"); return -1; } EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); if (!ctx) { printf("Error: EVP_CIPHER_CTX_new\n"); return -1; } unsigned char iv[BUFSIZE] = {0}; unsigned char out[BUFSIZE] = {0}; int outlen; if (!EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, iv)) { printf("Error: EVP_EncryptInit_ex\n"); return -1; } if (!EVP_EncryptUpdate(ctx, out, &outlen, (unsigned char *)message, message_len)) { printf("Error: EVP_EncryptUpdate\n"); return -1; } if (!EVP_EncryptFinal_ex(ctx, out + outlen, &outlen)) { printf("Error: EVP_EncryptFinal_ex\n"); return -1; } unsigned char *encrypted_data = NULL; size_t encrypted_data_len = outlen + message_len; encrypted_data = (unsigned char *)malloc(encrypted_data_len); if (!encrypted_data) { printf("Error: malloc\n"); return -1; } memcpy(encrypted_data, out, outlen); memcpy(encrypted_data + outlen, message, message_len); unsigned char decrypted_data[BUFSIZE] = {0}; int decrypted_data_len = 0; if (!EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, iv)) { printf("Error: EVP_DecryptInit_ex\n"); return -1; } if (!EVP_DecryptUpdate(ctx, decrypted_data, &decrypted_data_len, encrypted_data, encrypted_data_len)) { printf("Error: EVP_DecryptUpdate\n"); return -1; } if (!EVP_DecryptFinal_ex(ctx, decrypted_data + decrypted_data_len, &decrypted_data_len)) { printf("Error: EVP_DecryptFinal_ex\n"); return -1; } // 显示加密和解密结果 printf("Plain Text: %s\n", message); printf("Cipher Text: "); for (int i = 0; i < encrypted_data_len; i++) { printf("%02x", encrypted_data[i]); } printf("\n"); printf("Decrypted Text: %s\n", decrypted_data); // 释放资源 free(encrypted_data); EC_KEY_free(eckey); EVP_PKEY_free(privkey); EVP_PKEY_free(pubkey); EVP_CIPHER_CTX_free(ctx); OPENSSL_cleanup(); return 0; } ``` 需要使用OpenSSL库来实现ECC加密算法,可以通过以下命令安装: ```bash sudo apt-get install libssl-dev ``` 编译以上代码示例: ```bash gcc -o ecc ecc.c -lssl -lcrypto ``` 注意:以上代码示例仅供参考,实际使用时需要根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值