从今年 3 月末开始,请实现一个算法,可以计算出第 N 年后公司员工的平均年龄。(最后结果向上取整)。
数据范围: , , ,
输入W Y x N
输出第N年后的平均年龄
5 5 0.2 3
15
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()); } }
平均年龄不好算 但是由于人数是不变的 只需要算出总的年龄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; }
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(); } }
递归的代码!! #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; } }
// 题目意思确实没说清楚 // 需要注意每年老员工都会增加一岁 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)); } } }
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(); } }
//#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; }
# -*- 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()