首页 > 试题广场 >

员工考勤记录

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

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

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

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


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

输入

3

输出

7
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(1 + n + n * (n - 1) / 2);
    }
}
发表于 2019-07-02 16:42:10 回复(0)
高中数学
#include<bits/stdc++.h>
using namespace std;
int n;
int main() {
    scanf("%d", &n);
    printf("%d\n", n + (n * (n - 1) >> 1) + 1);
}

发表于 2019-10-06 11:32:36 回复(0)
这道题其实本意要考的是动态规划
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int  n = sc.nextInt();
        System.out.print(result(n));
    }
    private static int result(int n){
        if(n ==1){
            return 2;
        }
        int[] table = new int[n+1];
        table[1] = 2;
        for(int i = 2 ; i<=n ; i++){
            table[i] = table[i-1]+i;
        }
        return table[n];
    }
}

发表于 2022-03-05 20:05:01 回复(0)
Python:
n = int(input())
print(int(1+n+(n*(n-1))/2) if n>0 else 0)

C:
#include<stdio.h>
int main(){
    int n,i,j,m=0;
    scanf("%d",&n);
    if(n>0) m = 1+n+(n*(n-1))/2;
    printf("%d",m);
}


发表于 2020-04-07 00:14:08 回复(0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

//总结目前牛客问题 第一,没有循环输入问题, 第二 有循环输入问题, 第三 输入有多余空格问题 ,第四 中间插入多余空行问题 ....
namespace Test0001
{
    class Program
    {
        public static void Main(string[] args)
        {
            string line;
            while (!string.IsNullOrEmpty(line = Console.ReadLine())) Func(line);
        }
       
        public static void Func(string line)
        {
            int n = int.Parse(line);
            Console.WriteLine(1 + n + n * (n - 1) / 2);
        }
    }
}
高中数学问题
把问题转换一下,就是 有n天 _ _ _ _..._ n个空位
第一种情况无缺勤 就是 p p p p p p...p 只有1种情况
第二种 缺勤一天  由于 _ _ _..._ 有n个空位都可以是缺勤位置 所以有n种情况
第三种 缺勤两天 在 _ _ _..._ n个位置中 选择2个位置缺勤 无顺序 所以是C(n,2) = n*(n-1)/2
所以 S = 1 + n + n*(n-1)/2 一步出来




发表于 2019-12-06 14:40:56 回复(0)
// 排列组合 
#include <iostream>
using namespace std;
int main()
{
    int x;
    cin >> x;
    if(x%5)
        cout << x/5+1<< endl;
    else
        cout << x/5 << endl;
    return 0;
}

发表于 2019-11-01 16:08:49 回复(0)
#include <bits/stdc++.h>
using  namespace std;
typedef  long long ll;
// 答案就是 c(N,1) + c(N,2) + 1
// c(m,n)

int c(const int m, int n) {
    if ((m>>1) < n) {
        n = m - n;
    }
        ll mm = 1;
        for (int i=0; i<n; ++i) {
            mm *= (m-i);
        }
        ll nn = 1;
        for (int i=1; i<=n; ++i) {
            nn *= i;
        }
        return mm/nn;
}
int main() {
    int m = 0;
    while (cin >> m) {
        int ant = 1;
        for (int i=1; i<=2; ++i) {
            ant += c(m,i);
        }
        cout << ant << endl;
    }
}


发表于 2019-10-17 19:57:11 回复(1)
#include<bits/stdc++.h>

using namespace std;
//排列组合函数计算 C(m,n) 
int pailie(int m,int n){
    long long sum = 1LL;
    for(int i=0;i<m;i++){
        sum = sum * (n-i);
        sum = sum / (i+1);
    }
    return sum;
}

int main(){
    int N;
    cin >> N;
    cout << pailie(0,N)+pailie(1,N)+pailie(2,N) << endl;
    return 0;
}

发表于 2019-09-16 15:31:08 回复(0)
这题其实动手写几个例子知道规律了。意思就是
在N个P中,放置至多两个A,有几种方法
1.一个A都不放: 1种
2.只放一个A:N种
3.放2个A:
  3.1 绑着放:N-1种
  3.2 分开放:(N-2) + (N-3) + (N-4) + (N-5) + .. + 2 + 1
  等差数列: s = 1 + 2 + .. + N - 2 = (N - 2)(1 + (N - 2))/2 = (N - 2) * (N - 1) / 2

所以结果为:1 + N + (N - 1) + (N - 2) * (N - 1) / 2 = 2*N +(N - 2) * (N - 1) / 2(N = 2时不满足,为4)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = Integer.parseInt(input.nextLine());
        if (n == 2)
            System.out.println(4);
        else
            System.out.println(2 * n + (n - 2) * (n - 1) / 2);
    }
}



