ACM practice every day (monkey peach problem)

Source: Internet
Author: User
Description
There are a bunch of peaches I don't know how to count. The monkeys eat half of them on the first day and eat one more. The next day, this method is used to eat more than half of the peaches. Every day, on the morning of the M day, the monkey found only one peach left. How many peaches were there? (M <29)
Input
The first row has an integer N, indicating that there are N groups of test data (starting from the second row, the data in each row is: day m );
Output
Each row of data is the total number of peaches.
Sample Input
2311
Sample output
226142

Common Solution
Analysis: The recurrence formula for the relationship between the total number of peaches in the previous day and the total number of peaches in the next day is a [M-1] = (a [m] + 1) x 2; we can find the total number of peaches on the first day by repeating m times.
#include <stdio.h>main(){ int i,n,m,sum; scanf("%d",&n); while(n--) {  sum=1;  scanf("%d",&m);  for (i=m;i>0;i--)   sum=2*(sum+1);  printf("%d\n",sum); }}

The recursive method changes the previous for loop to recursive call.
#include <stdio.h>int sum(int n){ if (n==0) return 1; else return 2*(sum(n-1)+1);}main(){ int m,n; scanf("%d",&m); while(m--) {  scanf("%d",&n);  printf("%d\n",sum(n)); }}

Simplest solutionAnalysis A [0] = 1a [1] = (a [0] + 1) * 2... A [n-1] = (a [N-2] + 1) * 2a [N] = (a [n-1] + 1) * 2 from where, the general formula for this series can be obtained: A [n] = 3 * (POW (2, n)-2
Then the program can be improved
# Include <stdio. h> main () {int M, N; scanf ("% d", & M); While (M --) {scanf ("% d", & N ); // 3 shifts n places to the left, which is equivalent to 3 * POW (2, n) printf ("% d \ n", (3 <n)-2 );}}


Description
Give you a disordered string containing lowercase letters (A -- Z) and some special characters. Please find out the number of all lowercase letters in the given string, take this number pair for 26 and output the number after the remainder in lowercase letters (0 for Z, 1 for A, 2 for B .... 25 corresponds to Y ).
Input
The first line is an integer N (1 <n <1000) indicating that the string M (1 <m <200) with N rows to be input
Output
Output the corresponding lowercase letters. Each lowercase letter occupies a single row.
Sample Input
2asdasl+%$^&ksdhkjhjksdadklf&(%^(alkha
Sample output
qj

#include <stdio.h>main(){ int i,n; char c[198]; scanf("%d",&n); while(n--) {  int count=0;  scanf("%s",c);  for(i=0;c[i]!='\0';i++)   if (c[i]>96 && c[i]<123) count++;  if (count%26==0) printf("z\n");  else printf("%c\n",count%26+96); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.