首页 > 试题广场 >

平均年龄

[编程题]平均年龄
  • 热度指数:31440 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
已知某公司总人数为 W ,平均年龄为 Y 岁(每年 3 月末计算,同时每年 3 月初入职新人),假设每年离职率为 x , x > 0 && x < 1 ,每年保持所有员工总数不变进行招聘,新员工平均年龄 21 岁。
从今年 3 月末开始,请实现一个算法,可以计算出第 N 年后公司员工的平均年龄。(最后结果向上取整)。
数据范围:

输入描述:
输入W Y x N


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

输入

5 5 0.2 3

输出

15
推荐
#include <iostream>
#include <cmath>

using namespace std;
void AverageAge()
{
    // 总人数为W,平均年龄为Y岁
    // 每年离职率为x,x>0&&x<1
    double W, Y, x, N;

    while(cin >> W >> Y >> x >> N)
    {
        while(N--)
        {
            Y = 21 * x + (1 - x) * (Y + 1);
        }

        cout << ceil(Y) << endl;
    }
}

int main()
{
    AverageAge();
    return 0;
}

编辑于 2016-04-06 10:50:52 回复(20)
import java.util.Scanner;

public class Main{
    //这个题着实让我弄了好一会,首先有三个误区,
    //第一个误区,每年在招纳新员工的同时,老员工的年龄是要增长的
    //第二个误区,为什么最后的公式与 W 无关。
    //第三个误区,是这个向上取整,题目给出的用例是5 5 0.2 3
    //然后我计算第一年的平均年龄是9,不用取整。
    //然后计算第二年的平均年龄是11.4,这时候我直接向上取整了,变成了12
    //所以下一年的平均年龄我误以为是12+1,导致我的计算结果不符合
    //其实题目是想让你把最后一年计算出来的年龄向上取整。
    //下面看一下代码
    public static int Average(int W ,double Y ,double x ,int N){
        //W表示公司总人数,Y表示当年平均年龄,x表示离职率,N表示多年以后
        for(int i = 0 ; i < N ; i++){
            //原始公式是这个Y = ((Y+1)*(W-W*x) + 21*(W*x))/W;
            //先求的离职之后还没有纳新时所有员工的总年龄,
            //记得Y+1,老员工也是要长大的
            //求得老员工的总年龄,然后加上纳新的员工的总年龄
            //最后除去公司的总人数,就是当年公司的平均年龄。
            //这个年龄是不进行向上取整的,而且我们发现这个公式是可以化简的
            //*************************************
            //下面是化简之后的公式,可以发现公式是与W没有关系的,
            //这就是为啥有的人纳闷为啥别人直接给出公式为啥与W无关,
            //但是自己又感觉最后公式肯定与W有关的原因了。
            //所以传参的时候可以不用传W。此处只是为了说明,
            Y = (Y+1)*(1-x)+21*x;
        }
        //最后对求得的结果进行向上取整。返回就OK了
        return (int) Math.ceil(Y);
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        do{
            int W = sc.nextInt();
            double Y = sc.nextDouble();
            double x = sc.nextDouble();
            int N = sc.nextInt();
            System.out.println(Average(W,Y,x,N));
        }while(sc.hasNext());
    }
}

编辑于 2016-08-24 16:22:41 回复(8)

平均年龄不好算 但是由于人数是不变的 只需要算出总的年龄all即可,每年的剩下的1-x老员工的的总年龄加W 再加上新来的W*x*21的新员工的总年龄
最后总年龄all除以总人数W

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    double W, Y, N, x;
    while(scanf("%lf%lf%lf%lf", &W, &Y, &x, &N) == 4){
        double all = W*Y;
        while(N--) all = (1 - x)*(all + W) + x*W*21;
        int ans = ceil(all/W);
        cout<<ans<<endl;
    }
    return 0;
}
编辑于 2018-08-27 10:32:02 回复(1)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            double w = sc.nextDouble();
            double y = sc.nextDouble();
            double x = sc.nextDouble();
            double n = sc.nextDouble();
            double avg=y;
            for(int i=0;i<n;i++) {
                avg=(w*(1+y)*(1-x)+w*x*21)/w;
                y=avg;
            }
            System.out.println((int)Math.ceil(avg));
        }
    }
}

