1.不能用递归:超时
2.long long类型
数据规模与约定:1 <= n <= 1,000,000,000。
long long整型:-9×10的18次方~9×10的18次方
int整型:-2×10的9次方~2×10的9次方
3.字符串可加减数字计算【自动转化ASCII】
int main(){
string str = "000";
str[1]+=2;
cout<<str;//020
}
4. p进制转十进制
例子:9进制转10进制【2022省赛填空】
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<iomanip>
using namespace std;
int main(){
int x = 2022;
int y = 0, pro = 1;
int p = 9;
while(x!=0){
y = y + (x % 10) * pro;
x /= 10;
pro = pro * p;
}
cout<<y;
return 0;
}
5. 前缀和
参考:https://blog.csdn.net/weixin_30647065/article/details/95440092
题目:求和
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdbool>
#include <string.h>
#include <math.h>
using namespace std;
int main()
{
int a[100005] = {0};
int b[100005] = {0};
int n;
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> a[i];
b[i] = b[i-1] + a[i];
}
int t;
cin >> t;
while (t--)
{
int l,r;
int sum = 0;
cin >> l >> r;
sum = b[r] - b[l-1];
printf("%d\n",sum);
}
return 0;
}
6. reverse()反转
reverse(vi.begin(),vi.end());
reverse(vi,vi+3);
7. vector
vector<int> vi;
vi.push_back(x);
8. 全局变量注意
注意:
采用全局变量时,尽量将所有变量都使用全局变量,一开始一些使用全局,num不使用,就没有通过。
如果都不使用全局变量也是通过的。
要保证变量形式保持一致。
为了代码格式标准,尽量全部使用全局变量,如果不确定,就都不使用。
知识点:
思想掌握,未访问置为0,已经访问置为1。
9. 注意值变化,使用中间变量
for(int i = m; i <= n; i++){
int j = i; //注意要有中间变量,因为i是要变的
while(j!=0){
int temp = j % 10;
a[temp]++;
j /= 10;
}
}
10. 标记0\1思想
11. toupper/tolower()
将字符串大小写转换!不改变数字
12. 字符与ASCII码转换
字符加位
char c;
cin>>c;
printf("%c",c+1);
//a--b
数字转字符
int c;
cin>>c;
printf("%c",c+1);
13. 26字母加位循环
s[i] + n - 26
14. getline
一行都可以读入,包括空格。
参考:P5015 [NOIP2018 普及组] 标题统计
15. map键值对
参考:【入门5 字符串】P1125 [NOIP2008 提高组] 笨小猴——map
map使用:
1、map按照键值对存储,并且按照键进行升序
16. 标记思想、字符串–数字转换
标记思想!
字符串–数字:stoi()
数字–字符串:to_string()
参考:【入门5 字符串】P1957 口算练习题