C language programming 50 cases (1) (Classic favorites), C language programming

Source: Internet
Author: User

C language programming 50 cases (1) (Classic favorites), C language programming

[Procedure 1]
Question: How many three numbers can be composed of 1, 2, 3, and 4 numbers that are different from each other and have no repeated numbers? What is it?
1. Program Analysis: It can be filled in hundreds of digits, ten digits, and one digit is 1, 2, 3, and 4. Make up all sorts and then proceed
Drop the order that does not meet the conditions.

1 # include "stdio. h "2 # include" conio. h "3 main () 4 {5 int I, j, k; 6 printf (" \ n "); 7 for (I = 1; I <5; I ++) /* The following is a triple loop */8 for (j = 1; j <5; j ++) 9 for (k = 1; k <5; k ++) 10 {11 if (I! = K & I! = J & j! = K)/* Ensure that I, j, and k are different */12 printf ("% d, % d, % d \ n", I, j, k ); 13} 14 getch (); 15}

[Procedure 2]
Question: The bonus paid by an enterprise is based on the profit. If the profit (I) is less than or equal to 0.1 million yuan, the bonus can be raised by 10%. The profit is high.
If the price is less than 0.1 million yuan, the price of the part lower than 0.2 million yuan is 0.1 million, and the price of the part higher than 10% yuan is 0.1 million.
7.5%; 0.2 million to 0.4 million, more than 0.2 million yuan, can be raised to 5%; 0.4 million to 0.6 million is higher
For the part of 0.4 million yuan, it can be raised to 3%; for the part between 0.6 million and 1 million, the part above 0.6 million yuan can be raised to 1.5%, higher
1 million Yuan, more than 1 million yuan of part of the 1% commission, input the current month's profit from the keyboard I, the total number of bonuses payable?
1. Program Analysis: Use the number axis to divide and locate. Note that you need to define the bonus type as a growth integer.

 1 #include "stdio.h" 2 #include "conio.h" 3 main() 4 { 5   long int i; 6   int bonus1,bonus2,bonus4,bonus6,bonus10,bonus; 7   scanf("%ld",&i);  8   bonus1=100000*0. 1; 9   bonus2=bonus1+100000*0.75;10   bonus4=bonus2+200000*0.5;11   bonus6=bonus4+200000*0.3;12   bonus10=bonus6+400000*0.15;13   if(i<=100000)14     bonus=i*0.1;15     else if(i<=200000)16       bonus=bonus1+(i-100000)*0.075;17         else if(i<=400000)18           bonus=bonus2+(i-200000)*0.05;19             else if(i<=600000)20               bonus=bonus4+(i-400000)*0.03;21                 else if(i<=1000000)22                   bonus=bonus6+(i-600000)*0.015;23                     else24                       bonus=bonus10+(i-1000000)*0.01;25   printf("bonus=%d",bonus);26   getch(); 27 }

[Procedure 3]
Question: an integer. After 100 is added, it is a full number of workers, and 168 is a full number of workers. What is this number?
1. Program Analysis: If the value is less than 0.1 million, add 100 to the number before the start, and then add 268 to the number before the start. If
The following conditions are met. See the specific analysis:

1 # include "math. h "2 # include" stdio. h "3 # include" conio. h "4 main () 5 {6 long int I, x, y, z; 7 for (I = 1; I <100000; I ++) 8 {9 x = sqrt (I + 100);/* x is the result after 100 is added. */10 y = sqrt (I + 268 ); /* y is the result after 168 is added. */11 if (x * x = I + 100 & y * y = I + 268) /* If the square of the square root of a number is equal to this number, it indicates that this number is a full number of bytes */12 printf ("\ n % ld \ n", I ); 13} 14 getch (); 15}

[Procedure 4]
Question: enter a day of a year to determine the day of the year?
1. Program Analysis: Taking January 1, March 5 as an example, we should add up the previous two months, and then add 5 days, that is, the day of the current year.
In this case, an additional day is required when the input month is greater than 3 in a leap year.

