Flooded! UVA - 815 (sort排序)
错了好多遍,不知道为啥出错,如果有大神发现,请求指点!!!
附错误代码(错的不知道怎么回事):
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 32 + 5;
int hblock[maxn*maxn]; /*elevation*/
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == 0 && m == 0)
break;
// memset(hblock, 0, sizeof(hblock));
for(int i = 0; i < m * n; i++)
scanf("%d", &hblock[i]);
hblock[m*n] = 2147483647;
// cout << hblock[m*n] << endl;
sort(hblock, hblock + m*n);
double v;
double h;
cin >> v;
v = v / 100.0; /*除以一个区的底面积,得到其高度*/
double mark = 0; /*标记被淹的极限*/
for(int i = 1; i <= m * n; i++)
{
v += hblock[i-1];
h = v / i; /**淹完一个时的高度**/
if(h < hblock[i])
{
mark = i;
break;
}
}
int cases = 0;
printf("Region %d\n", ++cases);
printf("Water level is %.2lf meters.\n", h);
printf("%.2lf percent of the region is under water.\n", 100.0*mark/double(n*m)); /*double强制转换不可忘,不然会有大量损失*/
}
return 0;
}
正确代码:
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#define INF 0x7fffffff
int h[35 * 35], n, m;
double v, H, k;
int main()
{
int cas = 0;
while(scanf("%d%d", &n, &m), n)
{
n *= m;
for(int i = 0; i < n; i++)
scanf("%d", h + i);
h[n] = INF, n++;
sort(h, h + n);
scanf("%lf", &v);
v /= 100.0;
for(int i = 1; i <= n; i++)
{
v += h[i - 1];
H = v / i;
if(H < h[i])
{
k = i;
break;
}
}
printf("Region %d\n", ++cas);
printf("Water level is %.2lf meters.\n", H);
printf("%.2lf percent of the region is under water.\n\n", 100.0 * k / (n - 1));
}
return 0;
}
Flooded! UVA - 815 (sort排序)的更多相关文章
- 【每日一题】Flooded! UVA - 815 模拟阅读格式题
https://cn.vjudge.net/problem/UVA-815 题意:给你一个矩阵,每个格子的数代表一个海拔并且每个格子的面积100平方米.给你整个区域的降水量(立方米),问降水量(米). ...
- 2.sort 排序命令讲解
sort命令 sort:文本排序,仅仅是对显示文件的排序,而不影响源文件的顺序,是根据ASSII码 的字符升序来排列的. -n:安装数值大小从小到大排列 ,默认是升序. ...
- 反向输出及sort排序
建立条件:#include "algorithm"引用这个头文件 1.reverse 的用法,反向排序,由自己输入5个数: 1 2 3 4 5 for (int i = 0; i ...
- JAVA Collections工具类sort()排序方法
主要分析内容: 一.Collections工具类两种sort()方法 二.示例 一.Collections工具类两种sort()方法 格式一: public static <T extends ...
- javascript:算法之数组sort排序
数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...
- sort排序
/*问题 L: 使用sort排序题目描述标准库的sort函数给我们提供了一个很方便的排序的方法,光听别人说方便不顶事,得自己亲自实践一下才能体会到它的方便之处. 输入每组包含多组数据,每组数据第一行包 ...
- [转] C++的STL库,vector sort排序时间复杂度 及常见容器比较
http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...
- List<T>.Sort() 排序的用法
List<T> 可以通过 .Sort()进行排序,但是当 T 对象为自定义类型时(比如自定义模型),就需要 IComparable接口重写其中的方法来实现,实现代码如下: class Pr ...
- sort排序中的坑
问题的产生原因: 在一篇阿里面试题的跟帖中,很多人应用sort()方法对数组进行排序.看似合情合理的代码,运行结果却频频出错.为什么呢?因为很多人都忽略掉了一点,那就是sort()排序默认情况下是按A ...
随机推荐
- jquery与angular的交互
HTML: <table class="table table-striped" ng-app="myApp"> <tr> <th ...
- TCP/IP和OSI4层、7层协议介绍
1.TCP/IP全称:Transmission Control Protocol / Internet Protocol 中文翻译:传输控制协议 / 互联网协议 2.OSI4层.7层模型:
- C# COM组的开发以及调用
一.用C#编写一个COM组件 1. 打开Visual Studio2008,[文件]->[新建]->[项目] 2. 项目类型=Visual C#,模版=类库,名称= ...
- Error: Error occured while starting App. Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
Error: Error occured while starting App. Original error: Activity used to start app doesn't exist or ...
- windows下的计算时间间隔 -- GetTickCount()
用法: #include "windows.h" DWORD lastTime =0;DWORD currentTime = 0;DWORD spendTime = 0; last ...
- Scratch学习中需要注意的地方,学习Scratch时需要注意的地方
在所有的编程工具中,Scratch是比较简单的,适合孩子学习锻炼,也是信息学奥赛的常见项目.通常Scratch学习流程是,先掌握程序相关模块,并且了解各个模块的功能使用,然后通过项目的编写和练习,不断 ...
- coco2dx--Permission denied
在终端输入./cocos.py....创建项目时,出现Permission denied,是权限问题,可以先使用chmod命令获得权限,输入chmod u+x ./cocos.py 回车,接着再使用c ...
- 用python编写一个搜索引擎
完整代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- import sys import os import datetime from PyQt5 ...
- Fiddler的使用总结
关于Fiddler的使用过程中的总结: 1. 配置手机抓包的过程,以后再补充 2.使用Fiddler发送请求 1) 第一步 抓取接口,获取请求方式,以及请求参数 2) 第二步 请求接口 点击Exec ...
- .net core获取数据库连接 抛出The type initializer to throw an exception
原文:https://www.cnblogs.com/pudefu/p/7580722.html 在.NET Framework框架时代我们的应用配置内容一般都是写在Web.config或者App.c ...