#includeusing namespace std;//为了防止命名冲突 int main(){ cout << "hello world" << endl; return 0; }
#include变量类型//引入可以使用 print() scanf()
bool false/true 1byte
char 'c','a',' ','n' 1byte
int -2147483648---2147483647 4byte
float 1.23,2.5,1.235e2, 6-7位有效数字 4byte
double 15--16有效数字 8byte
long long -2^63-----2^63-1 8byte
long double 18--19位有效数字 12byte 16byte
提交代码常见错误
Wrong Answer 错误 Time Limit Error 超时 Memory Limit Error 超内存 Segmentation Fault 数组越界
presentation error 意为输出格式错误,也就是你的输出没有按照输出要求进行格式化。
#includeusing namespace std; int main(){ int a,b;//斐波那契数列数列。 a = 1; b = 1; int n; cin >> n; int i = 0; while (i < n - 1){ int c = a + b; a = b; b = c; i++; } cout << a << endl; return 0; }
如图:
曼哈顿距离: (x1,y1) (x2,y2) d=|x2-x1| + |y2 - y1|
一个整数,除了本身以外的其他所有约数的和如果等于该数,那么我们就称这个整数为完全数。
例如,6 就是一个完全数,因为它的除了本身以外的其他约数的和为 1+2+3=6。
现在,给定你 N 个整数,请你依次判断这些数是否是完全数。
解答:
#includeusing namespace std; int main(){ int n; cin >> n; while(n--){ int x; cin >> x; int s = 0; for(int i = 1;i< x;i++){ if (x % i == 0){ s +=i; } } if(s == x){ printf("%d is perfectn",x); }else{ printf("%d is not perfectn",x); } } return 0; }```//代码提交状态: Time Limit Exceeded //c++一秒内算不能超过一亿次, //10亿次。
优化版本:
#include斐波那契数列using namespace std; int main(){ int n; cin >> n; while(n--){ int x; cin >> x; int s = 0; for(int i = 1;i*i< x;i++){ if (x % i == 0){ if(i< x){ s +=i; } if(x/i != i && x/i < x){ s += x/i; } } } if(s == x){ printf("%d is perfectn",x); }else{ printf("%d is not perfectn",x); } } return 0; }
#include输入一个n,再输入n个整数.将这n个整数逆序输出.#include using namespace std; int main(){ int f[100]; f[0] = 0,f[1] = 1; int n; cin >>n; for(int i = 2;i <=n;i++){ f[i]=f[i-1]+f[i-2]; } printf("%d",f[n]); return 0; }
#include#include using namespace std; int main(){ int a[100]; int n,k; cin >> n >> k; for(int i = 0;i > a[i]; } while(k--){ int t = a[n-1]; for(int i = n-2;i>=0;i--){ a[i+1] =a[i]; } a[0]=t; } for(int i =0;i
优化;#include#include #include using namespace std; int main(){ int a[100]; int n,k; cin >> n >> k; for(int i =0;i< n;i++){ cin >> a[i]; } reverse(a,a + n); reverse(a,a+k); reverse(a+k,a+n); for(int i =0;i < n;i++){ cout << a[i] << ' '; } return 0; } reverse简介
求阶乘#include计算2的N次方。N <=10000.#include #include using namespace std; const double eps = 1e-6; double C(int a,int b){ double res = 1; for(int i=1,j=a;i<=b;i++,j--){ res = res*j/i; } return res; } int main(){ int a,b; cin >> a >> b; cout << C(a,b) << endl; return 0; } 2的N次方的大数求法:
个位存在a[0],高位在后面。#include#include memset(a,0,sizeof a)#include using namespace std; const int N = 3010; int main(){ int a[N] = {1}; int n; cin >> n; int t=1; // for(int i =0;i =0;i--){ cout << a[i]; } return 0; } a:数组名字
memcpy(b,a,sizeof a);
0:初始值
sizeof a:数组长度。(注以字节为单位).b:目标数组
753. 平方矩阵 I
a:原数组
sizeof a:要复制数组长度 (注以字节为单位).输入整数 N,输出一个 N 阶的回字形二维数组。
数组的最外层为 1,次外层为 2,以此类推。
输入格式
输入包含多行,每行包含一个整数 N。当输入行为 N=0 时,表示输入结束,且该行无需作任何处理。
输出格式
对于每个输入整数 N,输出一个满足要求的 N 阶二维数组。每个数组占 N 行,每行包含 N 个用空格隔开的整数。
每个数组输出完毕后,输出一个空行。
数据范围
0≤N≤100
输入样例:
1
2
3
4
5
0
输出样例:
11 1
1 11 1 1
1 2 1
1 1 11 1 1 1
1 2 2 1
1 2 2 1
1 1 1 11 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1#include754. 平方矩阵 IIusing namespace std; int main(){ int n; while(cin >>n,n){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int up=i,down=n-i+1,left =j,right =n-j+1; cout << min(min(up,down),min(left,right)) << ' '; } cout << endl; } cout << endl; } return 0; } #includeusing namespace std; int q[100][100]; int main(){ int n; while(cin >> n,n){ // 方法一: // for(int i=0;i



