Friends and Cookies(思维)
Abood's birthday has come, and his n friends are aligned in a single line from 1 to n, waiting for their cookies, Abood has x cookies to give to his friends.
Here is an example to understand how Abood gives away the cookies. Suppose Abood has 4 friends and x cookies, then Abood will do the following:
- Give a cookie to the 1st friend.
- Give a cookie to the 2nd friend.
- Give a cookie to the 3rd friend.
- Give a cookie to the 4th friend.
- Give a cookie to the 3rd friend.
- Give a cookie to the 2nd friend.
- Give a cookie to the 1st friend.
- Give a cookie to the 2nd friend.
- And so on until all the x cookies are given away.
Your task is to find how many cookies each friend will get. 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 ≤ 1018, 1 ≤ n ≤ 1000), in which x is the number of cookies Abood has, and n is the number of his friends.
For each test case, print a single line containing n space-separated integers a1, ..., an, in which ai represents how many cookies the ith friend got.
1
5 3
2 2 1 解题思路:我将蛋糕的分配方式模拟一下
- - - - - - -
- - - - - -
- - - - - -
- - - - - -
- - - - - -
- - -
我们可以看到的是:第一行每一个人都有,然后开始的奇数个前N-1个有,偶数个后N-1有,最后一行再判断是第奇数个还是偶数个就可以得出结果。
注意的是:没跑完第一行的情况需要特判一下,跑完第一行但是m<n也需要特判一下。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
using namespace std;
int main()
{
int t;
LL i,j,a[],m,n,x,y;
scanf("%d",&t);
while(t--)
{
memset(a,,sizeof(a));
scanf("%lld%lld",&m,&n);
if(n==)///特判一下当n为1的时候
{
printf("%lld\n",m);
}
else if(m<n)
{
for(i=;i<m;i++)
{
a[i]=;
}
for(i=;i<n;i++)///只有前m个人有蛋糕,后面的人没有了蛋糕
{
if(i==n-)
{
printf("%lld\n",a[i]);
}
else
{
printf("%lld ",a[i]);
}
} }
else
{
for(i=;i<n;i++)///第一行的分配
{
a[i]=;
}
m=m-n;///分配完第一行之后剩下的蛋糕数量
x=m/(n-);///x表示剩下的行数
y=m%(n-);///y表示最后一行余下的蛋糕份数
for(i=;i<n-;i++)
{
a[i]=a[i]+x;///除了第一个人和最后一个人其他的人再得到x份蛋糕
}
///下面是对第一个人和最后一个人以及最后一行的蛋糕分配
if(x%==)///行数为偶数
{
a[]=a[]+x/;
a[n-]=a[n-]+x/;
for(i=n-;i>=n--y+;i--)
{
a[i]++;
}
}
else///行数为奇数
{
a[]=a[]+x/+;
a[n-]=a[n-]+x/;
for(i=;i<=y;i++)
{
a[i]=a[i]+;
}
}
}
for(i=;i<n;i++)///输出
{
if(i==n-)
{
printf("%lld\n",a[i]);
}
else
{
printf("%lld ",a[i]);
}
}
}
return ;
}
Friends and Cookies(思维)的更多相关文章
- 官方解析Cookies和Session的区别
官方理解: Cookie机制 Cookie机制 Cookie是服务器存储在本地计算机上的小块文本,并随每个请求发送到同一服务器. IETF RFC 2965 HTTP状态管理机制是一种通用的coo ...
- 455. Assign Cookies 满足欲望 分配饼干
[抄题]: Assume you are an awesome parent and want to give your children some cookies. But, you should ...
- 1. sqlmap超详细笔记+思维导图
sqlmap思维导图: 基本操作笔记: -u #注入点 -f #指纹判别数据库类型 -b #获取数据库版本信息 -p #指定可测试的参数(?page=1&id=2 -p "page, ...
- 【面试题资源共享】一文总结最高频软件测试|sq|语句|思维发散|计算机基础|Linux|测试用例|接口测试|等技术面试题
思维发散 1.一个球, -把尺子长度是球直径的2/3,怎样测出半径?2.四枚硬币,花面朝上,每次翻转三个,几次可以将四枚硬币变为字面朝上?3. U2合唱团在1 7分钟内赶到演唱会现场问题?4.小明一家 ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- Photoshop、Illustrator思维导图笔记
半年前学习Photoshop时记得的思维导图笔记,可能不是很全,常用的基本都记下了.
- CYQ.Data 从入门到放弃ORM系列:开篇:自动化框架编程思维
前言: 随着CYQ.Data 开始回归免费使用之后,发现用户的情绪越来越激动,为了保持这持续的激动性,让我有了开源的念头. 同时,由于框架经过这5-6年来的不断演进,以前发的早期教程已经太落后了,包括 ...
- 计算机程序的思维逻辑 (8) - char的真正含义
看似简单的char 通过前两节,我们应该对字符和文本的编码和乱码有了一个清晰的认识,但前两节都是与编程语言无关的,我们还是不知道怎么在程序中处理字符和文本. 本节讨论在Java中进行字符处理的基础 - ...
- 计算机程序的思维逻辑 (29) - 剖析String
上节介绍了单个字符的封装类Character,本节介绍字符串类.字符串操作大概是计算机程序中最常见的操作了,Java中表示字符串的类是String,本节就来详细介绍String. 字符串的基本使用是比 ...
随机推荐
- Java运算符使用总结(重点:自增自减、位运算和逻辑运算)
Java运算符共包括这几种:算术运算符.比较运算符.位运算符.逻辑运算符.赋值运算符和其他运算符.(该图来自网络) 简单的运算符,就不过多介绍使用了,可自行测试.关于赋值运算,可以结合算术运算和位运算 ...
- Vue中使用webpack别名的方法
在工作中,我们经常会写出这种代码: import MHeader from '../../components/m-header/m-header' @import "../../commo ...
- 移动端H5页面解决软件键盘把页面顶起
在input失去焦点的时候加上强制页面归位 window.scroll(0,0); 上代码 <input data-component="SearchInput" type= ...
- [project X] tiny210(s5pv210)上电启动流程(BL0-BL2)
建议参考文档: S5PV210-iROM-ApplicationNote-Preliminary-20091126 S5PV210_UM_REV1.1 项目介绍参考 [project X] tiny2 ...
- ARMCC中$Super$$和$Sub$$的使用
代码: extern int $Super$$main(void); /* re-define main function */ int $Sub$$main(void) { rt_hw_interr ...
- Python面向对象总结及类与正则表达式
Python3 面向对象 一丶面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 方法:类中定义的函数. 类变 ...
- 【转】 GATK--原始数据预处理
1. 对原始下机fastq文件进行过滤和比对(mapping) 对于Illumina下机数据推荐使用bwa进行mapping. Bwa比对步骤大致如下: (1)对参考基因组构建索引: 例子:bwa i ...
- Python-dataframe合并(merge函数)
import pandas as pd import numpy as np df1=pd.DataFrame({'key':['b','b','a','c','a','a','b'],'data1' ...
- Webservice返回SoapHeader
Webservice在请求中加入自定义的SoapHeader,比较常用的场景是在SoapHeader中携带用户登陆信息,由服务端进行身份验证.今天遇到一个需求,除了在请求时要携带SoapHeader外 ...
- mini dc课堂练习补交
实验截图 实验代码 import java.util.StringTokenizer; import java.util.Stack; public class MyDC { /** * consta ...