首页 > 试题广场 >

列表补全

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

在商城的某个位置有一个商品列表,该列表是由L1、L2两个子列表拼接而成。当用户浏览并翻页时,需要从列表L1、L2中获取商品进行展示。展示规则如下:

1. 用户可以进行多次翻页,用offset表示用户在之前页面已经浏览的商品数量,比如offset为4,表示用户已经看了4个商品

2. n表示当前页面需要展示的商品数量

3. 展示商品时首先使用列表L1,如果列表L1长度不够,再从列表L2中选取商品

4. 从列表L2中补全商品时,也可能存在数量不足的情况

请根据上述规则,计算列表L1和L2中哪些商品在当前页面被展示了


输入描述:
每个测试输入包含1个测试用例,包含四个整数,分别表示偏移量offset、元素数量n,列表L1的长度l1,列表L2的长度l2。


输出描述:
在一行内输出四个整数分别表示L1和L2的区间start1,end1,start2,end2,每个数字之间有一个空格。
注意,区间段使用半开半闭区间表示,即包含起点,不包含终点。如果某个列表的区间为空,使用[0, 0)表示,如果某个列表被跳过,使用[len, len)表示,len表示列表的长度。
示例1

输入

2 4 4 4
1 2 4 4
4 1 3 3

输出

2 4 0 2
1 3 0 0
3 3 1 2
offset这个点,隐藏着如果大于l1+l2意味着已经把l1和l2跳过了。其他理一理感觉还行。
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int offset = in.nextInt();
            int n = in.nextInt();
            int l1 = in.nextInt();
            int l2 = in.nextInt();
            // 根据offset计算起点
            int l3 = l1 + l2;
            // int plus = offset % l3;
            int plus = offset;
            // 判断偏移是否已经大于一遍
            if (plus < l3) {
                // 判断偏移是否超过l1
                if (plus < l1) {
                    // 判断l1剩余空间是否够n
                    if (l1 - plus + 1 >= n) {
                        System.out.print(plus + " " + (plus + n - 1 + 1) + " "  + "0 0" );
                    } else {
                        System.out.print(plus + " " + l1);
                        n -= (l1 - 1 - plus + 1);
                        // 判断l2剩余空间是否够n'
                        if (l2 >= n) {
                            System.out.print(" 0 " + (n - 1 + 1));
                        } else {
                            System.out.print(" 0 " + l2);
                        }
                    }
                } else {
                    // 同理
                    System.out.print(l1 + " " + l1 + " ");
                    plus -= l1;
                    if (l2 - plus + 1 >= n) {
                        System.out.print(plus + " " + (plus + n - 1 + 1));
                    } else {
                        System.out.print(plus + " " + l2);
                    }
                }
            } else {
                System.out.print(l1 + " " + l1 + " " + l2 + " " + l2);
            }
            System.out.println();
        }
    }
}


发表于 2023-11-28 22:39:14 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
       
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
        int start1=0,end1=0,start2=0,end2=0;
            int offset=in.nextInt();
            int n=in.nextInt();
            int l1=in.nextInt();
            int l2=in.nextInt();
            start1=offset;
            if(start1<=l1)
            {
                if(n+start1<=l1)
                {
                    end1=start1+n;
                }
                else
                {
                    end1=l1;
                    start2=0;
                   end2=n-(end1-start1);
                    if(end2>l2)
                    end2=l2;
                }
            }
            else
            {
                start1=l1;
                end1=l1;
                start2=offset-l1;
                if(start2>l2)
                {
                    start2=l2;end2=l2;
                }
                else{
                end2=start2+n;
                if(end2>l2)
                end2=l2;
                }
               
            }
            System.out.print(start1+" "+end1+" "+start2+" "+end2+"\n");
        }
    }
}
//多看一眼就会爆炸
发表于 2023-07-07 17:35:37 回复(0)
import java.util.*;

public class Main{
    
    public static void main(String[] args){
    
    	Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int offset = sc.nextInt();
            int n = sc.nextInt();
            int l1 = sc.nextInt(), l2 = sc.nextInt();

            int[] ans = solution(offset,n,l1,l2);
            StringBuilder ss = new StringBuilder();
            for(int i = 0; i < ans.length; i++){
                if(i != ans.length - 1) ss.append(ans[i]).append(' ');
                else ss.append(ans[i]);
            }
            System.out.println(ss.toString());
        }
    }
    
    // 2 4 4 4 ==> 2 4 0 2
	// 1 2 4 4 ==> 1 3 0 0
	// 4 1 3 3 ==> 3 3 1 2
    private static int[] solution(int offset, int n, int l1, int l2){
    
    	int[] ans = new int[4];
    	// 如果之前页面浏览的商品数量 offset 大于后面两列的数量,则表示 l1 和 l2 都需要表示浏览过的商品
    	// 如 4,1(可以是任意值),2,1
    	if(offset >= l1 + l2){
    		ans[0] = ans[1] = l1;
    		ans[2] = ans[3] = l2;
    		// 如果 offset >= l1 表示 l1 需要全部用来表示浏览过的,而 l2 用来表示全部或部分当前页展示的商品数量
    		// 如 3,2,2,2(l2 只能展示部分)  或 3,4,2,3(l2 展示全部)
    	} else if(offset >= l1){
    		ans[0] = ans[1] = l1;
    		ans[2] = offset - l1;
    		ans[3] = Math.min(l2,n + ans[2]);
    		// 如果 offset < l1 表示 l1 部分用来展示之前浏览的,剩下可以展示部分或全部
    		// 如 1,2,3,2(l1 展示全部) 或 1,3,2,3(l1 展示部分)
    	} else if(offset < l1){
    		ans[0] = offset;
    		ans[1] = Math.min(offset + n, l1);
    		ans[2] = 0;
    		ans[3] = offset + n <= l1 ? 0 : Math.min(offset + n - l1,l2);
    	}
    	return ans;
    }
}

发表于 2020-08-02 11:46:55 回复(0)

public class Demo {
     public static void ShowList(int[][] input){
            if(input.length == 0) return;
            int start1=0,end1 = 0,start2 = 0,end2 = 0;
            for(int i =0;i<input.length;i++){
                if(input[i][0]<input[i][2]&&input[i][0]+input[i][1]<input[i][2]){
                    start1 = input[i][0];
                    end1 = start1+input[i][1];
                    start2 = 0;
                    end2 = 0;
                }else if(input[i][0]<input[i][2]&&input[i][0]+input[i][1]>input[i][2]){
                    start1 = input[i][0];
                    end1 = input[i][2];
                    start2 = 0;
                    end2 = start1+input[i][1]-input[i][2];
                }else{
                    start1 = input[i][2];
                    end1 = input[i][2];
                    start2 = input[i][0]-input[i][2];
                    end2 = start2+input[i][1];
                    if(end2>input[i][3])
                        end2 = input[i][3];
                }
                System.out.println(start1+" "+ end1+" "+start2+" "+end2);
            }
        }
    public static void main(String[] args) {
        int[][] A = {{2, 4, 4, 4},
                {1, 2, 4, 4},
                {4, 1, 3, 3}};
        ShowList(A);
    }
}
发表于 2020-08-02 09:40:28 回复(0)