C Language Code training camp (2)

Source: Internet
Author: User

In the comments in the previous article, we did not compose the code in the feedback comments. We change the rules, you can put your own implementation of the code in their own Jian Shubo text, and then paste the link in the comments. This is also convenient for you to trace back later.

Of course, you can also send me an email directly. I hope we all insist on punch and make progress together.

1. Exercises 1.1 Topics

At the end of the last chapter, I left this topic and printed the following picture in code.


Diamond 1.2 Analysis

This graph is divided into two sections in a total of 31 rows. The upper part is a 16-row positive triangle, and the lower half is a 15-row inverted triangle. How to draw a triangle we've already talked about it, and it's easy for us to write down this piece of code.

int main () {int i, J; int n;Positive triangle for (i =0; I <16; i++) {for (j =0; J <15-i; j + +) printf (0; J < 2 * i + 1; j + +) printf ( "*"); printf ( "\n");} //inverted triangle for (i = 0, I < 15; i++) {for ( j = 0; j < i + 1; j + +) printf (0; J < 2 * (15-i)-1; J + +) printf ( "\n");} return 0;             

Executing this code yourself has been implemented, but it's a bit cumbersome to use two sets of for loops in two parts. This code is optimized to get the final result.

1.3 Answers
#Define line 31IntMain(){int I, J;int n; For (i = 0; i < line; i++) { if (I <= line/ 2) n = i; Else n = line-i- 1; For (j = 0, J < (Line/ 2-n); j + +) printf (""); For (j = 0; J < 2 * n + 1; j + +) printf ("*"); printf ("\ n");} return 0;}                 

Please read this code carefully, there are questions welcome to explore.

2. Examples

Today's example is not very difficult, is a friend in the previous question in a fairly simple topic, the focus is to help you understand the basic idea of program design. The topics are as follows:

Please program to calculate the X + 2y + 3z = 100 of all solutions to this equation.

3. Analysis

In the face of this problem, we need to have a basic idea is to be poor to cite all possible, just like the password trial and error. We assume that the range of x, Y, Z is 0~100, then there are 100 * 100 * 100 combinations. As long as we judge all the combinations in turn, we can find the answer. One of the most basic methods of this permutation is the triple loop.

4. Answer
int main(){    int x, y, z;    0; x < 101; x++)        for (y = 0; y < 101; y++)            for (z = 0; z < 101; z++) if (x + 2 * y + 3 * z == 100) printf("x = %d, y = %d, z = %d\n", x, y, z); return 0;}

The results of the implementation are as follows:


Execution results

Of course, this program also has to continue to optimize the space, the value range of x 0~100,y range of 0~50,z value range is 0~34. This will greatly improve the efficiency of the program operation. Please make yourselves.

5. After-school practice

The weekend, today's exercises to some difficulty. Please try programming to complete the printing Yang Hui triangle. Such as:

You are welcome to join the Learning Exchange Group if you encounter any problems or want to acquire learning resources in the learning process.
639368839, we learn c/c++! together.

C Language Code training camp (2)

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.