【算法竞赛入门经典学习日记】第二章 循环结构程序设计

例题 数据统计(重定向,无bug版)

#include <iostream>
#include <cstdio>

int main()
{
    freopen("infile","r","stdin");
    freopen("outfile","w","stdout");
    int x, min, max, n = 0, sum = 0,count = 0;
    while(std::cin>>n && n)
    {
        count++;
        int s =0;
        cin>>x;
        min = x; max = x;
        for(int i = 1; i < n; i++)
        {
            std::cin>>x;
            s += x;
            if(x < min) min = x;
            if(x > max) max = x;
        }
        std::cout<<"Case"<< count <<":"<< min <<" "<< max <<" "<< (double)s/n <<'\n';
    }
    return 0;
}

习题2-1 水仙花数

#include <iostream>

int main()
{
    int i;
    for(i = 100; i < 999; i++)
    {
        int j,k,l;
        j = i/100;
        k = i%100/10;
        l = i%10;
        if(j*j*j + k*k*k + l*l*l == i) std::cout<< i << std::endl;
        //else continue;
    }
    return 0;
}

习题2-2 韩信点兵

#include <iostream>

int main()
{
    int a,b,c,count = 0;
    while(std::cin>>a>>b>>c)
    {
        int i;
        count++;
        for(i = 100; i <= 100; i++)
            if(i%3 == a && i%5 == b && i%7 == c) break;
        if(i < 100 | (100%3 == a && 100%5 == b && 100%7 == c))
            std::cout<< "Case" << count << ":"  << i << std::endl;
        else 
            std:: <<"Case"<< count << ":No answer" << srd::endl;; 
    }
    return 0;
}

习题2-3 倒三角形

#include <iostream>
int main()
{
    int i,j,k,n;
    std::cin>> n;
    for(i = n; i > 0; i--)
    {
        for(j = n-i; j > 0; j--) std::cout<<" ";
        for(k = 2*i-1; k > 0; k--) std::cout<<  "#";
        std::cout<<'\n';
    }
    return 0;
}

习题2-4 子序列的和

#include <iostream>
#include <cstdio>
int main()
{
    int count = 0;
    double a,b,sum = 0;
    while(std::cin>>a>>b)
    {
        if(a == b && a == 0) break;
        count++;
        if(a > 100)      //当a大于100时,其平方倒数小于0.00001
        {
            std::cout<<"Case"<< count << ":" << 0.00001 << std::endl;
            continue;
        }
        for(int i = b-a; i >= 0; i--)
            sum += 1/((a+i)*(a+i));
        std::cout<<"Case"<< count <<":";
        printf("%.5f\n", sum);
    }
    return 0;
}

习题2-5 分数化小数

#include <iostream>
#include <iomanip>

int main()
{
    int a,b,c,count = 0;
    while(std::cin>>a>>b>>c)
    {
        if(a == 0 && b == 0 && c == 0) break;
        count++;
        std::cout.precision(c);
        std::cout.setf(std::ios::fixed);  //设置小数位位数为c
        std::cout<<"Case"<< count << (double)a/(double)b <<std::endl;
    }
    return 0; 
}

习题2-6 排列

#include <iostream>

int main()
{
    int i,j,k;
    int a[9+1];
    for(i = 123; i <= 329; i++)
    {
        int n;
        j = 2 * i;
        k = 3 * i;
        a[i/100]++;
        a[i%100/10]++;
        a[i%10]++;
        a[j/100]++;
        a[j%100/10]++;
        a[j%10]++;
        a[k/100]++;
        a[k%100/10]++;
        a[k%10]++;
        for(n = 1; n < 10; n++)
            if(a[n]!= 1) break;
        if(n > 9) std::cout<< i <<" "<< j <<" "<< k <<std::endl;
        for(n = 0; n < 10; n++)
            a[n] = 0;
    }
    return 0;
}

参考文献:C++输出时小数点后的位数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值