C Language Program Development Classic example of three

Source: Internet
Author: User
Tags printf

"Program 21"

Topic: Monkey Eat Peach problem: The first day the monkey picked a number of peaches, immediately ate half, still not addicted, and eat one more, the next morning and the remaining peaches eat half, and eat one more. Every morning after the last day of the rest of the half zero. On the 10th day of the morning when I wanted to eat again, I saw only one peach left. Ask for the first day of the total number of pick.

1. Procedure analysis: Take the reverse thinking method, infer from the backward forward.

2. Program Source code:

main()
{
int day,x1,x2;
day9;
x21;
while(day>0)
 {x1(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/
 x2x1;
 day--;
 }
printf("the total is %d\n",x1);
}

"Program 22"

Title: Two table tennis teams compete, each out of three people. A team for three a,b,c, Team B for x,y,z three people. The list of the matches has been decided by lottery. The team was asked about the list of the matches. A said he does not and X, C said he does not and x,z, please compile a program to find the list of three teams.

1. Procedure analysis: To determine the method of prime number: To remove 2 to sqrt (this number), if divisible, it indicates that this number is not prime, conversely is prime.

2. Program Source code:

main()
{
char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/
for(i'x';i<'z';i++)
 for(j'x';j<'z';j++)
 {
 if(i!j)
  for(k'x';k<'z';k++)
  { if(i!k&&j!k)
   { if(i!'x'&&k!'x'&&k!'z')
   printf("order is a--%c\tb--%c\tc--%c\n",i,j,k);
   }
  }
 }
}

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.