Day 1 (2022.4.16)
bool false/true 1byte
char 'a','\n' 1byte
int -2^31 ~ 2^31-1 4byte
float 1.23, 1.24e2 4byte (6-7位有效数字)
double 1.23, 1.24e2 8byte (15-16位有效数字)
long long -2^63 ~ 2^63-1 8byte
long double 16byte (18-19位有效数字)
/ ************************************************* /
int: %d
float: %f
double: %lf
char: %c
long long: %lld
/ ************************************************* /
C++取模的符号适合前面的数有关 5%2==1, -5%2==-1
/ ************************************************* /
// cin cout 在头文件iostream
// printf scanf 在头文件cstdio
#include
using namespace std;
int main(){
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
// 只用scanf可以不用std命名空间
#include
int main(){
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
printf("DIFERENCA = %d\n", a*b - c*d);
return 0;
}
Day 2 (2022.4.17)
// string在iostream库有,且只能用cin读
// abs也在iostream库
#include
using namespacestd;
int main(){
double basic, sale;
string name;
cin >> name;
scanf("%lf%lf", &sale, &basic);
printf("TOTAL = R& %.2lf\n", basic + 0.15*sale);
return 0;
}
#include
using namespace std;
int main(){
double money;
cin >> money;
int t = money*100;
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100.00\n", t/10000); t %= 10000;
printf("%d nota(s) de R$ 50.00\n", t/5000); t %= 5000;
printf("%d nota(s) de R$ 20.00\n", t/2000); t %= 2000;
printf("%d nota(s) de R$ 10.00\n", t/1000); t %= 1000;
printf("%d nota(s) de R$ 5.00\n", t/500); t %= 500;
printf("%d nota(s) de R$ 2.00\n", t/200); t %= 200;
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1.00\n", t/100); t %= 100;
printf("%d moeda(s) de R$ 0.50\n", t/50); t %= 50;
printf("%d moeda(s) de R$ 0.25\n", t/25); t %= 25;
printf("%d moeda(s) de R$ 0.10\n", t/10); t %= 10;
printf("%d moeda(s) de R$ 0.05\n", t/5); t %= 5;
printf("%d moeda(s) de R$ 0.01\n", t/1);
return 0;
}
Day 3 (2022.4.18)
// min和max在algorithm,pow在cmath
#include
using namespace std;
int main()
{
double a, b, c;
cin >> a >> b >> c;
if (a < b) swap(a, b);
if (a < c) swap(a, c);
if (b < c) swap(b, c);
if (a >= b + c)
cout << "NAO FORMA TRIANGULO" << endl;
else
{
if (a * a == b * b + c * c)
cout << "TRIANGULO RETANGULO" << endl;
if (a * a > b * b + c * c)
cout << "TRIANGULO OBTUSANGULO" << endl;
if (a * a < b * b + c * c)
cout << "TRIANGULO ACUTANGULO" << endl;
if (a == b && a == c)
cout << "TRIANGULO EQUILATERO" << endl;
else if (a == b || a == c || b == c)
cout << "TRIANGULO ISOSCELES" << endl;
}
return 0;
}
Day 4 (2022.4.19)
// 大多数头文件都可以用替代
// 字符串就是字符数组加上结束符'\0'
// 可以使用字符串来初始化字符数组,但由于每个字符串结尾都会含一个'\0'字符,
// 因此字符数组的长度至少要比字符串的长度多 1
char a1 = {'C', '+', '+', '\0'};
char a2[4] = "C++";
// C串的读入和输出
char s[100];
scanf("%s", s) //以空格分开
fgets(s, 100, stdin); //读入一行
puts(s); //自动换行
printf("%s\n", s);
// string的读入和输出
string s;
getline(cin, s);
std::cout << s;
// C串的函数
strlen(a); //求a的长度
strcpy(a, b); //将b复制给a
strcmp(a, b); //ab返回1
//当把string对象和字符字面值及字符串字面值混在一条语句,必须确保每个加法运算符的两侧的运算对象至少有一个是string
string s1 = "12";
string s2 = '3' + "45" + s1; //错误,不能够将字面值直接相加
// cin >> x 是会返回值的,如果读到文件末尾返回EOF
// 逗号表达式的值等于最后一个值
#include
using namespace std;
int main(){
int x;
while (cin >> x, x){
for (int i = 1; i <= x; i++) cout << i << ' ';
cout << endl;
}
return 0;
}
// 一直读可以用代码
while (cin >> x)
while (scanf("%d",&x)!=-1)
// scanf在读入字符时,不会自动过滤空格、回车和Tab
// 当输入为 a = 1, b = 2, 可以用scanf读入
scanf("a = %d b = %d", &a, &b)
#include
int main(){
int arr[10];
for (int i=0; i<10; i++) scanf("%d", &arr[i]);
for (int i=0; i<10; i++){
if (arr[i]<=0) arr[i] = 1;
}
for (int i=0; i<10; i++) printf("X[%d] = %d\n", i, arr[i]);
return 0;
}
Day 5 (2022.4.25)
// acwing 756. 蛇形矩阵
#include
using namespace std;
const int N = 110;
int q[N][N];
int n, m;
int main(){
cin >> n >> m;
int dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0};
int x = 0, y = 0, d = 0;
for (int i=1; i<=n*m; i++){
q[x][y] = i;
int a = x + dx[d], b = y + dy[d];
if (a>=n || a<0 || b>=m || b<0 || q[a][b]){
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
for (int i=0; i
Day 6 (2022.4.26)
// AcWing 760. 字符串长度
#include
using namespace std;
int main(){
string str;
getline(cin, str);
cout << str.size() << endl;
return 0;
}
// AcWing 760. 字符串长度
#include
int main(){
char str[101];
// fgets函数会把回车也读进来
fgets(str, 101, stdin);
int len = 0;
for (int i = 0; str[i] && str[i] != '\n'; i++) len++;
printf("%d", len);
return 0;
}
AcWing 763. 循环相克令
// AcWing 765. 字符串加空格
// string 默认初始化为""
#include
using namespace std;
int main()
{
string a, b;
getline(cin, a);
for (char c : a) b = b + c + ' ';
// 删掉最后一个字符
b.pop_back();
cout << b;
return 0;
}
// AcWing 769. 替换字符
#include
#include
using namespace std;
int main(){
char str[31], c;
scanf("%s", str);
scanf("\n%c", &c);
for (int i=0; str[i]; i++)
{
if (str[i] == c) str[i] = '#';
}
puts(str);
return 0;
}
// AcWing 773. 字符串插入
#include
using namespace std;
int main()
{
string a, b;
while (cin >> a >> b)
{
int p = 0;
for (int i = 1; i < a.size(); i++)
{
if (a[i] > a[p]) p = i;
}
cout << a.substr(0, p + 1) + b + a.substr(p + 1) << endl;
}
return 0;
}
Day 7 (2022.4.27)
// AcWing 768. 忽略大小写比较字符串大小
#include
#include
int main()
{
char a[100], b[100];
fgets(a, 100, stdin);
fgets(b, 100, stdin);
if (a[strlen(a)-1] == '\n') a[strlen(a)-1] = 0;
if (b[strlen(b)-1] == '\n') b[strlen(b)-1] = 0;
for (int i = 0; a[i]; i++)
{
if (a[i] >= 'A' && a[i] <= 'Z') a[i] += 32;
}
for (int i = 0; b[i]; i++)
{
if (b[i] >= 'A' && b[i] <= 'Z') b[i] += 32;
}
int t = strcmp(a, b);
if (t == 0) puts("=");
else if (t < 0) puts("<");
else puts(">");
return 0;
}
#include
using namespace std;
int main()
{
string a, b;
getline(cin, a);
getline(cin, b);
for (auto &c : a) c = tolower(c);
for (auto &c : b) c = tolower(c);
if (a == b) puts("=");
else if (a > b) puts(">");
else puts("<");
return 0;
}
// AcWing 767. 信息加密
#include
using namespace std;
int main()
{
string s;
getline(cin, s);
for (auto &c : s)
if (c >= 'a' && c <= 'z') c = (c - 'a' + 1) % 26 + 'a';
else if (c >= 'A' && c <= 'Z') c = (c - 'A' + 1) % 26 + 'A';
cout << s << endl;
return 0;
}
Day 8 (2022.4.28)
#include
#include
using namespace std;
int main()
{
string s, a, b;
getline(cin, s);
cin >> a >> b;
stringstream ssin(s);
string str;
while(ssin >> str)
{
if (str == a) cout << b << " ";
else cout << str << " ";
}
return 0;
}
#include
using namespace std;
int main()
{
int n;
cin >> n;
while (n -- )
{
string str;
cin >> str;
int cnt = 0;
char c;
for (int i = 0; i < str.size(); i ++ )
{
int j = i;
while (j < str.size() && str[j] == str[i]) j ++ ;
if (j - i > cnt) cnt = j - i, c = str[i];
i = j - 1;
}
cout << c << ' ' << cnt << endl;
}
return 0;
}
Day 9 (2022.5.2)
// AcWing 774. 最长单词
#include
using namespace std;
int main()
{
string s, word;
while(cin >> s)
{
if (s.back() == '.') s.pop_back();
if (s.size() > word.size()) word = s;
}
cout << word << endl;
return 0;
}
// AcWing 775. 倒排单词
#include
using namespace std;
int main()
{
string s[100];
int n = 0;
while(cin >> s[n]) n++;
for (int i = n - 1; i>=0; i--)
{
cout << s[i] << ' ';
}
return 0;
}
// AcWing 776. 字符串移位包含问题
#include
#include
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
if (a.size() < b.size()) swap(a, b);
for (int i = 0, len = a.size(); i < len; i++)
{
a = a.substr(1) + a[0];
if (a.find(b) != string::npos)
{
cout << "true";
return 0;
}
}
cout << "false";
return 0;
}
Day 10 (2022.5.8)
// 函数重载:只和函数参数(个数,类型和顺序有关)
// 传参多维数组只能省略第一维个数
// AcWing 778. 字符串最大跨距
#include
using namespace std;
int main()
{
string s, a, b;
char c;
while (cin >> c, c != ',') s += c;
while (cin >> c, c != ',') a += c;
while (cin >> c) b += c;
int l, r;
l = s.find(a);
r = s.rfind(b);
if (l != -1 && r != -1 && l + a.size() - 1 < r) cout << r - l - a.size();
else cout << -1;
return 0;
}
// AcWing 779. 最长公共字符串后缀
#include
using namespace std;
int main()
{
int n;
while(cin >> n, n != 0)
{
int m_max = 0x3f3f3f3f;
string s;
cin >> s;
for (int i = 1; i < n; i++)
{
int t_max = 0;
string compare;
cin >> compare;
for (int len_s = s.size() - 1, len_com = compare.size() - 1; len_s >= 0 && len_com >= 0; len_s--, len_com--)
{
if (s[len_s] == compare[len_com]) t_max++;
else break;
}
m_max = min(m_max, t_max);
}
if (m_max) cout << s.substr(s.size() - m_max) << endl;
else cout << endl;
}
return 0;
}
Day 11 (2022.5.9)
// AcWing 809. 最小公倍数
#include
using namespace std;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
int main()
{
int a, b;
cin >> a >> b;
cout << lcm(a, b) << endl;
return 0;
}
// AcWing 818. 数组排序
#include
#include
using namespace std;
const int N = 1010;
int main()
{
int n, l, r, a[N];
cin >> n >> l >> r;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a + l, a + r);
for (int i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
// AcWing 823. 排列
#include
using namespace std;
int n;
const int N = 10;
void dfs(int u, int nums[], bool st[])
{
if (u == n)
{
for (int i = 0; i < n; i++) cout << nums[i] << " ";
cout << endl;
}
else
{
for (int i = 1; i <= n; i++)
{
if (!st[i])
{
nums[u] = i;
st[i] = true;
dfs(u + 1, nums, st);
st[i] = false;
}
}
}
}
int main()
{
cin >> n;
int nums[N];
// 这里 {0} 是一个0初始化器
bool st[N] = {0};
dfs(0, nums, st);
return 0;
}
Day 12 (2022.5.12)
// 结构体和类只有一点不同,结构体默认是public,类默认private
// 结构体初始化
struct Person
{
int age, height;
double money;
}
Person p = {18, 178, 100};
// 头节点一般说的是第一个节点的地址
#include
using namespace std;
struct Node
{
int val;
Node* next;
Node(int _val) : val(_val), next(NULL) {}
};
int main()
{
Node node = Node(1); // 定义一个节点
Node* p = &node; // 习惯用指针描述它,所以定义一个结构体指针
Node* p = new Node(1); // 直接生成一个结构体,返回结构体地址
}
// AcWing 35. 反转链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !head->next) return head;
ListNode* p = head, *q = p->next;
while(q)
{
ListNode* r = q->next;
q->next = p;
p = q, q = r;
}
head->next = NULL;
return p;
}
};
// AcWing 29. 删除链表中重复的节点
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
ListNode *fiction = new ListNode(-1);
fiction->next = head;
ListNode *p = fiction;
while(p->next)
{
ListNode *q = p->next;
while (q->next && q->next->val == p->next->val) q = q->next;
if (q == p->next) p = q;
else p->next = q->next;
}
return fiction->next;
}
};
Day 13 (2022.5.14)
// STL、位运算和常用库函数
- 第8讲 STL - AcWing
- 第9讲 位运算与常用库函数 - AcWing
// AcWing 75. 和为S的两个数字
class Solution {
public:
vector findNumbersWithSum(vector& nums, int target) {
unordered_set s;
for (int x : nums)
{
if (s.count(target - x)) return {x, target - x};
else s.insert(x);
}
}
};
// AcWing 51. 数字排列
class Solution {
public:
vector> permutation(vector& nums) {
sort(nums.begin(), nums.end());
vector> res;
do res.push_back(nums); while (next_permutation(nums.begin(), nums.end()));
return res;
}
};
// AcWing 862. 三元组排序
#include
#include
#include
using namespace std;
struct three_Pair{
int x;
double y;
string s;
bool operator< (const three_Pair &t) const
{
return x < t.x;
}
};
int main()
{
three_Pair p[10001];
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> p[i].x >> p[i].y >> p[i].s;
}
sort(p, p+n);
for (int i = 0; i < n; i++)
printf("%d %.2lf %s\n", p[i].x, p[i].y, p[i].s.c_str());
return 0;
}