China Naming Network - Ziwei Dou Shu - C language practice

C language practice

You can search the beauty of programming.

-

Program 1

Title: Classic question: There are a pair of rabbits. They have a pair of rabbits every month from the third month after birth, and the rabbits grow to the third month.

After that, a pair of rabbits were born every month. If rabbits don't die, what is the total number of rabbits per month?

1. Program analysis: the rabbit's rule is the sequence of 1, 1, 2,3,5,8,13,21. ....

2. The program source code:

Master ()

{

long f 1,F2;

int I;

f 1 = F2 = 1;

for(I = 1; I & lt=20; i++)

{ printf(" % 12ld % 12ld ",f 1,F2);

if(I % 2 = = 0)printf(" \ n "); /* Control output, four per line */

f 1 = f 1+F2; /* Add the first two months and allocate them to the third month */

F2 = f 1+F2; /* Add the first two months and allocate them to the third month */

}

}

==============================================================

Program 2

Topic: Judge how many prime numbers are between 10 1-200, and output all prime numbers.

1. program analysis: the method of judging the prime number: divide 2 by a number to sqrt (this number), if it can be divisible,

It means that this number is not prime, but prime.

2. The program source code:

# contains "math.h"

Master ()

{

int m,I,k,h=0,leap = 1;

printf(" \ n ");

for(m = 10 1; m & lt=200; m++)

{ k = sqrt(m+ 1);

for(I = 2; I<= k;; i++)

If (m%i==0)

{ leap = 0; Break; }

if(leap) {printf("%-4d ",m); h++;

if(h% 10==0)

printf(" \ n ");

}

leap = 1;

}

Printf ("\ nTotal is %d", h);

}

==============================================================

Program 3

Title: Print out all "Narcissus". The so-called "daffodil number" refers to a three-digit number, and the cubic sum of its digits is equal to this number.

In itself. For example, 153 is a "daffodil number" because 153= 1 cubic +5 cubic +3 cubic.

1. Program analysis: Use for loop to control the number of 100-999, and each number is decomposed into units, tens and hundreds.

2. The program source code:

Master ()

{

int i,j,k,n;

Printf ("Splash" is ");

for(n = 100; n & lt 1000; n++)

{

I = n/ 100; /* Break down into hundreds of */

j = n/ 10% 10; /* Break down into ten digits */

k = n % 10; /* Break down into bits */

if(I * 100+j * 10+k = = I * I * I+j * j * j+k * k * k)

{

printf("%-5d ",n);

}

}

printf(" \ n ");

}

==============================================================

Procedure 4

Topic: decompose a positive integer into prime factors. For example, enter 90 and print 90=2*3*3*5.

Program analysis: to decompose the prime factor of n, we must first find a minimum prime number k, and then complete it according to the following steps:

(1) If this prime number is just equal to n, it means that the process of decomposing the prime factor is over, just print it out.

(2) If n

Repeat the first step.

(3) If n is not divisible by k, use k+ 1 as the value of k and repeat the first step.

2. The program source code:

/* Zheng int was carved up */

Master ()

{

int n,I;

Printf ("\ nPlease enter a number: \ n");

scanf("%d ",& ampn);

printf("%d= ",n);

for(I = 2; I < = n; i++)

{

And (n! =i)

{

If (n%i==0)

{ printf("%d* ",I);

n = n/I;

}

other

Break;

}

}

printf("%d ",n); }

==============================================================

Plan 5

Topic: use the nesting of conditional operators to complete this question: academic performance >; = Students with 90 points are represented by A, and students with 60-89 points are represented by B,

Those below 60 points are indicated by C.

1. program analysis: (a>b)? This is a basic example of a conditional operator.

2. The program source code:

Master ()

{

Int score;

Carbonization grade;

Printf ("Please enter a score \ n");

Scanf("%d ",& score);

Grade = score & gt=90? A': (Score & gt=60? b ':' C ');

Printf("%d belongs to %c ",score, grade);

}

==============================================================

Program 6

Title: Enter two positive integers M and N, and find their greatest common divisor and least common multiple.

1. Program analysis: using rolling division.

2. The program source code:

Master ()

