Codeforces Round #667 (Div. 3) 1409C

题目链接

C. Yet Another Array Restoration

We have a secret array. You don’t know this array and you have to restore it. However, you know some facts about this array:

The array consists of n distinct positive (greater than 0) integers.
The array contains two elements x and y (these elements are known for you) such that x<y.
If you sort the array in increasing order (such that a1<a2<…<an), differences between all adjacent (consecutive) elements are equal (i.e. a2−a1=a3−a2=…=an−an−1).
It can be proven that such an array always exists under the constraints given below.

Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize max(a1,a2,…,an).

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.

The only line of the test case contains three integers n, x and y (2≤n≤50; 1≤x<y≤50) — the length of the array and two elements that are present in the array, respectively.

Output
For each test case, print the answer: n integers a1,a2,…,an (1≤ai≤109), where ai is the i-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn’t matter).

It can be proven that such an array always exists under the given constraints.

Example
inputCopy
5
2 1 49
5 20 50
6 20 50
5 3 8
9 13 22
outputCopy
1 49
20 40 30 50 10
26 32 20 38 44 50
8 23 18 13 3
1 10 13 4 19 22 25 16 7

题意

给定t组测试样例,每组测试样例有3个数,n代表数组的长度,x,y代表数组中的两个数(x<y),要求输出一个长度为n的数组,输出的数组是一个等差数列(数组中的数字全部大于0),使得数组中最大的值最小

思路

要想最大的值最小,最好的情况就是y作为数组中最大的值输出
那么我们就可以尽可能的在x和y中间找出所有的数,如果找出的数的个数小于n,那么就从x逐渐变小,之后再从y逐渐不大
主要思路:枚举

AC代码


import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while (t-->0){
            int n=sc.nextInt();
            int x=sc.nextInt();
            int y=sc.nextInt();
            if (n==2){      //如果数组长度为2,直接输出
                System.out.println(x+" "+y);
                continue;
            }
            int cha=y-x;  //x和y的差值
            int shu=n-1;  //x和y中间最多可以相间隔的距离
            int zhi;  //等差值
            while (true){
                if (cha%shu==0){   //x和y之间存在的数
                    zhi=cha/shu;
                    break;
                }
                shu--;
            }
            if (shu==n-1){  //x和y之间存在所有的数,满足长度为n的数组
                int count=0;
                while (count!=n){
                    System.out.print(x+" ");
                    x+=zhi;
                    count++;
                }
            }else{
                int count=0;
                int xx=x;
                while (xx<=y){//先将x,y中间的数输出
                    System.out.print(xx+" ");
                    xx+=zhi;
                    count++;
                }
                x-=zhi;
                while (count<n&&x>0){//如果输出的数不够,就继续往小变换,直到小于0为止
                    System.out.print(x+" ");
                    x-=zhi;
                    count++;
                }
                y+=zhi;
                while (count<n){//如果还是不够,就输出大于y的数
                    System.out.print(y+" ");
                    y+=zhi;
                    count++;
                }
                System.out.println();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值