摘要: 本题考查大数问题。大数一般用字符串或者数组表示。注意,strlen()函数返回的值是数组'\0'前元素的个数,并不包括'\0'。 C++版本 #include <iostream> #include <algorithm> #include <cstring> using namespace std 阅读全文
posted @ 2020-07-22 16:31 程序员曾奈斯 阅读(259) 评论(0) 推荐(0)
摘要: 引用字符串或者使用字符串函数 注意,在C++中,#include<string>与#include<ctring>和#include<string.h>是不一样的。 使用C中的字符串函数比如strlen(),需要引入的是#include<ctring>或者#include<string.h> 使用字 阅读全文
posted @ 2020-07-22 15:27 程序员曾奈斯 阅读(495) 评论(0) 推荐(0)
摘要: 本题考查大数问题。大数一般用字符串或者数组表示。注意,strlen()函数返回的值是数组'\0'前元素的个数,并不包括'\0'。 C++版本 #include <iostream> #include <algorithm> #include <cstring> using namespace std 阅读全文
posted @ 2020-07-19 17:14 程序员曾奈斯 阅读(138) 评论(0) 推荐(0)
摘要: 本题考查库函数的实现原理,特别注意用O(logn)时间求a的n次方的优化算法。 C++版 #include <iostream> #include <cmath> using namespace std; bool g_InvalidInput = false; double powerWithUn 阅读全文
posted @ 2020-07-18 20:28 程序员曾奈斯 阅读(144) 评论(0) 推荐(0)
摘要: 这个题目考察的是计算机基础知识。注意int型的-1在计算机中的二进制存储为补码0xFFFF FFFF,但是计算机在展示给我们的时候,是作为原码展示。 C++版 #include <iostream> using namespace std; int NumberOf1Plus(int n){ int 阅读全文
posted @ 2020-07-18 18:48 程序员曾奈斯 阅读(135) 评论(0) 推荐(0)
摘要: 一、计算机中的二进制位运算 二进制的位运算并不是很难掌握,因为位运算总共只有5种运算:与、或、异或、左移、右移。与、或和异或运算的规律我们可以用表1总结如下。 表1 与、或、异或的运算规律 与(&) 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1 或(|) 0 | 0 阅读全文
posted @ 2020-07-18 18:31 程序员曾奈斯 阅读(2182) 评论(0) 推荐(0)
摘要: 本题考察的是动态规划与贪心算法,有一个疑问,为何i<4时,products[i]=i。 C++版本 #include <iostream> #include <vector> using namespace std; // 动态规划 int maxProductAfterCutting_soluti 阅读全文
posted @ 2020-07-17 17:13 程序员曾奈斯 阅读(144) 评论(0) 推荐(0)
摘要: 本题考察的是回溯算法,可以使用DFS解决问题。 C++版本 #include <iostream> #include <vector> using namespace std; int getDigitSum(int num){ int sum = 0; while(num > 0){ sum += 阅读全文
posted @ 2020-07-17 10:40 程序员曾奈斯 阅读(142) 评论(0) 推荐(0)
摘要: 本题考察的是回溯算法,注意C/C++里面可以定义变量长度的数组,比如int a = 3;int b = 3;int c[a*b];。但是如果定义为bool *visited = new bool[rows*cols],就是类的类型,是不能用memset(visited, false, sizeof( 阅读全文
posted @ 2020-07-16 22:01 程序员曾奈斯 阅读(126) 评论(0) 推荐(0)
摘要: 本题考察的是查找,需要注意的是数组中有相同数字的特例,如果不能很好地处理这些特例,就很难写出让人满意的完美代码。 C++版本 #include <iostream> #include <vector> using namespace std; int minInOrder(vector<int> r 阅读全文
posted @ 2020-07-16 20:28 程序员曾奈斯 阅读(121) 评论(0) 推荐(0)