{

int a,b,num 1,num2,temp

Printf ("Please enter two numbers: \ n");

scanf("%d,%d ",& ampnum 1。 num 2);

if(num 1 { temp = num 1;

num 1 = num 2;

num2 = temp

}

a = num 1; b = num2

And (b! =0)/* Use rolling division until b is 0 */

{

temp = a % b;

a = b;

B = temperature;

}

printf("gongyueshu:%d\n ",a);

Printf ("Gongbeishu: %d\n", num1* num 2/a);

}

==============================================================

Procedure 7

Title: Enter a line of characters and count the number of English letters, spaces, numbers and other characters.

1. Program Analysis: Use the while statement, provided that the input character is not' \n'.

2. The program source code:

# contains "stdio.h"

Master ()

{ char c;

Int letters =0, spaces =0, numbers =0, others = 0;

Printf ("Please enter some characters \ n");

while((c=getchar())! ='\n ')

{

if(c & gt; = ' a ' & amp& ampc & lt= ' z ' | | c & gt= ' A ' & amp& ampc & lt='Z ')

Letter++;

else if(c== ' ')

space++;

else if(c & gt; = ' 0 ' & amp& ampc & lt='9')

digit++;

other

Other++;

}

Printf ("all in all: char =% d space =% d digit =% d others =% d \ n", letters,

Spaces, numbers, others);

}

==============================================================

Program 8

Topic: Find the value of s=a+aa+aaa+aaaa+aa ... A, where A is a number. For example, 2+22+222+2222+22222 (at this time

* * * added five numbers), and the addition of several numbers is controlled by the keyboard.

1. Program analysis: The key is to calculate the value of each item.

2. The program source code:

Master ()

{

int a,n,count = 1;

long int sn=0,TN = 0;

Printf ("Please enter a and n \ n");

Scanf("%d, %d ",& i, & n);

printf("a=%d,n=%d\n ",a,n);

while(count & lt; =n)

{

TN = TN+a;

sn = sn+TN;

a = a * 10;

++counting;

}

Printf("a+aa+...=%ld\n ",serial number);

}

==============================================================

Program 9

Title: If a number is exactly equal to the sum of its factors, it is called "perfect number". Such as 6 = 1+2+3. Programming; arrange

Find all perfect numbers within 1000.

1. Scheme analysis: please refer to the scheme.

2. The program source code:

Master ()

{

Static int k [10];

int i,j,n,s;

for(j = 2; j & lt 1000; j++)

{

n =- 1;

s = j;

for(I = 1; I {

If ((j%i)==0)

{ n++;

s = s-I;

k[n]= I;

}

}

If (s==0)

{

Printf("%d is a million trees ",j);

for(I = 0; i printf("%d ",k[I]);

printf("%d\n ",k[n]);

}

}

}

==============================================================

Program 10

Title: A ball falls freely from the height of 100 meters, and bounces back to half the original height after each landing; Fall again and ask it to be there.

* * * How many meters did the 10 landing pass? How high is the rebound of 10?

1. Program analysis: See the following note.

2. The program source code:

Master ()

{

float sn= 100.0,HN = sn/2;

int n;

for(n = 2; n & lt= 10; n++)

{

sn = sn+2 * HN; /* The number of meters passed by * * * when landing for the nth time */

HN = HN/2; /* The height of the nth rebound */

}

Printf ("Total roads are %f\n", sn);

Printf ("the tenth is %f meters \n", HN);

}

Program 1 1

Title: There are 1, 2, 3, 4 numbers. How many different three digits can you make up? How much are they altogether?

1. Program analysis: The numbers that can be filled in in hundreds, tens and units are all 1, 2, 3 and 4. Make all the arrangements before you leave.

Delete unqualified arrangements.

2. The program source code:

Master ()

{

int i,j,k;

printf(" \ n ");

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

for(j = 1; j & lt5; j++)

for(k = 1; k & lt5; k++)

{

If (me! = k & me! = j & amp& ampj! =k) /* Make sure that I, J and K are different from each other */

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

}

}

==============================================================

Program 12

Title: Bonuses paid by enterprises are based on profits. When the profit (i) is less than or equal to 6,543,800 yuan, the bonus can be increased by 654.38+ 00%; High profit

When it is 654.38+ 10,000 yuan, if it is less than 200,000 yuan, the commission of 654.38+ 00% will be paid for the part below 654.38+ 10,000 yuan, and the part above 654.38+10,000 yuan can be withdrawn.

7.5%; Between 200,000 yuan and 400,000 yuan, 5% can be deducted for the part above 200,000 yuan; Between 400 thousand and 600 thousand is higher than

The part of 400,000 yuan can be deducted by 3%; Between 600,000 yuan and 6,543,800 yuan, the part above 600,000 yuan can be deducted by 654.38+0.5%, which is higher than.

1 ten thousand yuan, the part exceeding 1 ten thousand yuan will be deducted 1%. Enter the profit I of the current month from the keyboard and ask for the total bonus.

1. Program analysis: Please calibrate and locate with several axes. Note that the bonus should be defined as an increasing integer.

2. The program source code:

Master ()

{

long int I;

int bonus 1,bonus2,bonus4,bonus6,bonus 10,bonus;

scanf("%ld ",& ampI);

bonus 1 = 100000 * 0. 1; bonus 2 = bonus 1+ 100000 * 0.75;

bonus 4 = bonus 2+200000 * 0.5;

bonus 6 = bonus 4+200000 * 0.3;

bonus 10 = bonus 6+400000 * 0. 15;

If (I < =100000)

Bonus = I * 0.1;

else if(I & lt; =200000)

bonus = bonus 1+(I- 100000)* 0.075;

else if(I & lt; =400000)

bonus = bonus 2+(I-200000)* 0.05;

else if(I & lt; =600000)

bonus = bonus 4+(I-400000)* 0.03;

else if(I & lt; = 1000000)

bonus = bonus 6+(I-600000)* 0.0 15;

other

bonus = bonus 10+(I- 1000000)* 0.0 1;

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

}

==============================================================

Program 13

Title: An integer, after adding 100, is a complete square number, and after adding 168, is a complete square number. What is the number?

1. Scheme analysis: If it is judged to be within 65438+ million, add 100 to this number before prescribing, and add 268 to this number before prescribing again. If it is stipulated,

The result is the result if the following conditions are met. Please see the specific analysis:

2. The program source code:

# contains "math.h"

Master ()

{

long int i,x,y,z;

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

{ x = sqrt(I+ 100); /*x is the result of adding 100 to the prescription */

y = sqrt(I+268); /*y is the result of adding 168 */

if(x * x = = I+ 100 & amp; & ampY*y==i+268)/* If the square of the square root of a number is equal to the number, it means that the number is completely squared */

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

}

}

==============================================================

Program 14

Title: Enter a day of a year to decide which day of the year it is.

1. Scheme analysis: Take March 5th as an example, first add up the first two months, and then add five days, which is the day of this year, which is quite special.

If it is a leap year, if the input month is greater than 3, you should consider adding one more day.

2. The program source code:

Master ()

{

Int day, month, year, harmony and leap;

Printf ("\ nPlease enter year, month and day \ n");

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

Switch(month)/* Calculate the total number of days in the month before a month */

{

Case1:sum = 0; Break;

Case 2: sum = 31; Break;

Case 3: sum = 59 broken;

Case 4: sum = 90 is broken;

Scenario 5: sum =120; Break;

Case 6: sum =151; Break;

Case 7: sum =181; Break;

Case 8: sum = 212; Break;

Case 9: sum = 243; Break;

Case10: sum = 273; Break;

Case11:sum = 304; Break;

Case12: sum = 334; Break;

Default value: printf ("data error"); Break;

}

Sum = sum+days; /* plus the number of days in a day */

if(year % 400 = = 0 | |(year % 4 = = 0 & amp; & amp Year% 100! =0))/* Judge whether it is a leap year */

leap = 1;

other

Leap = 0;

if(leap = = 1 & amp; & Month & gt2)/* If it is a leap year, if the month is greater than 2, the total number of days will be added by one day */

sum++;

Printf ("Today is the %dth day." ,sum); }

==============================================================

Program 15

Title: Enter three integers X, Y, z Y, 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 and y first. If X >;; Exchange the values of x and y,

Then compare x and z, if x >;; Z exchange the values of x and z to minimize x.

2. The program source code:

Master ()

{

int x,y,z,t;

scanf("%d%d%d ",& ampx & amp; y & amp; z);

if(x & gt; y)

{ t = x; x = y; y = t; }/* Exchange the values of x and y */

if(x & gt; z)

{ t = z; z = x; x = t; }/* Exchange the values of x and z */

If (y>z)

{ t = y; y = z; z = t; }/* Exchange the values of z and y */

Printf ("from small to large: %d %d %d\n", x, y, z);

}

==============================================================

Program 16

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

1. program analysis: you can write the letter c on paper with' *' first, and then output it line by line.

2. The program source code:

# contains "stdio.h"

Master ()

{

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

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

printf(" * \ n ");

printf(" * \ n ");

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

}

==============================================================

Program 17

Topic: output special patterns, please run it in C environment, it is very beautiful!

1. Program analysis: there are 256 characters * * *. Different roles, different graphics.

2. The program source code:

# contains "stdio.h"

Master ()

{

char a= 176,b = 2 19;

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 18

Title: Output 9*9 formula.

1. Program analysis: branch columns are considered, ***9 rows and 9 columns, I control row and J control column.

2. The program source code:

# contains "stdio.h"

Master ()

{

int i,j,result

printf(" \ n ");

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

{ for(j = 1; j & lt 10; j++)

{

Result = I * j;;

printf("%d*%d=%-3d ",I,j,result); /*-3d means left alignment, accounting for 3 digits */

}

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

}

}

==============================================================

Plan 19

Title: output chessboard is required.

1. Program analysis: use I to control rows, use J to control columns, and control whether to output black squares or white squares according to the change of the sum of I+J.

2. The program source code:

# contains "stdio.h"

Master ()

{

int i,j;

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

{

for(j = 0; j & lt8; j++)

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

printf("%c%c ",2 19,2 19);

other

printf(" ");

printf(" \ n ");

}

}

==============================================================

Plan 20

Title: Print the stairs and print two smiling faces at the top of the stairs at the same time.

1. Program analysis: I control row, J control column, and J control the number of output black squares according to the change of I.

2. The program source code:

# contains "stdio.h"

Master ()

{

int i,j;

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

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

{

for(j = 1; j & lt= I; j++)

printf("%c%c ",2 19,2 19);

printf(" \ n ");

}

}

Procedure 2 1

Topic: Monkeys eat peaches: On the first day, monkeys picked many peaches and ate half of them at once. He's not addicted. He ate another one.

The next morning, I ate half the remaining peaches and another one. I eat leftovers from the day before every morning.

Half and one. 10 in the morning, when I wanted to eat again, I saw only one peach left. Ask how much you picked on your first day.

1. program analysis: adopt the method of reverse thinking and infer from back to front.

2. The program source code:

Master ()

{

int day,x 1,x2;

Day = 9;

x2 = 1;

while(day & gt; 0)

{ x 1 =(x2+ 1)* 2; /* The number of peaches on the first day is twice that on the second day plus 1 */

x2 = x 1;

Day-;

}

Printf ("Total %d\n", x1);

}

==============================================================

Plan 22

Title: Two table tennis teams compete, each with three players. Team A is made up of three people, A, B and C, and Team B is made up of three people, X, Y, Z, Y and Z. It has been decided by drawing lots.

Competition list. Someone asked the players about the list of matches. A says he won't compare with X, and C says he won't compare with X and Z. Please make a program to find out.

A list of the players from the third team.

1. program analysis: the method of judging the prime number: divide 2 by a number to sqrt (this number), if it can be divisible,

It means that this number is not prime, but prime.

2. The program source code:

Master ()

{

char i,j,k; /*i is the opponent of A, J is the opponent of B and K is the opponent of C */

for(I = ' x '); I<=' zi++)

for(j = ' x '); j & lt= ' zj++)

{

If (me! =j)

for(k = ' x '); k & lt= ' zk++)

{If (I! = k & amp& ampj! =k)

{If (I! = ' x ' & amp& ampk! = ' x ' & amp& ampk! ='z ')

Printf("order is a-%c\tb-%c\tc-%c\n ",I, J, K);

}

}

}

}