Home >Backend Development >C#.Net Tutorial >10 classic small programs in C language

10 classic small programs in C language

黄舟
黄舟Original
2016-12-12 14:13:402112browse

[Procedure 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?
1. Program analysis: The numbers that can be filled in the hundreds, tens, and ones digits are all 1, 2, 3, and 4. After composing all the permutations, delete the permutations that do not meet the conditions.
2. Program source code:

main() 
{ 
int i,j,k; 
printf("\n"); 
for(i=1;i<5;i++)    /*以下为三重循环*/ 
 for(j=1;j<5;j++)  
  for (k=1;k<5;k++) 
   { 
    if (i!=k&&i!=j&&j!=k)    /*确保i、j、k三位互不相同*/ 
    printf("%d,%d,%d\n",i,j,k); 
    } 
}

[Program 2]

Title: The bonuses issued by the company are based on profits. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%; if the profit is high
  When the amount is less than 100,000 yuan and less than 200,000 yuan, the portion below 100,000 yuan will be commissioned at 10%. The portion above 100,000 yuan will be subject to commission.
  7.5% commission; when it is between 200,000 and 400,000 yuan, the commission is 5% on the part above 200,000 yuan; when between 400,000 and 600,000 yuan, the commission is 5%
  3% commission is available for the portion of 400,000 yuan; 1.5% commission for the portion above 600,000 yuan between 600,000 and 1 million.
  When the amount exceeds 1 million yuan, the commission will be 1% for the portion exceeding 1 million yuan. Enter the profit I of the month from the keyboard. What is the total number of bonuses that should be paid?
1. Program analysis: Please use the number axis to divide and locate. Note that the bonus needs to be defined as an integer when defining.​​​​
2. Program source code:

main() 
{ 
int i,j,k; 
printf("\n"); 
for(i=1;i<5;i++)    /*以下为三重循环*/ 
 for(j=1;j<5;j++)  
  for (k=1;k<5;k++) 
   { 
    if (i!=k&&i!=j&&j!=k)    /*确保i、j、k三位互不相同*/ 
    printf("%d,%d,%d\n",i,j,k); 
    } 
}

[Program 2]

Title: The bonuses issued by the company are based on profits. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%; if the profit is high
  When the amount is less than 100,000 yuan and less than 200,000 yuan, the portion below 100,000 yuan will be commissioned at 10%. The portion above 100,000 yuan will be subject to commission.
  7.5% commission; when it is between 200,000 and 400,000 yuan, the commission is 5% for the part above 200,000 yuan; when between 400,000 and 600,000 yuan, the commission is 5%
  3% commission is available for the portion of 400,000 yuan; 1.5% commission for the portion above 600,000 yuan between 600,000 and 1 million.
  When the amount exceeds 1 million yuan, the commission will be 1% for the portion exceeding 1 million yuan. Enter the profit I of the month from the keyboard. What is the total number of bonuses that should be paid?
1. Program analysis: Please use the number axis to divide and locate. Note that the bonus needs to be defined as an integer when defining.​​​​
2. 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); 
}

[Program 3]

Question: An integer, after adding 100, it becomes a perfect square number, and when added to 168, it becomes a perfect square number. What is this number?
1. Program analysis: To determine within 100,000, first add 100 to the number before prescribing, then add 268 to the number before prescribing, if after prescribing
   The result satisfies the following conditions, that is the result. Please see the specific analysis:
2. Program source code:

#include "math.h" 
main() 
{ 
long int i,x,y,z; 
for (i=1;i<100000;i++) 
 { x=sqrt(i+100);   /*x为加上100后开方后的结果*/ 
  y=sqrt(i+268);   /*y为再加上168后开方后的结果*/ 
   if(x*x==i+100&&y*y==i+268)/*如果一个数的平方根的平方等于该数,这说明此数是完全平方数*/ 
    printf("\n%ld\n",i); 
  } 
}

[Program 4]

Title: Enter a certain day of a certain year, a certain month, and determine what day of the year this day is?
1. Program analysis: Taking March 5th as an example, you should first add up the previous two months, and then add 5 days, which is the day of the year. Special
    case, if it is a leap year and the input month is greater than 3, you need to consider adding an extra day.
2. 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)/*先计算某月以前月份的总天数*/ 
{ 
 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;  /*再加上某天的天数*/ 
 if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/ 
  leap=1; 
 else 
  leap=0; 
if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/ 
sum++; 
printf("It is the %dth day.",sum); }

[Program 5]

Title: Input three integers x, y, z, please output these three numbers from small to large.
1. Program analysis: We find a way to put the smallest number on x. First compare x and y. If x>y, then exchange the values ​​​​of x and y.
    Then use x and z to compare. If x>z, exchange the values ​​​​of x and z, so that x can be minimized.
2. Program source code:

main() 
{ 
int x,y,z,t; 
scanf("%d%d%d",&x,&y,&z); 
if (x>y) 
/*交换x,y的值*/ 
if(x>z) 
/*交换x,z的值*/ 
if(y>z) 
/*交换z,y的值*/ 
printf("small to big: %d %d %d\n",x,y,z); 
}

[Program 6]

Title: Use the * sign to output the pattern of the letter C.
1. Program analysis: You can first write the letter C on the paper using the b4e17c67ac9499e268d9626ccce8267d*b4e17c67ac9499e268d9626ccce8267d symbols, and then output it in separate lines.
2. Program source code:

#include "stdio.h" 
main() 
{ 
printf("Hello C-world!\n"); 
printf(" ****\n"); 
printf(" *\n"); 
printf(" * \n"); 
printf(" ****\n"); 
}

[Program 7]

Title: Output special patterns, please run it in the c environment and take a look, Very Beautiful!
1. Program analysis: There are 256 characters in total. Different characters have different graphics.   
2. 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); }

【Program 8】

Title: Output 9*9 formula.
1. Program analysis: consider rows and columns, there are 9 rows and 9 columns in total, i controls the rows and j controls the columns.
2. 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表示左对齐,占3位*/ 
     } 
   printf("\n");/*每一行后换行*/ 
   } 
}

[Program 9]

Title: Request to output a chess board.
1. Program analysis: Use i to control rows, j to control columns, and control the output of black squares or white squares according to the change of the sum of i+j.
2. 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"); 
  } 
}

[Program 10]

Title: Print the stairs and print two smiley faces above the stairs at the same time.
1. 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 changes in i.
2. Program source code:

#include "stdio.h" 
main() 
{ 
int i,j; 
printf("\n");/*输出两个笑脸*/ 
for(i=1;i<11;i++) 
 { 
 for(j=1;j<=i;j++) 
   printf("%c%c",219,219); 
 printf("\n"); 
  } 
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn