C语言编程题:分糖果,如图。没有思路T^T ,求大神

如题所述

只有一个简单的思路,一起讨论一下:
第1个小孩给他一个(谁让他抢在前面呢:)
后面的小孩依次按规矩来,看应该给几个,可爱值高呢,就加一块,否则就减一块。
如果发现减到零了,那就倒回去,前面的人补一块。当然,不是全部补,只要补发到可爱值较高的地方就可以了。实际只需要计算一个总数,所以可以预先算好,直接加到总数中就行了。

开始时,第1个小孩1块糖,待补发的数量=0
第2个小孩先给2块糖,由于可爱值开始变高,待补发的数量=1
每3个小孩先给1块糖,由于可爱值变低,待补发的数量+1。
第4个小孩可爱值变低,待补发总数+1,照说应该给0块糖,当然不行,补发一块,前面的也都补一块,补3块。这个待补发数量还不能归零。当前第4个小孩的糖数应该修正成1。
第5个小孩给2块糖,由于可爱值开始变高,补发数量归零=1。
1+2+1+0+(3)+2=9

大体的思路应该就是这个样子。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-30
#include <stdio.h>

typedef struct child
{
int value; //可爱值
int number; //糖果数量
}Child;

int Check(Child tmp_child[],int x)
{
int j,k,Count = 0;

for(j=0; j < x - 1; j++) // 每次和后面一个孩子进行比较
{
if(tmp_child[j].value < tmp_child[j+1].value){

tmp_child[j+1].number += 1;

}else if(tmp_child[j].value > tmp_child[j+1].value){
tmp_child[j].number += 1;
}else{
printf("Nothing we can do\n");
}
}
return Count;
}
int main()
{
printf("Please input the number of Childs:");
int n = 0;
int count= 0;
scanf("%d",&n);

Child t_child[n];
int i = 0;
for(; i < n; i++) //初始化
{
scanf("%d",&t_child[i].value);
t_child[i].number = 1;
}
count = Check(t_child,n);
if(count <= n)
{
printf("Need to be check the function!!\n");
}
printf("The number is less than: %d\n",count);
return 0;
}

调试结果:
Please input the number of Childs:5
3
40
2
1
10
The number is less than: 9
第2个回答  2014-12-30
找到可爱值最低的小孩子,给他分配1个糖果,两边的分配2个糖果,重复操作
第3个回答  2014-12-30
动态规划。。。本回答被网友采纳
相似回答