[C/C++] 10 classic C language applets, novices must see!

  1. Question: There are 1, 2, 3, and 4 numbers. How many different three-digit numbers can be formed without repeated numbers? How many are they?

  Program analysis: The numbers that can be filled in the hundreds, tens, and ones are all 1, 2, 3, and 4. After composing all the permutations, remove the permutations that do not meet the conditions.

  Program source code:

  main()

  {

  int i,j,k;

  printf("\n");

  for(i=1;i<5;i++) /*The following is a triple loop*/

  for(j=1;j<5;j++)

  for (k=1;k<5;k++)

  {

  if (i!=k&&i!=j&&j!=k) /*Make sure that i, j, and k are different from each other*/

  printf("%d,%d,%d\n",i,j,k);

  }

  }

 

  2. Topic: The bonus issued by the company is based on the profit commission. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%; the profit is high

If it is less than RMB 200,000, the portion of less than RMB 100,000 shall be deducted at 10%, and the portion of more than RMB 100,000 may be deducted.

If it is between 200,000 and 400,000, the portion higher than 200,000 yuan can be commissioned 5%; between 400,000 and 600,000, the portion higher than 400,000 yuan can be commissioned 3%; between 600,000 and 600,000 When it is between 1 million, the portion above 600,000 yuan will receive a commission of 1.5%. When it is above 1 million yuan, the portion exceeding 1 million yuan will be commissioned at 1%. Enter the monthly profit I from the keyboard. What is the total amount of bonuses that should be paid?

  Program analysis: Please use the number axis to divide and locate. Note that the bonus must be defined as a growth integer when defining it.

  Program source code:

  main()

  {

  long int i;

  int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;

  scanf("%ld",&i);

  bonus1=100000*0.1;bonus2=bonus1+100000*0.75;

  bonus4=bonus2+200000*0.5;

  bonus6=bonus4+200000*0.3;

  bonus10=bonus6+400000*0.15;

  if(i<=100000)

  bonus=i*0.1;

  else if(i<=200000)

  bonus=bonus1+(i-100000)*0.075;

  else if(i<=400000)

  bonus = bonus2 + (i-200000) * 0.05;

  else if(i<=600000)

  bonus=bonus4+(i-400000)*0.03;

  else if(i<=1000000)

  bonus=bonus6+(i-600000)*0.015;

  else

  bonus=bonus10+(i-1000000)*0.01;

  printf("bonus=%d",bonus);

  }

 

 

 

  3. Question: An integer, after adding 100, it becomes a perfect square number, and adding 168 is another perfect square number. What is the number?

  Program analysis: judge within 100,000, first add 100 to the number and then open the square, then add 268 to the number and then open the square. If the result after the square meets the following conditions, it is the result.

  Program source code:

  #include "math.h"

  main()

  {

  long int i,x,y,z;

  for (i=1;i<100000;i++)

  {x=sqrt(i+100); /*x is the result after adding 100 to the square*/

  y=sqrt(i+268); /*y is the result after adding 168 to the square*/

  if(x*x==i+100&&y*y==i+268)/*If the square root of a number is equal to that number, it means that the number is a perfect square number*/

  printf("\n%ld\n",i);

  }

  }

 

  4. Topic: Enter a certain day, a certain month, and a certain day to determine which day is the day of the year?

  Program analysis: Take March 5th as an example. You should add up the previous two months first, and then add 5 days, which is the first few days of the year. In special circumstances, you need to consider adding one more day when the input month is greater than 3 in a leap year. .

  Program source code:

  main()

  {

  int day,month,year,sum,leap;

  printf("\nplease input year,month,day\n");

  scanf("%d,%d,%d",&year,&month,&day);

  switch(month)/*First calculate the total number of days in a month before the month*/

  {

  case 1:sum=0;break;

  case 2:sum=31;break;

  case 3:sum=59;break;

  case 4:sum=90;break;

  case 5:sum=120;break;

  case 6:sum=151;break;

  case 7:sum=181;break;

  case 8:sum=212;break;

  case 9:sum=243;break;

  case 10:sum=273;break;

  case 11:sum=304;break;

  case 12:sum=334;break;

  defaultrintf("data error");break;

  }

  sum=sum+day; /*Plus the number of days in a certain day*/

  if(year%400==0||(year%4==0&&year%100!=0))/* Determine if it is a leap year*/

  leap=1;

  else

  leap=0;

  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 added by one day*/

  sum++;

  printf("It is the %dth day.",sum); }

 

  5. Question: Input three integers x, y, z, please output these three numbers from small to large.

  Program analysis: we find a way to put the smallest number on x, first compare x and y, if x>y, exchange the values ​​of x and y, and then compare x and z, if x>z Then exchange the values ​​of x and z so that x can be minimized.

  Program source code:

  main()

  {

  int x,y,z,t;

  scanf("%d%d%d",&x,&y,&z);

  if (x>y)

  /*Exchange the values ​​of x and y*/

  if(x>z)

  /*Exchange the values ​​of x and z*/

  if(y>z)

  /*Exchange the values ​​of z,y*/

  printf("small to big: %d %d %d\n",x,y,z);

  }

 

  6. Title: Use * to output the pattern of letter C.

  Program analysis: You can write the letter C on the paper with the number <|>*<|>, and then output it in separate lines.

  Program source code:

  #include "stdio.h"

  main()

  {

  printf("Hello C-world!\n");

  printf(" ****\n");

  printf(" *\n");

  printf(" * \n");

  printf(" ****\n");

  }

  7. Title: To output special patterns, please run in c environment, take a look, Very Beautiful!

  Program analysis: There are 256 characters in total. Different characters have different graphics.

  Program source code:

  #include "stdio.h"

  main()

  {

  char a=176,b=219;

  printf("%c%c%c%c%c\n",b,a,a,a,b);

  printf("%c%c%c%c%c\n",a,b,a,b,a);

  printf("%c%c%c%c%c\n",a,a,b,a,a);

  printf("%c%c%c%c%c\n",a,b,a,b,a);

  printf("%c%c%c%c%c\n",b,a,a,a,b); }

  8. Question: Output 9*9 formulas.

  Program analysis: Considering the rows and columns, a total of 9 rows and 9 columns, i control row, j control column.

  Program source code:

  #include "stdio.h"

  main()

  {

  int i,j,result;

  printf("\n");

  for (i=1;i<10;i++)

  { for(j=1;j<10;j++)

  {

  result=i*j;

  printf("%d*%d=%-3d",i,j,result);/*-3d means left-justified, occupying 3 bits*/

  }

  printf("\n");/*New line after each line*/

  }

  }

 

 

 

 

  9. Title: Request to output a chess board.

  Program analysis: use i to control the row and j to control the column, and control whether the output is black or white according to the change of the sum of i+j.

  Program source code:

  #include "stdio.h"

  main()

  {

  int i,j;

  for(i=0;i<8;i++)

  {

  for(j=0;j<8;j++)

  if((i+j)%2==0)

  printf("%c%c",219,219);

  else

  printf(" ");

  printf("\n");

  }

  }

 

 

 

  10. Topic: Print the stairs and print two smiling faces on the top of the stairs at the same time.

  Program analysis: Use i to control the rows, j to control the columns, and j to control the number of output black squares according to the change of i.

  Program source code:

  #include "stdio.h"

  main()

  {

  int i,j;

  printf("\n");/*Output two smiling faces*/

  for(i=1;i<11;i++)

  {

  for(j=1;j<=i;j++)

  printf("%c%c",219,219);

  printf("\n");

  }

  }

Click for more information, more free open source projects and courses are waiting for you to watch!

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/109223374