蓝桥杯2017年第八届A组c/c++真题

  • 承压计算
    这题好坑 ,用printf 和用 cout 的输出结果居然不一样。
    如果当时做题的时候我用了 cout ,说不定要gg。
    主要原因还是因为 cout 和 printf 在打印浮点数输出的精确度不一样的原因。

    代码如下:

#include<iostream>
#include<cstring>
#include<cstdio> 
using namespace std;
int main()
{
    double  sum[35];
    double  sum2[35];
    int num[35];
    int i,j;
    int t;
    memset(sum,0,sizeof(sum));
    memset(num,0,sizeof(num));


    for(i=1;i<30;i++)
    {
        memset(sum2,0,sizeof(sum2));
        for(j=1;j<=i;j++)
        {
            cin>>t;
            sum[j]+=t;

        }
        for(j=1;j<=i;j++)
        {

            double temp   = sum[j]*1.0/2;
            sum2[j] +=temp;
            sum2[j+1] +=temp;


        }

        for(j=1;j<=i+1;j++)
        {
            sum[j] =sum2[j];
        }



    }
        double min =10000;
        double max=0;
        for(j=1;j<=i+1;j++)
        {
            if(sum[j]>max)max = sum[j];
            if(sum[j]<min&&sum[j]!=0)min = sum[j];      
        }

        cout<<max<<"  "<<min<<endl;
        cout<<2086458231*max/min<<endl;
        printf("%lf", 2086458231*max/min  );

}

输出结果如下:
这里写图片描述


区分 : “%lf” “%f” 有什么区别?
二者分别对应 double float 的输出,最好不要弄混。


日期问题


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int judge(int year,int month,int day)
{
    switch(month)
    {
        case 1:
            case 3:
                case 5:
                    case 7:
                        case 8:
                            case 10:
                                case 12:
                                    if(day>0&&day<32)return 1;
                                    return 0;
                                    break;
        case 4:
            case 6:
                case 9:
                    case 11:
                        if(day>0&&day<31)return 1;
                        return 0;
                        break;

        case 2:
            if(year%400==0||year%4==0&&year%100!=0)
            {
                if(day>0&&day<30)return 1;
                return 0;
            }
            else
            {
                if(day>0&&day<29)return 1;
                return 0;
            }
    }
}
struct node 
{
    int year;
    int month;
    int day;

};
bool cmp(node a,node b)
{
    if(a.year!=b.year)return a.year<b.year;
    else if(a.month!=b.month)return a.month <b.month;
    else return  a.day<b.day;
}

int main()
{
    int x,y,z;

    while(scanf("%d/%d/%d",&x,&y,&z)!=EOF)
    {

        node temp;
        vector<node> nodes;

        //第一种
        temp.year = x;
        temp.month =y;
        temp.day = z;
        if(temp.year>=60)temp.year +=1900;
        else temp.year +=2000;
        if(temp.month>0&&temp.month<13)
        {
            if(judge(temp.year,temp.month,temp.day)==1) nodes.push_back(temp);
        }



        //第二种
        temp.year = z;
        temp.month = x;
        temp.day = y;
        if(temp.year>=60)temp.year +=1900;
        else temp.year +=2000;
        if(temp.month>0&&temp.month<13)
        {
            if(judge(temp.year,temp.month,temp.day)==1) nodes.push_back(temp);
        }



        //第三种
        temp.year = z;
        temp.month = y;
        temp.day = x;
        if(temp.year>=60)temp.year +=1900;
        else temp.year +=2000;
        if(temp.month>0&&temp.month<13)
        {
            if(judge(temp.year,temp.month,temp.day)==1) nodes.push_back(temp);
        }


        sort(nodes.begin(),nodes.end(),cmp);

        for(int i=0;i<nodes.size();i++)
        {
            if(i!=0)
            {
                if(nodes[i].year==nodes[i-1].year && nodes[i].month == nodes[i-1].month &&nodes[i].day ==nodes[i-1].day) 
                continue;
                 cout<<nodes[i].year<<"-"<<nodes[i].month<<"-" <<nodes[i].day<<endl;
            }
            else cout<<nodes[i].year<<"-"<<nodes[i].month<<"-" <<nodes[i].day<<endl;
        }
    }
    return 0;
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值