- 博客(11)
- 收藏
- 关注
原创 2的幂 leetcode C++
个人解题思路:class Solution {public: bool isPowerOfTwo(int n) { if (n == 1) return true; else if (n == 0) return false; else if (n%2 == 1) return false; else return isPowerOfTwo(n/2);
2021-05-30 14:10:10
128
原创 两数之和 C++ Leetcode
原解题思路:class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector <int> result; for(int i = 0; i < nums.size(); i++){ int num2 = target - nums[i]; for (int j = i+
2021-05-29 16:57:38
127
原创 C++ 动态分配的strcyp函数
#include <iostream>using namespace std;//count length of char arrayint strlen(char* str) { int i = 0; while (*str++ != '\0') { i++; } return i;}// required copy string functionchar* copyCString(char* str) { int size = strlen(str); cha
2021-04-04 01:25:39
210
原创 汉诺塔移动次数 递归 C++
#include <iostream>using namespace std;void moveonetower(char start, char finish, int& count) { cout << start << "->" << finish << endl; }void movetower(int n, char start, char finish, char tmp, int& count) {
2021-03-18 15:10:24
525
原创 c++递归实现字符串倒序
#include <iostream>#include <string>using namespace std;string reverse(string& str) { if (str.length() <= 1) return str; else { string sub = str.substr(1, str.length() - 2); return str[str.length() - 1] + reverse(sub) + str[0];
2021-03-14 16:45:52
683
原创 c++递归 求一个整数的各个数字之和
int digitsum(int n) { if (n < 10) return n; else return (n % 10) + digitsum(n / 10);}
2021-03-14 13:35:48
572
原创 C++最大公约数 欧几里得算法
#include <iostream>using namespace std;int gcd(int x, int y) { if (x % y == 0) return y; else { return gcd(y, x % y); }}
2021-03-14 13:30:42
326
原创 C++ vector 读取一组数并求平均值和标准差
#include <iostream>#include <vector>#include <string>using namespace std;double mean(vector <double>& data);double stddev(vector <double>& data);int main() { double v; vector <double> data; cout <&l
2021-03-10 14:32:56
1675
2
原创 C++程序设计 基础、编程抽象与算法策略 第三章习题
#include <iostream>#include <string>using namespace std;bool endWith(string str, string suffix) { int a = str.length(); int b = suffix.length(); if (a < b) return false; for (int i = 0; i < b; i++) { if (str[a - i - 1] != suf...
2021-02-27 18:01:22
249
原创 C++程序设计 基础、编程抽象与算法策略 第二章习题
#include <iostream>using namespace std;int roundToNearstInt(double x) { int y; if (x >= 0) { y = x + 0.5; } else { y = x - 0.5; } return y;}int main() { double x; cout << "Enter the number: "; cin >> x; int y = ro...
2021-02-23 13:05:13
214
原创 C++程序设计 基础、编程抽象与算法策略 第一章习题
C++程序设计 基础、编程抽象与算法策略 第一章习题5.#include <iostream>using namespace std;const int SENTINEL = 0;int main(){ cout << "This program fins the largest interger in a list." << endl; cout << "Use " << SENTINEL << " to
2021-02-13 18:09:12
443
1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人