i==n?' ':'n'
经过大量计算后 由于误差影响可能把1变成0.999999 所以用floor(sqrt(n)+0.5). continue , break. while .do- while
/%d /%lld %lld (Mingw的gcc)%I64d.(暂且不深入)
避开了long long 的输入输出
int n ;
scanf("%d",&n)
long long n2 = n;
计时器 <time.h>
(double)clock()/CLOCK_PER_SEC;
2.4算法竞赛中的输入输出框架
while(scanf("%d",&x)==1){
s+=x;
if(x<min)min=x;
if(x>max)max=x;
n++;
...(此输入结束要cTRL +z Linux则是 Ctrl+D;
//定义INF 的意义 INF=100000 max=-INF min =INF;
}
文件重定向
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);上述语句使得
scanf从文件input读入 printf写入文件output.txt . 实际上所有读键盘输入与写屏幕输出的均被重定向
关于重定向的优化
#define LOCAL
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stout);
#endif
若要求用文件输入输出但禁止用重定向方式
FILE *fin ,*fout;
fin =fopen("data.in","rb");
fout=fopen("data.out","wb");
int x ;
while(fscanf(fin,"%d",&x)==1){
s+=x;
if(x<min)min=x;
if(x>max)max=x;
n++;
}
fprint(fout,"%d %d %.3f\n",min,max(double)s/n);
fclose(fin);
fclose(fout);
//重定向不能同时读写文件和标准输入输出;fopen的写法略繁琐,但是灵活性大(可以反复打开并读写文件)若想把fopen版的程序改成读写标准输入输出,只需要赋值"fin=stdin;fout=stdout"有读者可能试过fopen("con","r")的方法打开标准输入输出但这个方法不是可移植的 在linux上无效
#include <iostream>
#include <string>
#include<cstring>
#include<stdio.h>
#pragma warning(disable:4996)
using namespace std;
int main() {
FILE *fin, *fout;
fin = fopen("datain.txt", "r+");
fout = fopen("dataout.txt", "w+");
int x, n = 0; int s = 0; int avl = 0;
while (fscanf(fin, "%d", &x) == 1)
{
s += x;
n++;
}
avl = s / n;
fprintf(fout,"%d %d\n",s,avl);
fclose(fin);
fclose(fout);
return 0;
}
关于以上代码报错 主要原因在于没有把datain.txt dataout.txt 放在项目目录上~对以上可用C:\windows\system32\datain.txt;
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。