poj1064

Cable master
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 77090   Accepted: 15630

Descriptionios

Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants using a "star" topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it. 
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible. 
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled. 
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.

Inputgit

The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two digits after a decimal point.

Outputpromise

Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point. 
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number "0.00" (without quotes).

Sample Inputide

4 11
8.02
7.43
4.57
5.39

Sample Output函數

2.00

Sourcethis

一句話題意:
給定n條長度不一的線段,問是否能均分紅k段,若是不能則輸出0.00.(1 = N = 10000,1 = K = 10000,每條線段的長度大於等於1,小於等於100km)
解析:
這種題目,沒有明顯的規律,只能嘗試分段,比較快的解決方式就是二分。
因爲題目不能劃分K段則輸出0.00,因此L=0.00,R=100*1000或者讀入的最大值
讀入的最大值須要比較n次得出,不如R直接取最大值,這樣最多的比較次數爲log(100000)<20,也就是說最多比較也沒有20次。
程序:整數二分
 
#include<iostream>
#include<cstdio>
using namespace std;
int n,k;
const int maxn=10010;
int a[maxn];
int check(int  x){
    int s=0;
    for(int i=1;i<=n;i++)
        s=s+a[i]/x;
    if (s>=k)return 1;
    else return 0;
}
int main(){
    cin>>n>>k;
    double x;
    int temp0;
    for(int i=1;i<=n;i++){
        cin>>x;
        a[i]=x*100;
    }
    int l=0,r=10000000,mid;
    while (l<r){
        mid=(l+r+1)/2;
        if(check(mid))l=mid;
        else r=mid-1;
    }
    printf("%.2lf\n",l/100.0);
    return 0;
}
    

 

 2.實數二分spa

趙老師,這個題爲何要用floor函數呀
不加它還不對,這是考查的什麼呢
像這種題目怎麼判斷何時用,round或者floor或者ceil呢,它根本沒作具體說明
若是答案是2.458的話,保留兩位的話正確的答案應該是2.46仍是2.45?
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n,k;
const int maxn=10010;
double a[maxn];
int check(double  x){
    long long s=0;
    for(int i=1;i<=n;i++)
        s=s+int(a[i]/x);
    if (s>=k)return 1;
    else return 0;
}
int main(){
    cin>>n>>k;
    float x;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    double l=0,r=1000000,mid;
    for(int i=1;i<=100;i++){
        mid=(l+r)/2;
        if(check(mid))l=mid;
        else r=mid;//由於是實數。 不能減1.
    }
    printf("%.2lf\n",floor(100*l)/100);
    return 0;
}
    

疑問?本題爲何不能用int輸出呢?固然能夠,但不能這樣寫:    printf("%.2lf\n",int(100*l)/100);而能夠這樣printf("%.2lf\n",int(100*l)/100.00);3d

知識補充:int強制類型轉換\取整函數floor() ceil() round()函數的區別code

知識:在C++的頭文件中有floor()和ceil()函數。在STL中還有round()函數。這三個函數的做用以下:
                              
blog

從函數說明中能夠看出,
(1) Floor()會取不大於自變量的最大整數,這樣自變量是3.1或3.9是沒有區別的,返回都是3;自變量是-2.1或-2.9也是沒有區別的,返回都是-3;
(2) Ceil()會取不小於自變量的最大整數,這樣自變量是3.1或3.9,返回都是4;自變量是-2.1或-2.9,返回的都是-2;
(3) Round()函數,纔是咱們須要的四捨五入的函數,由於它會返回離自變量最近的整數,這個返回的整數可能大於也可能小於原來的數,可是必定是離它最近的那個整數。

注:floor(), ceil()函數都包含在頭文件「Math.h」中,可是round()函數未包含在該頭文件中。所以能夠經過以上的原理,來本身實現round()函數,實現含有小數的數字的四捨五入。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
	double k=-5.9999;//5.999
	cout<<int(k*1000)<<endl;
	cout<<floor(k*1000)<<endl;
	printf("%.0lf\n",k*1000);
	return 0;
}

 C/C++中使用int強制類型轉換和floor函數有區別嗎?

一、int是向0取整,好比:1.9會變成1,-1.9會變成-1
floor是向下取整,好比:1.8會變成1,-1.1會變成-2(注意這點和int不一樣)

二、返回值類型也有區別。如下是floor的原型:
float floor( float arg );
double floor( double arg );
long double floor( long double arg );
double floor( Integral arg ); (C++11

因而可知,floor返回的值是浮點型,而(int)返回的值是整型。

floor 返回小於或者等於指定表達式的最大整數 而int只返回整數部分 例如:假設a=3.2 用floor和int都是3,可是當a=-3.2的時候floor(a)返回的是-4,而(int)(a)返回的是-3;

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息