首页 > ACM题库 > HDU-杭电 > Hdu 1592 Half of and a Half-大数[解题报告] C++
2013
12-12

Hdu 1592 Half of and a Half-大数[解题报告] C++

Half of and a Half

问题描述 :

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 contains many test cases.
Each case have a integer N, represents the number of people Gardon met except Angel. N will never exceed 1000;

输出:

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

样例输入:

2

样例输出:

7


很水的一道题!题目很优美!

大数!!!!

#include<stdio.h>

#include<string.h>
int num[1010][1010];
int main()
{
int i,n,j;
memset(num,0,sizeof(num));
num[0][1]=1;
for(i=1;i<1010;i++)
{
for(j=1;j<1010;j++)
{
if(j==1)
num[i][j]+=(num[i-1][j])*2+1;
else
num[i][j]+=num[i-1][j]*2;
}
for(j=2;j<1010;j++)
{
if(num[i][j-1]>9)
num[i][j]+=num[i][j-1]/10;
num[i][j-1]%=10;
}
}
while(scanf("%d",&n)!=EOF)
{
for(i=1010;i>=1;i--)
if(num[n][i]!=0)
{
printf("%d",num[n][i]);
break;
}
for(j=i-1;j>=1;j--)
printf("%d",num[n][j]);
printf("\n");
}
return 0;
}

转自:http://blog.csdn.net/u011518888/article/details/9890891


  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。