#include<iostream> #include<cstring> usingnamespace std; intmain(){ //a[10]的长度其实是40byte,因为每个int是4个byte int a[10], b[10]; memset(a, 0, 40);//cstring中的memset(要初始化的数组,初始化的值,要初始化的字节长度(byte)) memset(b, 0, sizeof b);//sizeof可以用来求数组占用的字节数量 for (int i = 0; i < 10; i++) { cout << a[i] << ' '; } cout << endl; for (int i = 0; i < 10; i++) { cout << b[i] << ' '; } cout << endl; return0; }
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream> #include<cstring> usingnamespace std; intmain(){ char data[]="This is a test of the memset function"; printf("Before:%s\n",data); memset(data,'*',4); printf("After:%s\n",data); return0; } 输出: Before:This is a test of the memset function After:**** is a test of the memset function
atoi/stoi 字符串转换为整数
1 2 3 4 5 6 7
#include<iostream> usingnamespace std; intmain(){ int a = atoi("123"); cout << a << endl; return0; }
1 2 3 4 5 6 7
#include<bits/stdc++.h> usingnamespace std; intmain(){ string s = "123"; int n = stoi(s); cout << n << endl; }
#include<bits/stdc++.h> usingnamespace std; int a[1005]; intmain() { int n; while (~scanf("%d", &n)) { for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } printf("\n"); sort(a, a + n); do { for (int i = 0; i < n; i++) { printf("%d ", a[i]); } printf("\n"); } while (next_permutation(a, a + n)); } return0; }
关于数据范围:大题的题目中会给出相应的数据范围,根据题目的数据范围来选择数据型是int 还是long long 还是其他类型,注意如果是使用long long的话,要用%i64d(i大写)进行输入输出,如果是平常做题过程中使用%lld进行输入输出。(用%i64d(i大写)还是%lld根据情况而定,如果Windows评测机采用%i64d(i大写);如果Linux评测机采用%lld)
1 2 3 4
蓝桥杯输入/出 longlong数据需要使用的是 longlong x = 0; scanf("%I64d", &x); printf("%I64d", x);
1. // 当需要多次使用一个表达式的值的时候, 可以存起来。 这样可以减少计算次数。 // 如: int n = 10; int b = 30; for(int i = 0; i < n; i++){ printf("%d", n * b * i); } // 更改为 int n = 10; int b = 30; int t = n * b; for(int i = 0; i < n; i++){ printf("%d", t * i); } // 如: // 计算一个数组 / 或者字符串长度的时候, 最后一直存着,以免多次计算。
2. // 位运算符的应用 // 如: int n = 30; int i = n* 2; int c = n / 16; // 可以更改为 int i = n << 1; // 相信我会快。 int c = n >> 4;