发表于 2019-09-10 21:04:36 回复(0)
思路:为C(n,2)+C(n,1)+C(n,0)=n*(n-1)/2+n+1;之前可能会想到判断从0到2的n次方,看为1的位数小于等于2的个数,但实际上位数很大时,过于复杂通不过,我遇到过50次方的就不行。
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
int ***(int );
int main()
{
    int n;
    while(cin>>n)
    {
        int t=***(n);
        int res=t+n+1;
        cout<<res<<endl;
    }
    return 0;
}
int ***(int a)
{
    return (a)*(a-1)/2;
}
发表于 2019-08-23 10:21:46 回复(0)
//可以选择缺勤0、1、2天,对应种类分别为C(0,n)、C(1,n),C(2,n)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int count=1;//0天
        count+=n;//1天
        count+=n*(n-1)/2;//2天
        cout<<count<<endl;
    }
    return 0;
}
发表于 2019-08-12 10:10:59 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution7_员工考勤记录 {

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bf.readLine());
        /**
         * 分三种情况:
         * 1.一天不休
         * 2.休息一天
         * 3.休息两天
         *   1.两天连休
         *   2.两天分开休息
         */
        int sum = 1;//1.情况1
        sum += n;//2.休息一天,往 n-1 个 PPP...PPP中插入一个A n-1+1
        sum += n - 1;//3.1两天连休
        sum += (n - 1) * (n - 2) / 2;//3.2两天分开休
        System.out.println(sum);
    }
}
发表于 2019-08-09 21:40:19 回复(0)
#include <bits/stdc++.h> using namespace std; int main(){     int n;     while(cin>>n)         cout<<1+n+n*(n-1)/2<<endl;     return 0; }

编辑于 2019-07-16 01:18:09 回复(0)
/**  * n 的阶乘  */ public static int factorial(int n) { if (n > 1) { return n * factorial(n - 1);  } return 1; } /**  * Cmn 组合数  * m > n  * m! / n! * (m - n)!  */ public static int compose(int m, int n) { return m >= n? factorial(m) / (factorial(m - n) * factorial(n)) : 0; } /**  * Amn 排列数  * m > n  * m! / (m-n)!  */ public static int arrange(int m, int n) { return m >= n ? factorial(m) / factorial(m - n) : 0; }

发表于 2018-12-04 22:06:26 回复(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)
package main

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scan(&n)
    ans:=0
    var dfs func(int,int)
    dfs=func(tot int,idx int){
        if tot<=1{
            ans++
        }
        if tot>=2{
            if tot==2{
                ans++
            }
            return
        }
        for i:=idx;i<n;i++{
            tot++
            dfs(tot,i+1)
            tot--
        }
    }
    dfs(0,0)
    fmt.Print(ans)
}

发表于 2023-03-20 23:43:04 回复(0)
#include<iostream>
int main()
{
    int N;
    while(std::cin>>N)
    {
        std::cout<<1+N+N*(N-1)/2<<std::endl;

    }
    return 0;
}

发表于 2020-09-18 13:24:20 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        // String str = sc.nextLine();
        // String[] strmun = str.trim().split(" ");

        int num = sc.nextInt();
        
        
        System.out.println((num*num+num)/2+1);
        
        
        

    }

}
发表于 2020-05-31 16:44:15 回复(0)
/**
 * @author like
 * @version 1.0
 * @date 2020/5/20 14:41
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println((n+1)*n/2+1);
    }
}

发表于 2020-05-21 11:23:01 回复(0)
#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    cout<<(n*(n-1)/2+n+1);
    return 0;
}

发表于 2020-05-12 13:12:50 回复(0)