您的位置:首页 > 其它

学习心得一之算法竞赛入门

2015-07-31 21:28 357 查看
1.%lf 标准控制输出符 保留小数点后六位。

2.int / int = int; 

3.在算法竞赛中不要使用头文件conio.h,包getch(),getche()函数。

4.尽量用const关键字声明常数。

5.不拘一格得使用伪代码来思考和描述算法。

在观察无法找出错误时,可以用“输出中间变量”的方法查错。

6.floor(x)函数返回x的整数部分。

scanf 函数返回成功输入的变量个数。

7.程序计时器:

#include<ctime>cout << (double)clock()/CLOCKS_PER_SEC << endl;

8.使用文件输入输出重定向:(freopen)

//#define LOCAL                           //只有定义了符号LOCAL,才编译freopen两条语句

#include<stdio.h>

#define INF 1000000000

int main()

{

    //#ifdef LOCAL

        freopen ("data.in.txt", "r" , stdin);

        freopen ("data.out.txt", "w" ,stdout);

    //#endif

    int x, n = 0, min = INF , max = -INF, s = 0;

    while(scanf("%d", &x) == 1)

    {

        s += x;

        if(x < min)     min = x;

        if(x > max)     max = x;

        //printf("x = %d, min = %d, max = %d\n", x ,min, max);  //输出中间结果

        n++;

    }

    printf("%d %d %.3lf\n", min, max, (double)s/n);

    return 0;

}

用文件输入输出:(fopen)

c:

#include<stdio.h>

#define INF 1000000000

int main()

{

    FILE *fin, *fout;

    fin = fopen("data.in.txt", "rb");

    fout = fopen("data.out.txt", "wb");

    int  x, n = 0, min = INF, max = -INF, s = 0;

   while(fscanf(fin, "%d", &x) == 1)

    {

        s += x;

        if(x < min)     min = x;

        if(x > max)     max = x;

        //printf("x = %d, min = %d, max = %d\n", x ,min, max);

        n++;

    }

    fprintf(fout, "%d %d %.3lf\n", min, max, (double)s/n);

    fclose(fin);

    fclose(fout);

    return 0;

}

c++:

#include<iostream>

#include<fstream>

using namespace std;

ifstream fin("data.in.txt");

ofstream fout("data.out.txt");

int main()

{

    int a,b;

    while (fin >> a >> b)   fout << a + b <<endl;

    return 0;

}

9.题目:输入两个正整数n<m<10^6,输出1/n^2 + 1/(n+1)^2 + …… + 1/m^2,保留5位小数。例如n=2,m=4时答案是0.42361;n=65536,m=655360时答案为0.00001。注意:本题有陷阱。

#include<iostream>

#include<iomanip>

#include<fstream>

using namespace std;

//ifstream fin("data.in.txt");

//ofstream fout("data.out.txt");

int main()

{

  int n,m,i;

  long double sum = 0;

    cin >> n >> m;

    for(i = n; i <= m; i ++)

    {

        sum += 1.0 / i / i;                                                 //本题陷阱在于n比较大时,n*n会溢出,所以 1/n^2 应该用 1/n/n 而不是 1/(n*n).

        //sum += 1 / double(pow(double(i), 2.0));

    }

    cout << fixed << setprecision(5) << sum << endl;

    return 0;

}

10.用1,2,3,...,9组成3个三位数abc,def和ghi,每个数字恰好使用一次。要求abc:def:ghi=1:2:3.输出所有解。提示:不必太动脑筋。

#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

    int x, y, z, a[10] = {0};

    for(x = 100; x < 333; x++)

    {

        y = 2*x;

        z = 3*x;

        //令a[出现的数字] = 1

        a[x/100] = a[x/10%10] = a[x%10] = 1;

        a[y/100] = a[y/10%10] = a[y%10] = 1;

        a[z/100] = a[z/10%10] = a[z%10] = 1;

        int i, s = 0;

        for(i = 1; i < 10; i++)

            s += a[i];

        if(s == 9)

           cout << x << " " << y << " " << z << endl;

        for(i = 1; i < 10; i++)  //重新赋值为0

            a[i] = 0;

    }

    return 0;

}

第一章~第二章
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: