Introduction to C language

1, Loop (while)

1. How does the computer determine the number of digits?

(1) . the size range of the number of customs clearance judgments by the computer:

//The number shall not be greater than five digits; 
	int x;
	int n=0;
	scanf ("%d",&x);
	if (9999>x>999){
    	n=4;
	}else if (999>x>99){
    	n=3;
	}else if (99>x>9){
    	n=2;
	}else 
    	n=1;
	printf ("%d\n",n);

(2) Let the computer judge through circulation:

    int x;
    int n=0;
	scanf("%d",&x);
    n++;
    x/=10;
	while(x>0){
   		n++;
    	x/=10;
    }
	printf("%d\n",n);

2. Do while loop

(1) . structure:

do
{
    <Loop body statement>
}while (<Cycle condition>);

(2) The difference between, while and do while: the former judges first, and the latter carries out the loop body first.

(3) . optimize the above procedures:

    int x;
    int n=0;
	scanf("%d",&x);
	do
    {
        n++:
        x/=10;
    } while (x>0);
	printf("%d\n",n);

3. Cyclic calculation

(1) Calculate the true number with two as the bottom of a number:

int x;
int ret=0;
scanf ("%d",&x);
int t=x;
while (x>0) {
    x/=2;
    ret ++;
}
printf ("log2 of %d is %d.",t,ret);

(2) . counting cycle

int count=100;
while ( count >=0 ){
    count --;                  print("%d",count);
    print("%d",count);         count --;
}
printf ("Launch!\n");

Both sides have different orders and different outputs, but the number of cycles is the same and the value after the cycle is over is the same.

2, Average

A variable records the result of accumulation, and a variable records the number of reads.

1. Using the do while structure, you can judge number twice, which is more wasteful;

int number;
int sum = 0;
int count = 0;

do
{
    scanf ("%d",&number);
	if (number != -1) {
    sum += number;
    count ++;
    }
} while (number != -1);
printf ("%f\n",1.0*sum/count);

2. Use the while statement to do scanf twice. If there is no second time, the loop cannot stop or times out;

int sum = 0;
int number;
int count = 0;

scanf ("%d",&number);
while (number != -1) {
    sum += number;
    count ++;
    scanf ("%d",&number);
}
printf ("%f\n",1.0*sum/count);

3, Guessing game

1. Rule: let the computer think of a number, and then let the user guess. Every time the user enters a number, he will tell him whether it is big or small until the user guesses it correctly. Finally, he will tell the user how many times he guessed.

2. Text description: (the condition of the loop is that a and number are not equal)

(1) The computer thinks of a number randomly and records it in the variable number;

(2) . a variable count responsible for counting times is initialized to 0;

(3) Let the user input a number a;

(4) . count increment (plus one);

(5) Judge the size relationship between a and number. If a is large, output "large"; If a is small, output "small";

(6) If a and number are not equal (large or small), the program returns to step 3;

(7) Otherwise, the program outputs "guess" and times, and then ends.

3. Get a random number through rand().

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()

{
srand(time(0));
int number = rand()%100+1;
int count = 0;
int a = 0;

printf("I've figured out a number between 1 and 100.");

do {
printf("Please guess the number between 1 and 100:");
scanf("%d", &a);

if ( a > number ) {
printf("Your guess is too big.");
} else if ( a < number ) {
printf("Your guess is too small.");
}

count ++;
} while (a != number);

printf("Great, you used it%d I guessed the answer once.\n", count);


return 0;

}

4, Integer inversion

Requirements: input a number and output it in reverse order.

In the loop: first take the remainder to get the single digit, then output digit, and then assign it to ret. at the end, divide x by ten, and output the result when the conditions are not met.

    int x;
    scanf ("%d",&x);
    int digit = 0;
    int ret = 0;

    while (x > 0){
    	digit = x % 10;
    	printf("%d",digit);
    	ret = ret * 10 + digit;
    	x /= 10;
	}

Keywords: C Algorithm

Added by dineshsjce on Fri, 01 Oct 2021 05:25:06 +0300