1、输出的间距问题,直接复制样例输入,来参考。
2、C++函数,max(a,b) min(a,b)
返回两个值中较(大/小)的值,头文件是
#include <algorithm>
3、C++控制输出格式
头文件是
#include <iomanip>
4、开方(四舍五入)
#include <math.h>
int x;
sqrt(x) 开方
floor( sqrt(x) +0.5 ) 减小误差,四舍五入
5、获取程序的运行时间
#include <time.h>
printf(" Time used = %.2f\n", (double)clock() / CLOCKS_PER_SEC);
6、输入输出框架
scanf()函数的返回值是返回正确输入的个数
while(scanf("%d",x)==1) 输入空白字符无法结束程序的
ctrl + Z 即可
重定向输入,免去每次输入数据的麻烦,将输入输出保存在文件中(此种写法免去删除)
#define LOCAL
#include <stdio.h>
int main()
{
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif 后接正式的程序
}
文件输入(非重定向)
FILE *fin,*fout;
fin = fopen("input.txt","rb");
fout = fopen("output.txt","wb");
while(fscanf(fin,"%d",&x)==1) 读入文件
fprintf(fout,"%d",x);
fclose(fin);
fclose(fout);
标准输入输出,即为使用键盘进行输入。
fopen版程序改为标准输入输出,只需
fin = stdin; fout=stdout;
以下编辑于2019-03-21
生成指定位数的全排列,示例代码如下:
#include <cstdio>
#inlcude <algorithm>
using namespace std;
int main()
{
int n,p[10];
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&p[i]);
sort(p,p+10);
do
{
for(int i=0;i<n;i++)printf("%d",p[i]);
printf("\n");
}while(next_permutation(p,p+n);
return 0;
}
以下编辑于2019-03-23明天又是新的开始
返回 长度为n,的数组p的值为x的元素出现第一次的位置。
lower_bound(p,p+n,x);
cout<<*lower_bound(p,p+n,x);