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. 字符串的基本使用是比 ...
随机推荐
- python_frm组件
一.URL添加 from django.contrib import admin from django.urls import path,re_path from app01 import view ...
- nodejs中npm以及yarn常用指令
1.npm下载相关 1.npm install/i vue //下载vue的包 2.npm i vue --save-dev / -D //下载vue的包,并添加到开发依赖中 3.npm i //下载 ...
- linux 学习第十二天(网络会话connection、bond、ssh配置)
一.网络会话 使用 con-name 参数指定公司所使用的网络会话名称company,然后依次用ifname 参 数指定本机的网卡名称,用autoconnect no 参数设置该网络会话默认不被自动激 ...
- Linux系统查找清理磁盘大文件
本文主要介绍Linux系统磁盘使用空间不足时,如何查找大文件并进行清理的方法. 使用df-h检查一台服务器磁盘使用空间,发现磁盘已经使用了100%,其中/dev/mapper/vg_iavp-lv_r ...
- Linux多进程CS服务器简单测试
Linux多进程CS服务器简单实现 server端 多进程实现多用户连接,即每个用户一个连接,这里仍然用server将收到的字符串转大写后返回给客户端. 代码实现 #include <stdio ...
- Home Assistant系列--之树莓派安装Samba 和 Jupyter Notebook
1.什么是Samba? Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上 ...
- python 消息队列-rabbitMQ 和 redis介绍使用
1.rabbitMQ 与ptyhon 进程queue 区别.进程queue 主要用户Python父子进程之间或者统一进程不同子进程.rabbit可以用户不同语言之前的相互交流,socket可以实现同样 ...
- 《Java核心技术36讲》阅读笔记:Exception和Error有什么区别?
1.Exception 和 Error有什么区别?运行时异常与一般异常有什么区别? Exception和Error都继承自java.lang.Throwable.在Java中只有Throwable的实 ...
- 20155215 2006-2007-2 《Java程序设计》第2周学习总结
20155215 2006-2007-2 <Java程序设计>第2周学习总结 教材学习内容总结 第三章主要讲述了JAVA程序编写中的一些基本语法.其实看了第三章之后我就感觉到,C语言不愧是 ...
- 20155222 2016-2017-2 《Java程序设计》实验三
20155222 2016-2017-2 <Java程序设计>实验三 1 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单 ...