1 # include "stdio. h "2 # include" conio. h "3 main () 4 {5 int day, month, year, sum, leap; 6 printf (" \ nplease input year, month, day \ n "); 7 scanf ("% d, % d, % d", & year, & month, & day); 8 switch (month) /* calculate the total number of days of the month before a month */9 {10 case 1: sum = 0; break; 11 case 2: sum = 31; break; 12 case 3: sum = 59; break; 13 case 4: sum = 90; break; 14 case 5: sum = 120; break; 15 case 6: sum = 151; break; 16 case 7: sum = 181; break; 17 case 8: sum = 212; break; 18 case 9: sum = 243; break; 19 case 10: sum = 273; break; 20 case 11: sum = 304; break; 21 case 12: sum = 334; break; 22 default: printf ("data error"); break; 23} 24 sum = sum + day; /* plus the day */25 if (year % 400 = 0 | (year % 4 = 0 & year % 100! = 0)/* determine whether it is a leap year */26 leap = 1; 27 else28 leap = 0; 29 if (leap = 1 & month> 2) /* if It is a leap year and the month is greater than 2, the total number of days should be plus one day */30 sum ++; 31 printf ("It is the % dth day. ", sum); 32 getch (); 33}

[Procedure 5]
Question: input three integers x, y, and z. Please output these three numbers from small to large.
1. Program Analysis: we try to put the smallest number on x and compare x with y first. If x> y, the values of x and y are exchanged,
Then compare x with z. If x> z, the value of x is exchanged with z to minimize x.

1 # include "stdio. h "2 # include" conio. h "3 main () 4 {5 int x, y, z, t; 6 scanf (" % d ", & x, & y, & z ); 7 if (x> y) 8 {t = x; x = y; y = t;}/* swap x, y value */9 if (x> z) 10 {t = z; z = x; x = t;}/* swap x, z value */11 if (y> z) 12 {t = y; y = z; z = t;}/* swap z, y value */13 printf ("small to big: % d \ n", x, y, z); 14 getch (); 15}

[Procedure 6]
Question: Use the letter "*" to output the C "pattern.
1. Program Analysis: You can use the '*' sign to write the letter C on the paper, and then output it by branch.

 1 #include "stdio.h" 2 #include "conio.h" 3 main() 4 { 5   printf("Hello C-world!\n"); 6   printf(" ****\n"); 7   printf(" *\n"); 8   printf(" * \n"); 9   printf(" ****\n");10   getch(); 11 }

[Procedure 7]
Question: Output A special pattern. Run it in the c environment. Have a look, Very Beautiful!
1. Program Analysis: There are 256 characters in total. Different Characters and images are different.

 1 #include "stdio.h" 2 #include "conio.h" 3 main() 4 { 5   char a=176,b=219; 6   printf("%c%c%c%c%c\n",b,a,a,a,b); 7   printf("%c%c%c%c%c\n",a,b,a,b,a); 8   printf("%c%c%c%c%c\n",a,a,b,a,a); 9   printf("%c%c%c%c%c\n",a,b,a,b,a);10   printf("%c%c%c%c%c\n",b,a,a,a,b);11   getch(); 12 }

[Procedure 8]
Question: 9*9 tips.
1. Program Analysis: For branch and column consideration, there are 9 rows and 9 columns, I control row and j control column.

1 # include "stdio. h "2 # include" conio. h "3 main () 4 {5 int I, j, result; 6 printf (" \ n "); 7 for (I = 1; I <10; I ++) 8 {9 for (j = 1; j <10; j ++) 10 {11 result = I * j; 12 printf ("% d * % d = %-3d", I, j, result);/*-3d indicates left alignment, 3 digits */13} 14 printf ("\ n");/* line feed after each line */15} 16 getch (); 17}

[Procedure 9]
Question: It is required to output the chess board.
1. Program Analysis: Use I control rows and j to control columns, and control the output black square or white square according to the sum of I + j.

 1 #include "stdio.h" 2 #include "conio.h" 3 main() 4 { 5   int i,j; 6   for(i=0;i<8;i++) 7   { 8     for(j=0;j<8;j++) 9       if((i+j)%2==0)10         printf("%c%c",219,219);11       else12         printf("  ");13     printf("\n");14   }15   getch(); 16 }

[Procedure 10]
Question: print the stairs and print two smiling faces above the stairs.
1. Program Analysis: Use I to control rows and j to control columns. j controls the number of black squares output based on I changes.

1 # include "stdio. h "2 # include" conio. h "3 main () 4 {5 int I, j; 6 printf (" \ 1 \ 1 \ n "); /* output two smiling faces */7 for (I = 1; I <11; I ++) 8 {9 for (j = 1; j <= I; j ++) 10 printf ("% c", 219,219); 11 printf ("\ n"); 12} 13 getch (); 14}

 

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.