zoukankan      html  css  js  c++  java
  • PAT (Basic Level) Practice (中文) 1070 结绳 (25分)

    1.题目

    给定一段一段的绳子,你需要把它们串成一条绳。每次串连的时候,是把两段绳子对折,再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连。每次串连后,原来两段绳子的长度就会减半。

    rope.jpg

    给定 N 段绳子的长度,你需要找出它们能串成的绳子的最大长度。

    输入格式:

    每个输入包含 1 个测试用例。每个测试用例第 1 行给出正整数 N (2≤N≤10​4​​);第 2 行给出 N 个正整数,即原始绳段的长度,数字间以空格分隔。所有整数都不超过10​4​​。

    输出格式:

    在一行中输出能够串成的绳子的最大长度。结果向下取整,即取为不超过最大长度的最近整数。

    输入样例:

    8
    10 15 12 3 4 13 1 15
    

    输出样例:

    14

    2.题目分析

    数字向上取整、向下取整、四舍五入方法(https://www.cnblogs.com/dyhaohaoxuexi/p/10928566.html

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
        double a=1.5;
        cout<<ceil(a)<<endl;   //向上取整
        cout<<floor(a)<<endl;   //向下取整
        cout<<round(a)<<endl;   //四舍五入
        //不使用函数   
        cout<<(int)a<<endl;  //向下取整
        cout<<(a>(int)a?(int)a+1:(int)a)<<endl;   //向上取整
        cout<<(int)(a+0.5)<<endl;//四舍五入
        return 0;
    }

     3.代码

    #include<iostream>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    double list[10001];
    int main()
    {
    	int n;
    	cin >> n;
    	for (int i = 0; i<n; i++)
    		cin >> list[i];
    	sort(list, list + n);
        if(n==2){cout<<floor((list[0]/2+list[1]/2));return 0;}
    	double count = (list[0] / 2 + list[1] / 2) / 2;
    	for (int i = 2; i<n-1; i++)
    	{
    		count =(count + list[i] / 2)/2;
    	}
    	count = count + list[n-1]/2;
    	cout << floor(count);
    }
  • 相关阅读:
    oracle 批量修改表名 字段为大写197
    身份证附件上传样例197
    npm 设置源197
    manajo常用命令197
    vue 父组件向子组件传参197
    将BAT文件注册为服务197
    teaweb — linux 系统资源监控
    glances — linux 系统资源监控
    emitter-Client
    urlencode编码 — 为什么要编码
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788921.html
Copyright © 2011-2022 走看看