做题的小总结

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值