![](https://img-blog.csdnimg.cn/20190918140053667.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
C++基础语法——讲义+例题+习题 | 学习打卡
文章平均质量分 51
以例题为辅,习题为主,配合讲义深入了解C++基础语法。
永夜天
小呆呆
展开
-
《STL容器、位运算与常用库函数——例题 | CSDN创作打卡》
67. 数字在排序数组中出现的次数https://www.acwing.com/problem/content/63/class Solution {public: int getNumberOfK(vector<int>& nums , int k) { int ans = 0; for(int i = 0; i < nums.size(); i++){ if(nums[i] == k) ans++;原创 2022-02-09 12:11:30 · 758 阅读 · 0 评论 -
《第九讲 位运算与常用库函数 | CSDN创作打卡 》
C++帮我们实现好了很多有用的函数,我们要避免重复造轮子。位运算& 与| 或~ 非^ 异或>> 右移<< 左移常用操作:求x的第k位数字 x >> k & 1 lowbit(x) = x & -x,返回x的最后一位1常用库函数、reverse 翻转翻转一个vector:reverse(a.begin(), a.end());翻转一个数组,元素存放在下标1~n:reverse(a ..原创 2022-01-31 00:30:00 · 87 阅读 · 0 评论 -
第八讲 STL容器
STL是提高C++编写效率的一个利器。#include <vector>vector是变长数组,支持随机访问,不支持在任意位置O(1)插入。为了保证效率,元素的增删一般应该在末尾进行。 声明 #include <vector> 头文件 vector<int> a; 相当于一个长度动态变化的int数组 vector<...原创 2022-01-31 00:15:00 · 94 阅读 · 0 评论 -
结构体、类、指针、引用——习题
78. 左旋转字符串https://www.acwing.com/problem/content/74/class Solution {public: string leftRotateString(string str, int n) { string res; if(n != str.size()){ for(int i = n; i < str.size(); i++) res += st原创 2022-01-27 00:15:00 · 85 阅读 · 0 评论 -
结构体、类、指针、引用——例题
21. 斐波那契数列https://www.acwing.com/problem/content/19/class Solution {public: int Fibonacci(int n) { if(n <= 1) return n; else return Fibonacci(n - 1) + Fibonacci(n - 2); }};16. 替换空格https://www.acwing.com/problem/content原创 2022-01-26 00:30:00 · 95 阅读 · 0 评论 -
第七章 类、结构体、指针、引用
类与结构体类的定义: 类中的变量和函数被统一称为类的成员变量。private后面的内容是私有成员变量,在类的外部不能访问;public后面的内容是公有成员变量,在类的外部可以访问。 类的使用:#include <iostream>using namespace std;const int N = 1000010;class Person{ private: int age, height;...原创 2022-01-26 00:15:00 · 124 阅读 · 0 评论 -
C++中的函数——习题2
816. 数组翻转https://www.acwing.com/problem/content/818/#include <bits/stdc++.h>using namespace std;void reverse(int a[], int size,int n){ for(int i = size; i >= 1; i--) cout << a[i] << " "; for(int i = size+1; i <= n;原创 2022-01-25 00:30:00 · 211 阅读 · 0 评论 -
C++中的函数——习题1
810. 绝对值https://www.acwing.com/problem/content/812/#include <iostream>using namespace std;int main(){ int n; cin >> n; cout << abs(n) << endl; return 0;}806. 两个数的和https://www.acwing.com/problem/con原创 2022-01-25 00:15:00 · 412 阅读 · 0 评论 -
《C++中的常见函数——例题+代码实现 | CSDN创作打卡》
804. n的阶乘https://www.acwing.com/problem/content/806/#include <iostream>using namespace std;int fact(int n){ int a = 1; for (int i = 1; i <= n; i ++ ){ a *= i; } return a;}int main(){ int n; cin >>原创 2022-01-24 00:30:00 · 325 阅读 · 0 评论 -
《第六章 C++中的函数 | CSDN创作打卡》
函数基础一个典型的函数定义包括以下部分:返回类型、函数名字、由0个或多个形参组成的列表以及函数体。 编写函数 我们来编写一个求阶乘的程序。程序如下所示:int fact(int val){ int ret = 1; while (val > 1) ret *= val -- ; return ret;}函数名字是fact,它作用于一个整型参数,返回一个整型值。return语句负责结束fact并返回ret的值。...原创 2022-01-24 00:15:00 · 151 阅读 · 0 评论 -
字符串习题2
771. 字符串中最长的连续出现的字符(双指针)https://www.acwing.com/problem/content/773/#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ int n; cin >> n; while(n--) { string s;原创 2022-01-23 00:30:00 · 153 阅读 · 0 评论 -
字符串习题1
762. 字符串匹配https://www.acwing.com/problem/content/764/#include <bits/stdc++.h>using namespace std;int main(){ float a,b; string s1,s2; cin >> a >> s1 >> s2; int l = s1.size(),ans = 0; for (int i = 0; i &l原创 2022-01-22 00:30:00 · 203 阅读 · 0 评论 -
C++字符串——习题
760. 字符串长度https://www.acwing.com/problem/content/762/#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ char s[111]; cin.getline(s,111); cout << strlen(s) << endl;原创 2022-01-21 00:30:00 · 808 阅读 · 1 评论 -
字符串长度(常用的不忽略空格的4种字符串读入方式)
算法1C语言代码#include<string.h>#include<stdio.h>int main(){ char s[105]; gets(s); printf("%d",strlen(s)); return 0;}算法2C++ 代码(常用)#include <iostream>#include <cstring>using namespace std;int main(){ st...原创 2022-01-15 11:27:54 · 722 阅读 · 0 评论 -
第五章 C++中的字符串
字符与整数的联系——ASCII码每个常用字符都对应一个-128~127的数字,二者之间可以相互转化: 常用ASCII值:’A’-‘Z’ 是65~90,’a’-‘z’是97-122,’0’-‘9’是48-57。 字符可以参与运算,运算时会将其当做整数: 练习:输入一行字符,统计出其中数字字符的个数,以及字母字符的个数。字符数组字符串就是字符数组加上结束符’\0’。可以使用字符串来初始化字符数组,但此时要...原创 2022-01-21 00:30:00 · 461 阅读 · 0 评论 -
C++数组——习题2
752. 数组的右方区域https://www.acwing.com/problem/content/754/#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ char a; cin>>a; double c,s; int t=0; for(int i=0;i<12;i原创 2022-01-20 00:15:00 · 201 阅读 · 0 评论 -
C++中的数组——习题1
AcWing 740. 数组变换https://www.acwing.com/problem/content/742/#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ int a[100],b[100]; for(int i=0;i<20;i++){ cin>>a[i];原创 2022-01-19 00:30:00 · 252 阅读 · 0 评论 -
C++数组——例题
AcWing 737. 数组替换#include <iostream>#include <cstring>#include <algorithm>const int N = 10;int X[N];using namespace std;int main(){ for(int i = 0; i < N; i++){ int a; cin >> a; if(a <= 0)原创 2022-01-20 00:15:00 · 875 阅读 · 0 评论 -
第四章 C++中的数组
一维数组 数组的定义 数组的定义方式和变量类似。 数组的初始化 在main函数内部,未初始化的数组中的元素是随机的。 访问数组元素 通过下标访问数组。 练习题1: 使用数组实现求斐波那契数列的第N项。 练习题2:输入一个n,再输入n个整数。将这n个整数逆序输出。 练习题3:输入一个n,再输入n个整数。将这个数组顺时针旋转k(k <= n)次,最后将结果输出。 练习...原创 2022-01-19 00:45:00 · 549 阅读 · 0 评论 -
C++循环语句——习题2
AcWing 717. 简单斐波那契#include <iostream>#include <cstring>#include <algorithm>typedef long long ll;int main(){ int n; ll a = 0,b = 1,c; scanf("%d", &n); while (n -- ){ printf("%lld ",a); c = a + b.原创 2022-01-19 00:30:00 · 435 阅读 · 0 评论 -
循环语句——习题
AcWing 715. 余数#include <bits/stdc++.h>typedef long long ll;using namespace std;int main(){ int n; cin >> n; for(int i = 2; i < 10000; i++){ if(i % n == 2) printf("%d\n",i); } return 0;}AcWing 710. 六个奇原创 2022-01-18 00:30:00 · 226 阅读 · 0 评论 -
循环语句——例题
AcWing 708. 偶数#include <iostream>using namespace std;int main(){ for(int i = 1; i <= 100; i++) { if((i&1) == 0) //位运算 。偶数和1 &位元算为0 { cout<<i<<endl; } } return 0;原创 2022-01-18 00:15:00 · 320 阅读 · 0 评论 -
《第三章 C++中的循环结构 | CSDN创作打卡》
学习循环语句只需要抓住一点——代码执行顺序!while循环可以简单理解为循环版的if语句。If语句是判断一次,如果条件成立,则执行后面的语句;while是每次判断,如果成立,则执行循环体中的语句,否则停止。 练习:求1~100中所有数的立方和。 练习:求斐波那契数列的第n项。f(1)=1, f(2)=1, f(3)=2, f(n)=f(n-1) + f(n-2)。死循环:循环永久执行,无法结束。我们要避免写出死循...原创 2022-01-17 00:30:00 · 341 阅读 · 0 评论 -
《第二章 printf语句与C++中的判断结构|CSDN创作打卡》
一、printf输出格式注意:使用printf 时最好添加头文件 #include <cstdio>。Int、float、double、char等类型的输出格式:Int:%d Float: %f, 默认保留6位小数 Double: %lf, 默认保留6位小数 Char: %c, 回车也是一个字符,用’\n’表示所有输出的变量均可包含在一个字符串中:练习:输入一个字符,用这个字符输出一个菱形:练习:输入一个整数,表示时间,单位是秒。输出一个字符.原创 2022-01-17 00:15:00 · 1410 阅读 · 0 评论 -
C++语法基础—— 判断语句习题_2——ACwing
AcWing 672. 税#include<iostream>using namespace std;int main(){ double num; cin >> num; double tex; if (0 <= num && num <= 2000) {cout << "Isento"; return 0;} else if (2000 < num && num &l...原创 2022-01-16 00:30:00 · 183 阅读 · 0 评论 -
C++语法基础—— 判断语句习题——ACwing
AcWing 657. 选择练习1#include <bits/stdc++.h>using namespace std;int main(){ int a, b, c, d; cin >> a >> b >> c >> d; if(b > c && d > a && c + d > a + b && c > 0 && b &...原创 2022-01-16 00:15:00 · 232 阅读 · 0 评论 -
C++语法基础—— 判断语句例题——ACwing
AcWing 665. 倍数#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ int a,b; cin >> a >> b; if(a % b == 0 || b % a == 0) printf("Sao Multiplos"); else printf("Nao sa...原创 2022-01-15 00:30:00 · 159 阅读 · 0 评论 -
C++语法基础——变量、输入输出、表达式与顺序语句习题——ACwing
AcWing 605. 简单乘积#include<iostream>using namespace std;int main(){ int a,b; cin >> a >> b; printf("PROD = %d",a*b); return 0;}AcWing 611. 简单计算 #include<iostream>using namespace std;int main(){...原创 2022-01-15 00:15:00 · 147 阅读 · 0 评论 -
C++语法基础——变量、输入输出、表达式与顺序语句例题——Acwing
1. A + B#include<iostream>using namespace std;int main(){ int a, b; cin >> a >> b; cout << a + b; return 0;}AcWing 608. 差 #include<iostream>using namespace std;int main(){ int a,b,c,d; ...原创 2022-01-14 02:00:00 · 211 阅读 · 0 评论 -
第一章C++入门及简单的顺序结构
编写一个简单的C++程序——手速练习语法基础变量的定义变量必须先定义,才可以使用。不能重名。变量定义的方式:常用变量类型及范围:输入输出整数的输入输出:字符串的输入输出:输入输出多个不同类型的变量:表达式整数的加减乘除四则运算:浮点数(小数)的运算:整型变量的自增、自减:变量的类型转换:顺序语句输出第二个整数:计算 (a + b) * c的值原创 2022-01-13 21:33:54 · 168 阅读 · 0 评论