首页 > 试题广场 >

员工考勤记录

[编程题]员工考勤记录
  • 热度指数:8739 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

给定一个字符串来代表一个员工的考勤纪录,这个纪录仅包含以下两个字符:
'A' : Absent,缺勤
'P' : Present,到场
如果一个员工的考勤纪录中不超过两个'A'(缺勤),那么这个员工会被奖赏。

如果你作为一个员工,想在连续N天的考勤周期中获得奖赏,请问有多少种考勤的组合能够满足要求

输入描述:
考勤周期的天数N(正整数)


输出描述:
这N天里能获得奖赏的考勤组合数
示例1

输入

3

输出

7
import java.util.Scanner;
//46
public class Main {
    private static int res; 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		count(0, 0,n);
		System.out.println(res);
	}
	static void count(int a,int p,int n){
		if(a+p>=n){
			res++;
			return ;
		}
		if(a<2){
			count(a+1, p, n);
			
		}
		count(a, p+1, n);
	}
}

发表于 2019-09-14 18:38:20 回复(0)
JAVA解答,其实就是组合问题,
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int len = input.nextInt();
        if (len == 1)
            System.out.println(1);
        else if (len == 2)
            System.out.println(4);
        else{
            System.out.println(len*(len-1)/2+len+1);
        }
    }
}

编辑于 2019-08-02 19:33:35 回复(0)
import java.util.Scanner; public class Main { public static void main(String[] args){
        Scanner sc = new Scanner(System.in); int i = Integer.parseInt(sc.nextLine());  int x1 = 1; int x2 = 0; int x3 = 0;
        x2 = i;
        x3 = ((i) * (i-1)) / (2);
        System.out.println(x1+x2+x3);
    }
}

编辑于 2019-07-03 17:06:40 回复(0)