杭电1592Half of and a Half(大数)

本文介绍了一道关于大数处理的编程题目,通过逆向思维和数据结构的应用,解决了不断分配一半加半个巧克力直至归零的问题。代码实现采用C++,展示了如何利用数组模拟大整数运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Half of and a Half

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1023    Accepted Submission(s): 453


Problem Description
Gardon bought many many chocolates from the A Chocolate Market (ACM). When he was on the way to meet Angel, he met Speakless by accident.
"Ah, so many delicious chocolates! I'll get half of them and a half!" Speakless said.
Gardon went on his way, but soon he met YZG1984 by accident....
"Ah, so many delicious chocolates! I'll get half of them and a half!" YZG1984 said.
Gardon went on his way, but soon he met Doramon by accident....
"Ah, so many delicious chocolates! I'll get half of them and a half!" Doramon said.
Gardon went on his way, but soon he met JGShining by accident....
"Ah, so many delicious chocolates! I'll get half of them and a half!" JGShining said.
.
.
.
After had had met N people , Gardon finally met Angel. He gave her half of the rest and a half, then Gardon have none for himself. Could you tell how many chocolates did he bought from ACM?
 

Input
Input contains many test cases.
Each case have a integer N, represents the number of people Gardon met except Angel. N will never exceed 1000;
 

Output
For every N inputed, tell how many chocolates Gardon had at first.
 

Sample Input
2
 
Sample Output
7


大数题,题意是见一个人分总数的一半+0.5,最后正好没有,问总共有多少!


要想最后一个人正好分完,倒数第二个人分完后应剩下1,这样一半+0.5才正好为0;


所以大数u!


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int a[1100];
}s[1010];
int main()
{
    int i,j,t,n;
    s[0].a[0]=1;
    for(i=1;i<=1000;i++)
    {
        int r=0;
        for(j=0;j<1000;j++)
        {
            s[i].a[j]=s[i-1].a[j]*2+r;

            r=s[i].a[j]/10;

            s[i].a[j]%=10;

        }
        t=0;
        while(s[i].a[t]+1>9)
        {
            s[i].a[t]=0;
            t++;
        }
        s[i].a[t]+=1;
    }
    while(scanf("%d",&n)!=EOF)
    {
        for(i=999;i>=0,s[n].a[i]==0;i--);
        for(j=i;j>=0;j--)
            printf("%d",s[n].a[j]);
        printf("\n");

    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值