Take notes for the fourth day of IOS Learning (loop), and take notes for the fourth day of ios Learning

Source: Internet
Author: User
Tags uppercase letter

Take notes for the fourth day of IOS Learning (loop), and take notes for the fourth day of ios Learning

IOS Learning (C Language) knowledge point sorting notes

I. Branch Structure

1. The branch structure can be divided into two types: single branch: if () {}; multiple branches: if () {} else {}

2. if the single branch if expression is true, execute the statement in {}. if the double branch if expression is not true, execute the statement in else {}.

3. The output system of the string will print from the array address until the character '\ 0'. If the initialization is not correct, information outside the array may be printed.

4. If only one execution statement in the branch structure can omit the braces {}.

Ii. Switch statements

1. Statement structure: switch (parameter) case constant: execution statement; break; default: execution statement; break; case must be a constant and cannot be repeated.

2. In no special case, break must be added next to each case. Otherwise, case penetration will occur.

3. default is generally recommended to retain the conditions that are not met in all cases.

Iii. Loop Structure

1. Loop: The statement group is executed repeatedly when the expression conditions are met. The loop must have exit conditions or an endless loop.

2. Loop type: for (initialization Statement 1; condition judgment Statement 2; Statement 3) {execution statement}; while (Condition Statement) {execution statement }; do {execution statement} while (Condition Statement );

3. In the loop body, continue indicates that the content of the current condition is skipped. Execute the next loop. break indicates that the entire loop is ended.

4. When do while is in the first loop, execute the statement first and then make a judgment. Execute the statement at least once.

5. while and do while must be initialized before the loop. The for Loop itself contains the initialization statement.

Iv. goto jump statement

1. goto indicates to jump to a tag unconditionally and avoid using it whenever possible. Disadvantages: Make the code directory unclear and damage the sense of attention.

2. goto instance:

// Goto Implementation 1 ~ 100 sum

 

