1:保留1位小数
cout<<setiosflags(ios::fixed)<<setprecision(2)<<sum<<endl;
printf("%.3f",v2[N-M]);
2:指向一个int型数组 用一个int[]数组 迭代
3:#include <stdio.h>
输入输出 printf("%02d:%02d:%02d\n", hour, minutes, second);
scanf("%d%d%d",&a,&b,&c);
scanf("%d%d", &node[a].key, &node[a].next);
printf("a=%d,b=%d,c=%d",a,b,c);
4: printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
Preceding with blanks: 1977
Preceding with zeros: 0000001977
5: pair<int,double> node[N+1];
for(int i=1;i<N+1;i++){
a[i]=a[i]/100;
node[i] = make_pair(i,a[i]);
}
bool cmp(pair<int,double> a,pair<int,double> b)
{
if(a.second==b.second){
return (a.first<b.first);
}else {
return a.second>b.second;
}
}
6:replace(str.begin(),str.end(),'/','0');
7:指向set集合 遍历的时候 用迭代器 因为不是数组
cout<<*iter;
for(iter++;iter!=v2.end();iter++){
cout<<" "<<*iter;
}
set<int> v2;
set<int>::iterator iter;
8: 行尾不允许有多余空格
cnt = 0;
if(level[i] != -1 && cnt != N - 1) {
printf("%d ", level[i]);
cnt++;
} else if(level[i] != -1){
printf("%d", level[i]);
break;
}
9:使用sscanf和sprintf函数。
sscanf() – 从一个字符串中读进与指定格式相符的数据
sprintf() – 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。
sscanf(a, "%lf", &temp); lf?
sprintf(b, "%.2lf",temp);
10:求字符数组的长度 char a[50]
strlen(a)
11: 输入的情况
while(scanf("%d",&n)!=EOF)
12:c[t++]
13: 搜搜这个是干嘛的
sscanf(a,"%lf",&temp);
sprintf(b,"%.2lf",temp);
可以直接把字符串转成double类型的数字?
13:printf 一个字符串
14:怎么能让控制台定住呢
cin.ignore();
system("PAUSE");
加一个
System("Pause");
或者
getchar();
或者
cin>>变量名
15:整形的取值范围
char -128 ~ +127 (1 Byte)
short -32767 ~ + 32768 (2 Bytes)
unsigned short 0 ~ 65536 (2 Bytes)
int -2147483648 ~ +2147483647 (4 Bytes)
unsigned int 0 ~ 4294967295 (4 Bytes)
long == int
long long -9223372036854775808 ~ +9223372036854775807 (8 Bytes)
double 1.7 * 10^308 (8 Bytes)
16:while(scanf("%d", &n) != EOF) {
if(n < 0) break;
scanf("%d", &d);
if(isprime(n) == false) {
printf("No\n");
continue;
}
17:
bool isPrime(int num)
{
if(num<=1){
return false;
}
for(int i=2;i<=sqrt(num);i++){
if(num%i==0){
return false;
}
}
return true;
}
p=m*n;//用来求最小公倍数
while(m!=0)
{
r=n%m;
n=m;
m=r;
}
cout<<"最小公倍数:"<<p/n<<endl;
18:scanf("%d/%d/%d",&year, &month, &day);
19:swap(a,c);
20:
a3 = (BSum-ASum) / (29*17);
b3 = (BSum-ASum) % (29*17) /29;
c3 = (BSum-ASum) % (29*17) %29;
21:
if (it != s.begin())
printf(" ");
printf("%05d", *it);
22:
vector<int> v(n);
for(int i = 0; i < n; ++i){
scanf("%d", &v[i]);
}
23:reverse(strB.begin(),strB.end()); 字符串逆转
24: 字符串相加 是有区别的!+= 和 “J”+str
25: 查一下动态规划等基本的算法
26:建堆的过程/不断调整堆
27:并查集
28:
Node* build_tree(int back[],int in[],int length)
{
if(length==0){
return NULL;
}
Node* temp = new Node;;
int pos = 0;
while(in[pos]!= back[length-1]){
pos++;
}
temp->data = in[pos]; ////建树的具体代码
temp->lchlid = build_tree(back,in,pos);
temp->rchild = build_tree(back+pos,in+pos+1,length-pos-1);
return temp;
}
29:
考虑到进位,乘以2以后可能会多出一位,而且20位的数字要用string表示而不是用long long表示。
可以看到以下数字的表示范围中long long不够20位。
int ,long , long long类型的范围
unsigned int 0~4294967295
int 2147483648~2147483647
unsigned long 0~4294967295
long 2147483648~2147483647
long long的最大值:9223372036854775807 (刚好19位)
29:std::string::npos
static const size_t npos = -1;
30:longlong溢出的情况
31:堆 并查集 递归分治 贪心 动态规划 回溯搜索
32:string.h strlen 求字符数组的实际长度
char a[22];
scanf("%s",a);
int flag = 0;
int len = strlen(a);
33:
void *memset(void *s, int ch, size_t n);
函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。
memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法
34:
vector<int>::iterator iter = find(v1.begin(),v1.end(),1);
cout<<*iter;
35:贪心算法 一直往前 寻找最优解
36:<%=request.getContextPath()%>和${pageContext.request.contextPath}获取的结果都是项目名(上下文)
37:分数的最小公倍数:分子=分子的最小公倍数、分母=分母的最大公约数。
注意:求分数的最小公倍数时,这两个分数一定要化简为最简形式,不然会出错。
38:
int a[1100000];
memset(a,0,sizeof(a));//初始化,将其全部标记为0
需要包含 #include <string.h>
39:超时的情况 ~~~~ while(~scanf("%d %d",&a,&b)){
40:
code blocks使用
Ctrl+Shift+C:注释掉当前行或选中块
Ctrl+Shift+X:解除注释
Tab:缩进当前行或选中块
Shift+Tab:减少缩进按住
Ctrl,滚动鼠标滚轮,放大或缩小字体编译与运行部分
Ctrl + F9:编译
Ctrl + F10:运行上次成功编译后的程序
Ctrl + Shift + F9:编译当前文件(而不是当前打开的工程项目)
F9:编译并运行当前代码(如果编译错误会提示错误而不会运行)
F8:debug
F10:全屏
Ctrl + C:终止正在运行的程序
Ctrl + Z:终止输入界面部分
Shift + F2:显示或隐藏左侧导航栏
41: freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
42:Standard C String
atof:char* 转double
atoi:char* 转int
atol:char* 转long
以上函数使用需要包含头文件<cstdlib>,即#include <cstdlib>。 如果要将string转为double,int或long,只需要先用c_str()将它转为char*, 然后使用上面的函数做转换即可。
double d = atof("42.3");
int i = atoi("423");
long l = atol("2313455");
string s = "23.45";
d = atof(s.c_str());
isalnum:如果字符是一个字母或者一个数字,返回非0值;否则返回0
isalpha:如果字符是一个字母,返回非0值;否则返回0
isdigit:如果字符是0-9的一个数,返回非0值;否则返回0
以上函数的使用需要包含头文件<cctype>,即#include <cctype>。
cout<<isalnum('A'); //返回非0
cout<<isalpha('1'); //返回0
cout<<isdigit('1'); //返回非0
43:strB = string(strA.size()-strB.size(),'0')+strB; //使两个字符串的长度相同 便于以后相加 注意加的左右不能反 下同
44:
/*斯特林[striling]公式(求阶乘(n!)的位数)
-10-05 13:49
例如1000阶乘位数:
log10(1)+log10(2)+···+log10(1000)取整后加1
*/
45:
map<int,int> mapp;
for(int i = 0; i < n; i++){
int temp;
cin >> temp;
mapp[temp]++ ;
}
int max = 0, ans = 0;
for(map<int,int>::iterator iter = mapp.begin(); iter != mapp.end(); iter++){
if(iter -> second > max){
max = iter -> second;
ans = iter -> first;
}
}
46:Ctrl+Shift+C 注释代码块
Ctrl+Shift+X 取消注释
47:两个字符字符数组的比较
strcmp
int strcmp ( const char * str1, const char * str2 );
Compare two strings
Compares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
重载小于运算符
bool operator < (const Student &b){
if(b.grade != grade){
return grade < b.grade;
}else if(b.name != name){
return name < b.name;
}else {
return age < b.age;
}
}
48:宏定义(相当于一个函数)#define ISYEAR(x) ((x % 100 != 0 && x % 4 == 0) || x % 400 == 0) ? 1 : 0
49:while(scanf("%4d %2d %2d",&y1, &m1, &d1) != EOF){
scanf("%4d %2d %2d",&y2, &m2, &d2);
printf("%d\n",abs(buf[y2][m2][d2]-buf[y1][m1][d1]) + 1);
}
50:哈希表!
int Hash[101] = {0};
for(int i = 0; i < n; i++){
int temp;
cin >> temp;
Hash[temp] ++;
}
51: string::npos find函数的使用
52: mapp进行key value排序
bool cmp(pair<int,int> a,pair<int,int> b)
{
if(a.second == b.second){
return a.first < b.first;
}
return a.second > b.second;
}
int main()
{
map<int,int> mapp;
int n;
cin >> n;
for(int i = 0; i < n; i++){
int t;
cin >> t;
mapp[t]++;
}
vector<pair<int,int> > vec (mapp.begin(), mapp.end());
sort(vec.begin(),vec.end(),cmp);
cout << vec[0].first;
return 0;
}
53: 判断的时候从后往前判断 不然第一个就判断大的值 然后如果是一个非常小的值呢?
54:vector<string> v1;
string str;
//getchar();
while(cin >> str){
if(str == ".")break;
v1.push_back(str);
}
55: 输入一个double类型的数值
while(scanf("%lf %d",&n, &m) != EOF){
56: for(map<int,int>::iterator iter = mapp.begin(); iter != mapp.end(); iter++){
if(iter->second % 2 != 0){
cout << iter->first;
break;
}
}
57://遍历a中每一个字符,直到a字符串结尾
char a[11];
char b[11];
while(~scanf("%s%s", a, b)){ //利用字符串将两个数组读入,作为字符串保存在内存中
int ans = 0;
for(int i = 0; a[i] != 0; i++){ //遍历a中每一个字符,直到a字符串结尾
for(int j = 0; b[j] != 0; j++){
ans += (a[i] - '0') * (b[j] - '0');
}
}
printf("%d", ans);
}
58:scanf("%lld %lld",&a, &b); 输入一个long long 类型的值
59:
//筛素数法
bool mark[100001];
int prime[100001];
int primeSize;
void init()
{
primeSize = 0;
for(int i = 2; i <= 100000; i++){
if(mark[i]){
continue;
}
mark[i] = true;
prime[primeSize++] = i;
for(int j = i * i; j <= 100000; j += i){
mark[j] = true;
}
}
}//用素数筛法筛选出2到100000以内的所有素数
【持续更新】刷题时遇到的一些问题
最新推荐文章于 2024-09-14 13:46:46 发布