首页 > 试题广场 >

编程题2

[编程题]编程题2
  • 热度指数:182 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解

有n个房间,现在i号房间里的人需要被重新分配,分配的规则是这样的:先让i号房间里的人全都出来,接下来按照 i+1, i+2, i+3, ... 的顺序依此往这些房间里放一个人,n号房间的的下一个房间是1号房间,直到所有的人都被重新分配。

现在告诉你分配完后每个房间的人数以及最后一个人被分配的房间号x,你需要求出分配前每个房间的人数。数据保证一定有解,若有多解输出任意一个解。


输入描述:
第一行两个整数n, x (2<=n<=10^5, 1<=x<=n),代表房间房间数量以及最后一个人被分配的房间号;
第二行n个整数 a_i(0<=a_i<=10^9) ,代表每个房间分配后的人数。


输出描述:
输出n个整数,代表每个房间分配前的人数。
示例1

输入

3 1
6 5 1

输出

4 4 4
AC代码
开始timeout了,后来通过找到最小数,所有房间人数都先减去这个最小数,
然后遍历一遍即可。
 public static void computer() {
        Scanner scanner = new Scanner(System.in);
        int n, x;
        n = scanner.nextInt();
        x = scanner.nextInt();
        x--;
        long[] houses = new long[n];
        for(int i = 0; i < n; i++) {
            houses[i] = scanner.nextInt();
        }
        long min=houses[0];
        for(longhouse : houses) {
            if(house < min) {
                min = house;
            }
        }
        for(int i = 0; i < houses.length; i++) {
            houses[i]-=min;
        }
        long count=min*n;
        while(true) {
            if(houses[x]==0) {
                houses[x]=count;
                break;
            } else{
                houses[x]--;
                count++;
            }
 
            if(x <= 0) {
                x=houses.length-1;
            }else{
                x--;
            }
        }
        for(longhouse : houses) {
            System.out.print(house+" ");
        }
 
        }

编辑于 2019-02-26 17:20:25 回复(2)

import java.util.*;
import java.text.DecimalFormat;

public class Main {

    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        long[] person = new long[n];
        long outperson = 0l;
        long min = Long.MAX_VALUE;
        
        for(int i=0;i< n;i++ ) {
            person[i] = sc.nextLong();
            if(person[i] < min) {
                min = person[i];
            }
        }
        k = k-1;
    
        
        for(int i=0;i< n;i++ ) {
            person[i] = person[i] - min;
            outperson = outperson + min;
        }
        
    
        
        while(person[k] > 0 ) {
            person[k] = person[k] -1;
            outperson ++;
            
            k = k-1;
            
            if(k == -1) {
                k = n-1;
            }
            
            
        }
        
        person[k] = outperson;
        
        for(int i = 0;i< person.length;i++) {
            if(i != n-1) {
                System.out.print(new DecimalFormat("0").format(person[i]) + " ");
            }else {
                System.out.print(new DecimalFormat("0").format(person[i]));
            }
        }
        
        sc.close();
    }
    
    
}

发表于 2020-08-08 21:19:54 回复(0)
package ByteDanceInterview;

import java.util.Scanner;

public class Room {

    public static void main(String[] args) {
        int n, k;
        Scanner cin = new Scanner(System.in);
        n = cin.nextInt();
        k = cin.nextInt();
        int a[] = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = cin.nextInt();
        }
        // 出去了多少人
        int outPerson = 0;

        //逆推
        while(a[k]!=0) {
            a[k]--;
            outPerson++;

            //k为房间号 
            k = (k - 1) % n;
            if (k == 0)
                k = n;

        }

        a[k] = outPerson;

        for(int i=1;i<=n;i++) {
            if(i!=n) {
                System.out.print(a[i]+" ");
            }else {
                System.out.print(a[i]);
            }
        }


    }

}

通过率80%

编辑于 2018-11-13 20:29:45 回复(0)
1、所有的房间人数  减去  人数最少的房间人数,产生至少一个空房间。
2、x房间左边最近的空房间为i号房间,注意环绕,x房间为空即x=i。
3、i号房间到x号房间,每个房间再减去1人,为该房间原始人数,注意环绕i-->x。
4、i号房间人数为总人数   减去  状态3中房间总人数。

python通过率50%,,,,,,
发表于 2018-10-02 17:03:39 回复(0)
1. i<x<=n 且i房里最终人数为0 i+1到x房的最终人数-1,其余房不变。 2. 0<=x<i 且i房里最终人数为0 0-x,i+1到n房的最终人数-1,其余房不变。 3.x=i 且i房里最终人数为0→i房为0人,不变。 4. i房里最终人数为m,先所有房间人数-m,再按照123的选项判断。
发表于 2018-01-27 00:27:14 回复(0)