1 int main () {3 int sum = 0; 5 int I = 1; 7 ADD: 9 sum + = I; 11 I ++; 13 if (I <= 100) {15 goto ADD; // jump to the row where the tag number is located and execute 17} 19 printf ("sum = % d \ n", sum); 21 return 0; 23}

5. Exercise cyclically

1. Calculate the numbers, uppercase/lowercase letters, and other characters in the current input.

Implementation Code:

1 int main () {3 int sz = 0, xz = 0, dz = 0, qt = 0; 5 char ch; 7 while (ch = getchar ())! = '\ N') {9 if (ch> = '0' & ch <= '9') 11 sz ++; 13 else if (ch> = 'A' & ch <= 'Z') 15 xz ++; 17 else if (ch> = 'A' & ch <= 'Z') 19 dz ++; 21 else23 qt ++; 25} 27 printf ("% d digits \ n", sz); 29 printf ("% d lowercase letters \ n ", xz); 31 printf ("% d uppercase letters \ n", dz); 33 printf ("% d other characters \ n ", dz); 35 return 0; 37}

2. output the 9-9 multiplication table

1*1 = 1,

2*1 = 2*2 = 4

3*1 ......

Implementation Code:

 1  int main(){ 3    for (int i=1; i<=9; i++) { 5       for (int j=1; j<=i; j++) {7         printf("%dx%d=%d\t\t",i,j,i*j);          }11        printf("\n");13     }15      return 0;   17   }

3. Output 1 ~ All prime numbers of 100 (prime number: the number that can only be divided by 1 and itself except 1)

Implementation Code:

1 int main () {3 for (int I = 1; I <= 100; I ++) {5 for (int j = 1; j <= I; j ++) {7 if (j = 1) 9 continue; 11 if (I % j = 0) 13 {15 if (I! = J) 17 {19 // printf ("% d is not a prime number \ n", I); 21 break; 23} 25 else27 {29 printf ("% d is prime number \ n", I); 31 break; 33} 35} 37} 39} 41 return 0; 43}

4. A ball falls freely from the height of 100 meters. After each landing, the ball jumps back to half of the original height. Then, how many meters does it go through during the first landing?

Implementation Code:

1 int main () {3 float hight = 100, num = 10, sumh = 0; 5 for (int I = 1; I <= num; I ++) {7 sumh + = hight; 9 hight/= 2; 11 printf ("% d fall (%. 1f) meters \ n ", I, hight); 13} 15 printf (" Total pass (%. 1f) Meters ", sumh); 19 return 0; 21}

5. Calculate the maximum common divisor of two numbers by moving and division.

Implementation Code:

1 int main () {3 // (28, 35) = (35, 28) = (28, 7) = 0 5 int m, n, temp, rst; 7 printf ("enter two numbers: \ n"); 9 scanf ("% d", & m, & n); 11 while (m % n! = 0) {13 if (m % n = m) 15 {17 temp = m; 19 m = n; 21 n = temp; 23 rst = m % n; 25 printf ("m = % d, n = % d \ n", m, n); 27} else {29 rst = m % n; 31 m = n; 33 n = rst; 35 printf ("m = % d, n = % d \ n", m, n); 37} 39} 40 printf ("% d ", n); 41 return 0; 42}

6. decompose a positive integer into a prime factor. For example, enter 90 and print 90 = 2*3*3*5.

Implementation Code:

 1 int main(){ 2     int v; 3     scanf("%d",&v); 4     printf("%d=",v); 5     for (int i=2; i<=v; i++) { 6         while (v%i==0) { 7             if(i<v) 8                printf("%d*",i); 9             else10                printf("%d",i);11            v/=i;12         }13     }14     return 0;15   }

7. Process Control (for factorial and) Input n (int type), print 1! + 2! + 3! + 4! + 5! +... + N! Value

Implementation Code:

1 int main () {2 // For example, 5 factorial and = 1*1 + 2*2 + 3*3*3 + 4*4*4*4 + 5*5*5*5*5*5 3 int n, temp = 0, sum = 0; 4 scanf ("% d", & n); 5 for (int I = 1; I <= n; I ++) {6 temp = 1; 7 for (int j = 1; j <= I; j ++) {8 temp * = I; 9} 10 sum + = temp; 11} 12 printf ("sum = % d", sum); 13 return 0; 14}

8. Integer backward output. For example, if an integer of 12345 is input, the output value is 54321.

Implementation Code:

 1 int main(){ 2   int a; 3     scanf("%d",&a); 4     while(a/10!=0){ 5         printf("%d ",a%10); 6         a/=10; 7     } 8    printf("%d ",a%10); 9    return 0;10 }

 

9. Process Control (for loop) Outputs A letter triangle and an uppercase letter, for example, F output:

A

ABA

ABCBA

ABCDCBA

ABCDEDCBA

ABCDEFEDCBA

Implementation Code:

 1 int main(){ 2     char ch='A'; 3     for (int i=0; i<='F'-ch; i++) { 4         for (int n='F'-ch-i; n>0; n--) { 5              printf(" "); 6         } 7      for (int j=0; j<=i; j++) { 8            printf("%c",ch+j); 9         }10      for (int m=i;m>0 ;m-- ) {11             printf("%c",ch+m-1);12        }13       printf("\n");14     }15     return 0;16  }

10. Process Control (for loop), output letter sequence such as input: F

Output:

FEDCBA

EDCBAB

DCBABC

CBABCD

BABCDE

ABCDEF

Implementation Code:

1 int main () {2 char sh, nsh, bsh; 3 printf ("enter a character:"); 4 scanf ("% c", & sh ); 5 bsh = sh; 6 int len = sh-'A'; 7 for (int I = 0; I <= len; I ++) {8 sh = bsh-I; 9 // nsh = bsh-len + 1; 10 for (int j = 1; j <= I; j ++) {11 printf (""); 12} 13 for (int n = I; n <= len; n ++) {14 printf ("% c", sh --); 15} 16 nsh = sh + 2; 17 for (int m = 0; m <I; m ++) {18 printf ("% c", nsh ++ ); 19} 20 printf ("\ n"); 21} 22 return 0; 23}

11. Enter a date, year, month, and day to calculate the day of the year (considering the leap year)

Implementation Code:

1 int main () {2 int year, month, day, total = 0; 3 scanf ("% d", & year, & month, & day ); 4 if (year <= 0) 5 printf ("enter the correct year. "); 6 for (int I = 1; I <month; I ++) {7 switch (I) {8 case 1: 9 case total + = 31; 16 break; 17 case total + = 30; 22 break; 23 case 2: 24 if ((! (Year % 4) & amp; year % 100) |! (Year % 400) 25 total + = 29; 26 else27 total + = 28; 28 break; 29} 30} 31 total + = day; 32 printf ("% d day of year % d", year, total); 33 return 0; 34}

12. Monkey peach eating problem: the monkey picked a few peaches on the first day, and immediately eats half of them. It is not enough, and eats more than half of the peaches on the morning of the next day, I ate another one. In the future, I eat the remaining half of the previous day every morning. When you want to eat again in the morning of the seventh day, there is only one peach left. Calculate the total number of extracted items on the first day.

Implementation Code:

Int main () {// analysis: Day 10 1 day 9: Day 4 week: Day 10: 22 int sum = 1, I, index = 10; for (I = 1; I <= 9; I ++) {printf ("% d days have peach % d \ n", index, sum); sum = (sum + 1) * 2; index --;} printf ("monkey picked % d peaches. \ N ", sum); return 0 ;}

 

 

 

Related Article

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.