发表于 2019-04-11 14:43:13 回复(0)
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
		while (scanner.hasNextInt()) {
			double w = scanner.nextDouble();
			double y = scanner.nextDouble();
			double x = scanner.nextDouble();
			int n = scanner.nextInt();
			while (n-- > 0) {
				y = (y + 1) * (1 - x) + x * 21;
			}
			System.out.println((int) Math.ceil(y));
		}
		scanner.close();
    }

    
}

发表于 2016-08-21 23:20:32 回复(0)
#include<iostream>
#include<cmath>

using namespace std;

int main() {
	int W, N;
	double Y, x;
	while (cin >> W >> Y >> x >> N) {
		while (N--) {
			Y = (1.0 - x)*(Y + 1) + x * 21; //(Y+1):平均年龄y第二年要+1岁!
		}
		cout << ceil(Y) << endl;
	}
}

发表于 2017-06-27 20:49:10 回复(0)
递归的代码!!
#include<iostream>
#include<cmath>
using namespace std;
double calculation(double W,double Y,double x,int &N)
{
    if(N)
    {
        Y=(Y+1)*(1-x)+21*x;
        N--;
        return calculation(W,Y,x,N);
    }
    return Y;
}
int main()
{   
    int N;
    double W,Y,x;
    while(cin>>W>>Y>>x>>N)
    {
        cout<<ceil(calculation(W,Y,x,N))<<endl;
    }
}

发表于 2017-06-25 16:07:59 回复(1)

math.ceil为向上取整函数

from math import ceil
while True:
    try:
        w, y, x, n = map(float, input().split())
        ave_age = y
        for i in range(int(n)):
            ave_age = (ave_age + 1) * (1 - x) + x * 21
        print(ceil(ave_age))
    except Exception:
        break
编辑于 2019-05-05 21:31:04 回复(0)
#include <bits/stdc++.h>

using namespace std;

int main()
{     int W,Y,N;     double x;     while(cin>>W>>Y>>x>>N)     {         double y = Y;         for(int i=1;i<=N;i++)             y = 21*x+(1-x)*(y+1);             cout<<ceil(y)<<endl;                 return 0;
}

发表于 2017-11-27 00:53:00 回复(0)
// 题目意思确实没说清楚
// 需要注意每年老员工都会增加一岁
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int w = sc.nextInt();
            int y = sc.nextInt();
            double x = sc.nextDouble();
            int n = sc.nextInt();
            double avgAge = y;
            for(int i=0;i<n;i++){
                avgAge = (1.0-x)*(avgAge+1.0)+x*21.0;
            }
            System.out.println((int)Math.ceil(avgAge));
        }
    }
}

发表于 2017-08-31 10:17:13 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        while (in.hasNext()) {
            int W,N;
            double Y,x;

            W=in.nextInt();
            Y=in.nextDouble();
            x=in.nextDouble();
            N=in.nextInt();

            while (N>0) {
                Y=x*21+(1-x)*(Y+1);
                N--;
            }

            System.out.println((int)Math.ceil(Y));
        }
        in.close();
    }
}


发表于 2017-07-18 19:40:43 回复(0)
#include<iostream>
#include<math.h>
using namespace std;
int main(){
    float w,y,x,n;
    while(cin >> w >> y >> x >> n){
        while(n--)
            y =  (y+1)*(1-x) + 21*x ;
        cout << ceil(y) << endl;
    }
}
编辑于 2017-06-25 09:28:19 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        
        Main  M = new Main();
        Scanner sc = new Scanner(System.in);
do{
            int W = sc.nextInt();
            double Y = sc.nextDouble();
            double x = sc.nextDouble();
            int N = sc.nextInt();
            System.out.println(ageA(W,Y,x,N));
        }while(sc.hasNext());
    }
    
    public static int ageA(int X,double Y,double x,int N){
        double age = Y;//每年的平均年龄
        if(N == 0){
            return (int) Math.ceil(age);
        }
        for(int i = 1;i <= N ; i++){
            //每年的新增人平均年龄 + 离职后剩下员工(年龄要加1)的平均年龄
            age = (21*(X*x) + (age+1)*X*(1-x))/X;
        }
        return (int) Math.ceil(age);
    }
}
发表于 2017-06-08 18:15:00 回复(0)
import math

