Split the Number(思维)
You are given an integer x. Your task is to split the number x into exactly n strictly positive integers such that the difference between the largest and smallest integer among them is as minimal as possible. Can you?
The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.
Each test case consists of a single line containing two integers x and n (1 ≤ x ≤ 109, 1 ≤ n ≤ 1000), as described in the statement above.
For each test case, print a single line containing n space-separated integers sorted in a non-decreasing order. If there is no answer, print - 1.
1
5 3
1 2 2
The strictly positive integers are the set defined as: . That is, all the integers that are strictly greater than zero: .
题目意思:对于T组输入输出,给你一个n将其分为k份,要求分成的份最大值和最小值相差最小。
解题思路:这道题很简单,我看了队友写的代码后发现这个题还挺有意思的,这里给出解释。
例如10分成4份,用10直接去整除4得到的是2,我们可以这样理解平均每一份是ans 2,即n/k得到的是一种平均的分配,但是这样还没有全部分完n,还会有余数m=n%k,因为m必然是比k小的数,也就可以理解为有m个分数需要在原来的ans上再加一个上1,这样得到的分配能够保证最大值和最小值相差最小。
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int t,ans,m,i,k,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
if(n<k)
{
printf("-1\n");
continue;
}
ans=n/k;
m=n-ans*k;
for(i=;i<k-m;i++)
{
printf("%d ",ans);
}
for(i=k-m;i<k;i++)
{ if(i==k-)
{
printf("%d\n",ans+);
}
else
{
printf("%d ",ans+);
}
}
}
return ;
}
Split the Number(思维)的更多相关文章
- Codeforces C. Split a Number(贪心大数运算)
题目描述: time limit per test 2 seconds memory limit per test 512 megabytes input standard input output ...
- Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心)
B. Split a Number time limit per test2 seconds memory limit per test512 megabytes inputstandard inpu ...
- B. Split a Number(字符串加法)
Dima worked all day and wrote down on a long paper strip his favorite number nn consisting of ll dig ...
- Codeforces Round #567 (Div. 2) B. Split a Number
Split a Number time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- Codeforces - 1181B - Split a Number - 贪心
https://codeforces.com/contest/1181/problem/B 从中间拆开然后用大数搞一波. 当时没想清楚奇偶是怎么弄,其实都可以,奇数长度字符串的中心就在len/2,偶数 ...
- 2019牛客多校训练第四场K.number(思维)
题目传送门 题意: 输入一个只包含数字的字符串,求出是300的倍数的子串的个数(不同位置的0.00.000等都算,并考虑前导零的情况). sample input: 600 1230003210132 ...
- ZOJ - 2132:The Most Frequent Number(思维题)
pro:给定N个数的数组a[],其中一个数X的出现次数大于N/2,求X,空间很小. sol:不能用保存数组,考虑其他做法. 由于出现次数较多,我们维护一个栈,栈中的数字相同,所以我们记录栈的元素和个数 ...
- index.js:13 Uncaught TypeError: Cannot read property 'split' of undefined
使用 webpack 编译 Vue 项目时出现报错: index.js:13 Uncaught TypeError: Cannot read property 'split' of undefined ...
- split 过滤空的元素
命令形式: split(str='',number=string.count(str))[n] str 分隔符 number 切分几次,[n] 获取第几个值. 1.如果切分的可迭代对象中包含空元素的解 ...
随机推荐
- lnmp+coreseek实现站内全文检索(安装篇)
coreseek安装与简单实用 安装环境 系统环境 centos7.2 1核2G 软件环境 coreseek-3.2.14 lnmp1.5 安装mmseg 更新依赖包和安装编译环境 yum -y in ...
- nginx 开启phpinfo
在nginx配置文件中加 location / { //如果是资源文件,则不走phpinfo模式 if (!-e $request_filename){ ewrite ^/(.*)$ /index.p ...
- IAR新建MSP430工程
一.在IAR官网下载IAR for MSP430 软件 https://www.iar.com/iar-embedded-workbench/#!?architecture= 选择MSP430,然后 ...
- CALL TRANSACTION
概要 SUBMITと違い.トランザクションコードで呼び出すのが特徴. アドオンからの伝票照会やバッチインプットによるSAPへのデータ登録/更新処理にも利用される. なお.呼び出された側から呼び出し元へ ...
- Why mobile web apps are slow
http://sealedabstract.com/rants/why-mobile-web-apps-are-slow/ I’ve had an unusual number of interest ...
- Oracle入门第二天(下)——单行函数
一.概述 以下内容完整参阅,参考官方文档函数手册部分:https://docs.oracle.com/cd/E11882_01/nav/portal_5.htm 离线chm手册英文版:链接:https ...
- 20155202 实验四 Android开发基础
20155202 实验四 Android开发基础 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: 3.掌握An ...
- 201555334 实验一:Java开发环境的熟悉 总结
201555334 实验一:Java开发环境的熟悉 一.实验目的: 使用JDK编译.运行简单的Java程序: 使用Idea软件 编辑.编译.运行.调试Java程序. 二.实验内容: 编程实现让用户输入 ...
- [并发并行]_[线程模型]_[Pthread线程使用模型之二 工作组work crew]
Pthread线程使用模型之二工作组(Work crew) 场景 1.一些耗时的任务,比如分析多个类型的数据, 是独立的任务, 并不像 pipeline那样有序的依赖关系, 这时候pipeline就显 ...
- android studio提交到开源git时出现:fatal: refusing to merge unrelated histories的解决办法
创建本地库和fetch远程分支这些前面的步骤这里略过.可以自行百度. 解决办法: 1.cmd进入项目的根目录. 2.执行下面的命令:git pull origin master --allow-unr ...