1202: 2018四川理工学院大学生ACM程序设计:The wheat of the prime minister
时间限制: 1 Sec 内存限制: 128 MB提交: 4 解决: 3
[提交][状态][讨论版][命题人:liangxingjian]
题目描述
Sissa Ben, a minister in ancient India, invented the game of chess. The king intended to reward him because of it. The minister said, “I want nothing but some wheat. Please put a grain of wheat on the first square of the chessboard, two on the second, four on the third, eight on the fourth, ..., and so on, doubling the number for each following square, and give me enough grains to cover the 64 squares of the chessboard.” The king ordered a bag of wheat brought to the palace. But when the counting began with one gain for the first square, two for the second, four for the third, and so on, the bag was emptied before the 20th square. More bags were brought but the number of grain needed for the following squares increased so rapidly that the king soon realized that he was not able to keep his promise even with all the crops in the whole India!
Suppose the king had n wheat, which square should the last one cover?
输入
输出
样例输入
35
123456
0
样例输出
6
17
题目大意:
①、求A[1] = 1, A[2] = 2, A[3] = 4, ..., A[64]酱紫的序列 A{n} = pow(2, n-1)
②、S[1] = A[1], S[2] = A[1] + A[2], S[3] = A[1] + A[2] + A{3} ..., S{64} 前n项和
③、S[n]第一次 >= 题目输入的数据是在第几位
核心代码:
1 void cal_excel() 2 { 3 A[1] = 1; 4 for(int i = 2; i <= 64; ++ i) 5 { 6 A[i] = A[i-1] + pow(2, i-1); 7 } 8 return ; 9 }
C/C++代码实现(AC):
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 #include <stack> 7 #include <map> 8 #include <queue> 9 10 using namespace std; 11 long long A[70]; 12 13 void cal_excel() 14 { 15 A[1] = 1; 16 for(int i = 2; i <= 64; ++ i) 17 A[i] = A[i-1] + pow(2, i-1); 18 } 19 20 int main() 21 { 22 cal_excel(); 23 long long n; 24 while(~scanf("%lld", &n), n) 25 { 26 for(int i = 1; i <= 64; ++ i) 27 { 28 if(A[i] >= n) 29 { 30 printf("%d\n", i); 31 break; 32 } 33 } 34 } 35 return 0; 36 }
本文探讨了一个经典的数学问题:如果在棋盘上放置小麦,第一个格子放一粒,之后每个格子翻倍,国王有多少小麦才能填满棋盘?通过C/C++代码实现了计算过程,找出给定数量的小麦能覆盖到棋盘的哪一格。
1373

被折叠的 条评论
为什么被折叠?