while 1:
    try:
        s = raw_input()
    except:
        break
    w, y, x, n = map(float, s.split(' '))
    for i in range(int(n)):
        y = (1 - x) * (y + 1) + 21 * x
    print int(math.ceil(y))

编辑于 2017-04-29 23:59:04 回复(0)
//#include <bits/stdc++.h>
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
    double W,Y,N;
    double x;
    double ave;
    double temp;
 
    while(cin>>W>>Y>>x>>N)
    {
         ave=Y;
        for(int i=0;i<N;i++)
        {
             ave= x*21+(1-x)*(ave+1);
         
        }   
        cout<<ceil(ave)<<endl;
    }
  return 0;
}
//#include <bits/stdc++.h>
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
    double W,Y,N;
    double x;
    double ave;
    double temp;
 
    while(cin>>W>>Y>>x>>N)
    {
         ave=Y;
        for(int i=0;i<N;i++)
        {
             ave= x*21+(1-x)*(ave+1);
         
        }   
        cout<<ceil(ave)<<endl;
    }
  return 0;
}


发表于 2017-03-20 21:46:29 回复(0)
# -*- coding: utf-8 -*-
import math
import sys
def average(W, Y, x, N):
    N = int(N)
    for i in range(0, N):
        # (新入职员工年龄总和 + 老员工年龄增长一年后总和) / 公司总人数
        Y = ((21 * (W * x)) + (((Y + 1) * W) * (1 - x))) / W
    # 向上取整
    Y = math.ceil(Y)
    print(int(Y))
def main():
    try:
        while True:
            # 输入公司人数W,平均年龄Y,离职率x,统计年数N(通过一行读取)
            W, Y, x, N = map(float, sys.stdin.readline().split())
            average(W, Y, x, N)
    except:
        pass
if __name__ == "__main__":
    main()

发表于 2017-03-14 14:29:25 回复(0)
坑爹的老员工每年+1岁,坑爹的向上取整 。   真是醉了
发表于 2016-11-11 02:23:48 回复(0)
发表于 2016-09-08 21:55:46 回复(1)
import java.util.Scanner;
public class Main{
        
    public static void main(String[] args) {
       Scanner in=new Scanner(System.in);
       int W,Y0,N;
       float x,Y1;
       while(in.hasNext()){
           W=in.nextInt();
           Y0=in.nextInt();
           x=in.nextFloat();
           N=in.nextInt();
           
           Y1=Y0;
           
           for(int i=0;i<N;i++){
               Y1=calAvgAgeFloat(W,Y1,x);
           }  
           Y0=(int)Y1;
           if(Y1>Y0)
               System.out.println(Y0+1);
           else
               System.out.println(Y0);
       }

    }
    public static float calAvgAgeFloat(int W,float Y,float x){
        float sumOfAge=(W*Y-W*x*Y)+W*(1-x)*1+21*W*x;        
        float avgOfAge=sumOfAge/W;
        return avgOfAge;
    }    
}
每过一年,老员工加一岁,同时,不要每次计算向上取整,把最后一次的结果向上取整
发表于 2016-08-20 15:09:22 回复(0)
每过一年,老员工就老一岁~~~~(>_<)~~~~
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int w,n;
    double x,y;
    while(cin>>w>>y>>x>>n){
        for(int i=0;i<n;++i){
            y=(y+1)*(1-x)+21*x; 
        }
        cout<<ceil(y)<<endl;
    }
    return 0;
}

发表于 2016-08-13 15:37:55 回复(0)
//每年公司平均年龄就要加一岁,大坑。。
#include <iostream>
#include <cmath>
using namespace std;
int main(void){
    int w,n;
    double x,y;
    while(cin>>w>>y>>x>>n){
        for(int i=0;i<n;++i){
            y=(y+1)*(1-x)+21*x;  
        }
        cout<<ceil(y)<<endl;
    }
}

发表于 2016-08-11 18:37:55 回复(0)

热门推荐

通过挑战的用户

查看代码