目录
思路:
假设有n个小猴子,总共有x个桃子,举例,现在n=3,
由以上推理得,现在有n个猴子,则剩余桃子数量是((4/5)^n*x-(4/5)^n - (4/5)^(n-1)....-4/5)
我们先单独把s = -(4/5)^n - (4/5)^(n-1)....-4/5)拿出来,
用错位相减法,得到(4/5)*s = -(4/5)^(n+1) - (4/5)^(n)....-(4/5)(n+1))
得到(1/5)s = - 4/5 + (4/5)^n-1
在将剩余桃子数量得第一个((4/5)^n*x部分拿下来,
得到剩余桃子数量是4^n/5^n * (x+4) - 4,由于桃子剩余数量不可能是分数,又要最小值,
所以5^n = x + 4
所以x = 5^n - 4,为总共有多少个桃子
老猴子最少能得到得桃子数,等于小猴子数量加上最后剩余得桃子数量,
所以是4^n/5^n * (x+4) - 4+n,将x = 5^n - 4带入其中4^n/5^n * (5^n - 4+4) - 4+n,
化简为4^n - 4+n
- #include
- #include
- using namespace std;
- int main ()
- {
- int n;//猴子数量
- while(cin>>n)
- {
- if(n == 0)
- break;
- else
- {
- long num = pow(5, n) - 4;
- long left = pow(4, n) - 4 + n;
- cout<
" "< - }
- }
- return 0;
- }
第二题-反转部分单向列表
思路:
只要看翻转部分就好,其他都是系统写好了的,为了方便我们定义一个哨兵位,首先找到需要反转的区间,然后利用三个指针将翻转区间翻转,然后首尾连接输出即可,注意翻转时细节,防止next指向空
- # include
- using namespace std;
-
- struct list_node{
- int val;
- struct list_node * next;
- };
-
- list_node * input_list(void)
- {
- int n, val;
- list_node * phead = new list_node();
- list_node * cur_pnode = phead;
- scanf("%d", &n);
- for (int i = 1; i <= n; ++i) {
- scanf("%d", &val);
- if (i == 1) {
- cur_pnode->val = val;
- cur_pnode->next = NULL;
- }
- else {
- list_node * new_pnode = new list_node();
- new_pnode->val = val;
- new_pnode->next = NULL;
- cur_pnode->next = new_pnode;
- cur_pnode = new_pnode;
- }
- }
- return phead;
- }
-
-
- list_node * reverse_list(list_node * head, int L, int R)
- {
- list_node* phead = new list_node;
- phead->next = head;
- list_node* right = head;
- list_node* left = head;
- while(--L)
- {
- right = right->next;
- }
- while(--R)
- {
- left = left->next;
- }
- list_node* cur = right;
- list_node* next = cur->next;
- list_node* pre = nullptr;
-
- while(pre != left)
- {
- cur->next = pre;
- pre = cur;
- cur = next;
- if(next)
- next = next->next;
- }
- right->next = cur;
- cur = phead;
- while(cur->next!=right)
- {
- cur = cur->next;
- }
- cur->next = left;
- return phead->next;
- }
-
- void print_list(list_node * head)
- {
- while (head != NULL) {
- printf("%d ", head->val);
- head = head->next;
- }
- puts("");
- }
-
-
- int main ()
- {
- int L, R;
- list_node * head = input_list();
- scanf("%d%d", &L, &R);
- list_node * new_head = reverse_list(head, L, R);
- print_list(new_head);
- return 0;
- }