平均年龄(<cmath> 中 ceil 函数的应用)---- 美团2016研发工程师在线编程题

来源:互联网 发布:手机简谱制作软件 编辑:程序博客网 时间:2024/04/28 22:12

[编程题] 平均年龄
已知某公司总人数为W,平均年龄为Y岁(每年3月末计算,同时每年3月初入职新人),假设每年离职率为x,x>0&&x<1,每年保持所有员工总数不变进行招聘,新员工平均年龄21岁。 
从今年3月末开始,请实现一个算法,可以计算出第N年后公司员工的平均年龄。(结果向上取整)。 
输入描述:
输入W Y x N


输出描述:
输出第N年后的平均年龄

输入例子:
3

输出例子:
5

#include <iostream>#include <cmath>using namespace::std ;int main() {    int w ;    float y ;    float x ;    int n ;            while ( cin >> w >> y >> x >> n ) {        while ( n -- ) {            y = 21 * x + ( 1 - x ) * ( y + 1 ) ;        }                cout << ceil( y ) << endl ;    }    return 0 ;}



第二次做:

#include <iostream>#include <cmath>using namespace::std ;int main() {    double w, y, x, n ;        while ( cin >> w >> y >> x >> n ) {        while ( n -- ) {            y = x * 21 + ( 1 - x ) * ( y + 1 ) ;        }        cout << ceil( y ) << endl ;    }        return 0 ;}


第三次做:

#include <iostream>#include <algorithm>using namespace::std ;int main() {    float w, x, y ;    int n ;        while ( cin >> w >> y >> x >> n ) {        while ( n -- ) {            y = 21 * x + ( 1 - x ) * ( y + 1 ) ;        }        cout << ceil( y ) << endl ;    }        return 0 ;}


0 0
原创粉丝点击