C language classic 100 questions-1

C language classic 100 questions-1

C language classic 100 questions (1-10)

Topic 1: there are 1, 2, 3 and 4 numbers. How many different three digits can be formed without repeated numbers? How much is it?

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

#include "stdio.h"
#include "conio.h"
void main()
{
  int i,j,k,m;
  m=0;
  for(i=1;i<5;i++) /*The following is a triple cycle*/
    for(j=1;j<5;j++)
      for (k=1;k<5;k++)
      {
        if (i!=k&&i!=j&&j!=k) /*Make sure i, j and k are different from each other*/
        {
          m++;
          printf("%d%d%d  ",i,j,k);
          if(m%10 == 0)
          {
            putchar('\n');
          }
        }
      }
  getch();
}

Topic 2: the bonus paid by the enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increased by 10%; When the profit is higher than 100000 yuan and lower than 200000 yuan, the part lower than 100000 yuan will be deducted by 10%, and the part higher than 100000 yuan will be deducted by 7.5%; Between 200000 and 400000 yuan, the part higher than 200000 yuan can be deducted by 5%; For the part higher than 400000 yuan between 400000 and 600000 yuan, the commission can be 3%; When it is between 600000 and 1 million yuan, the part higher than 600000 yuan can be deducted by 1.5%. When it is higher than 1 million yuan, the part higher than 1 million yuan can be deducted by 1%. Enter the profit I of the current month from the keyboard to calculate the total amount of bonus to be paid?

#include "stdio.h"

void main()
{
	int I=0;
	double sum=0;
	printf("Please enter profit I (10000 yuan):");
	scanf("%d",&I);
	switch(I/10)
	{
    case 0:sum=I*0.1;break;
    case 1:sum=10*0.1+(I-10)*0.075;break;
    case 2:case 3:sum=10*0.1+(I-10)*0.075+(I-20)*0.05;break;
    case 4:case 5:sum=10*0.1+(I-10)*0.075+(I-20)*0.05+(I-40)*0.03;break;
    case 6:case 7:case 8:case 9:sum=10*0.1+(I-10)*0.075+(I-20)*0.05+(I-40)*0.03+(I-60)*0.015;break;
    default:sum=10*0.1+(I-10)*0.075+(I-20)*0.05+(I-40)*0.03+(I-60)*0.015+(I-100)*0.01;break;
	}
	printf("The final bonus is %.2lf.\n",sum);
}

Topic 3: an integer is a complete square number after adding 100, and a complete square number after adding 168. What is the number?

#include "stdio.h"
#include "math.h"
void main()
{
	long int i,x,y,z;
  for (i=1;i<100000;i++)
  {
    x=sqrt(i+100); /*x Is the result after adding 100 square meters*/
    y=sqrt(i+268); /*y Is the result after adding the formula after 168*/
    if(x*x==i+100&&y*y==i+268) /*If the square of the square root of a number is equal to the number, it means that the number is a complete square*/
    printf("\n%ld\n",i);
  }
}

perhaps

#include "stdio.h"
#include "math.h"
void main()
{
	int i;
	for(i=0;i<10000;i++)
	{
		if(sqrt(i+100)==(int)sqrt(i+100))
		{
			if(sqrt(i+268)==(int)sqrt(i+268))
			{
				printf("the intger is %d\n",i);
			}
		}	
	}
}

Topic 4: enter a certain day of a certain month in a certain year to judge the day of the year?

Program analysis: take March 5 as an example, you should first add up the of the first two months, and then add 5 days, that is, the day of the year. Under special circumstances, when the leap year and the input month is greater than 3, you need to consider adding one more day. The judgment conditions of leap year:

  1. The year can be divided by 4 and cannot be divided by 100;
  2. The year can be divided by 400.
#include "stdio.h"

void 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 before a 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;
    default:printf("data error");break;
  }
  sum=sum+day; /*Plus the number of days of a day*/
  if(year%400==0||(year%4==0&&year%100!=0)) /*Judge whether 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);
}

Topic 5: input three integers x, y and z, and output them from small to large.

#include "stdio.h"

void main()
{
	int x,y,z,buff;
	printf("Please enter three integers x,y,z: ");
	scanf("%d,%d,%d",&x,&y,&z);
	if(x>y)//Swap the values of x, y
	{
		buff = x;
		x = y;
		y = buff;
	}
	if(x>z)//Swap the values of x, z
	{
		buff = x;
		x = z;
		z = buff;
	}
	if(y>z)//Swap the values of z, y
	{
		buff = y;
		y = z;
		z = buff;
	}
	printf("The three numbers arrive in the following order:%d,%d,%d\n",x,y,z);
}

Topic 6: output the pattern of letter C with * sign.

Program analysis: first write the letter C on the paper with '*' and then output it in branches.

#include "stdio.h"

void main()
{
  printf("Hello C-world!\n");
  printf(" ****\n");
  printf(" *\n");
  printf(" * \n");
  printf(" ****\n");
}

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

#include "stdio.h"
#include "conio.h"
void 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);
  getch(); 
}

According to this code, the output result at the beginning is as follows:

This is clearly not the desired effect. The specific reason for the disordered code is that the output ASCII code is output by cmd in GBK code, the hexadecimal of 176 is B0219, the hexadecimal of 0xB0DB is DB, and 0xB0DB is the internal code of "Bai", so the output is "Bai".

To display the extended ASCII code, it needs to be displayed under 437 OEM America, so that you can display what you want. The specific steps to modify the default code page of the console are as follows:

  1. Right click the title bar icon in the upper left corner of the operation interface and select default value
  2. Use the old console - > close and rerun the program - > modify the default code page. 936 (ANSI/OEM simplified Chinese GBK) is 437 OEM USA (if it has not changed after changing to 437, you need to change the font to Lucia Console in the default value option)
  3. Just close it and run it again

Similarly, if you want to output Chinese without garbled code, you need to change the code page to 936 (ANSI/OEM simplified Chinese GBK).

Title 8: output 9 * 9 formula.

#include "stdio.h"

void main()
{
  int i,j;
  for(i=1;i<=9;i++)
  {
	  for(j=1;j<=i;j++)
	  {
		  printf("%d x %d = %-3d ",j,i,i*j);/*-3d Indicates left alignment, accounting for 3 digits*/
	  }
	  printf("\n");
  }
}

Title 9: request to output the chess board.

Program analysis: to display the extended ASCII code, it needs to be displayed under 437 OEM America.

#include "stdio.h"

int 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");
  }
return 0;
}

The output results are as follows:

Title 10: print the stairs, and print two smiling faces above the stairs at the same time.

Program analysis: to display the extended ASCII code, it needs to be displayed under 437 OEM America.

#include "stdio.h"

void main()
{
  int i,j;
  printf("\1\1\n"); /*Output two smiling faces*/
  for(i=1;i<11;i++)
  {
    for(j=1;j<=i;j++)
      printf("%c%c",219,219);
    printf("\n");
  }
}

The output results are as follows:

(welcome to WeChat official account: Notes from Jimmy)

Keywords: C Back-end

Added by killsite on Sun, 20 Feb 2022 03:03:14 +0200