练习codeforces1790B. Taisia and Dice
题目大意
B. Taisia and Dice
time limit per test1 second
memory limit per test256 megabytes
Taisia has 𝑛 six-sided dice. Each face of the die is marked with a number from 1 to 6, each number from 1 to 6 is used once.
Taisia rolls all 𝑛 dice at the same time and gets a sequence of values 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤6), where 𝑎𝑖 is the value on the upper face of the 𝑖-th dice. The sum of this sequence is equal to 𝑠.
Suddenly, Taisia's pet cat steals exactly one dice with maximum value 𝑎𝑖 and calculates the sum of the values on the remaining 𝑛−1 dice, which is equal to 𝑟.
You only know the number of dice 𝑛 and the values of 𝑠, 𝑟. Restore a possible sequence 𝑎 that fulfills the constraints.
Input
The first line contains the integer 𝑡 (1≤𝑡≤1000) — the number of testcases.
Each testcase is given on a separate line and contains three integers 𝑛, 𝑠, 𝑟 (2≤𝑛≤50, 1≤𝑟<𝑠≤300).
It is guaranteed that a solution exists.
Output
For each testcase, print: 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 in any order. It is guaranteed that such sequence exists.
If there are multiple solutions, print any.
题目大意
原有n个面值不同的骰子,现被猫偷走面值最大的骰子,现在知晓骰子数量n,所有骰子的值的总和s以及余下n-1个骰子的总值r的情况下,求所有骰子的值
题目分析
既然已经知道n个骰子的值和n-1个骰子的值,那么被偷走的骰子的值一定等于s-r
那么就是值的分配问题,把n-1个骰子的总值r分配给n-1个骰子,且每个骰子值不能超过被偷走的最大的骰子的值
点击查看代码
int dice[55];
for(int i = 0; i < n - 1; i++){
if (i < (r % (n - 1)))
dice[i] = r / (n - 1) + 1;
else
dice[i] = r / (n - 1);
}
完整代码
点击查看代码
#include <stdio.h>
int main(){
int t;
scanf("%d", &t);
while(t--){
int n,s,r;
scanf("%d%d%d", &n, &s, &r);
int dice[55];
for(int i = 0; i < n - 1; i++){
if (i < (r % (n - 1)))
dice[i] = r / (n - 1) + 1;
else
dice[i] = r / (n - 1);
}
dice[n - 1] = s - r; //丢失的骰子的面值
for(int i = 0; i < n; i++)
printf("%d ", dice[i]);
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号