Categories
Uncategorized

C language portal – cycling

A circulation

A digital input, the digital output Several

#include  

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

Two, while circulation

Loop body should have the opportunity to change conditions, to avoid infinite loop

while loop means: when the condition, constantly repeating cycle statements vivo

Determine whether to continue the cycle before the loop is executed, it is possible once the cycle has not been executed

We look at the code above, if a bit redundant components, we have to transform, but there is a problem is when there is no input 0-digit

#include  

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

Three, do-while loop

Not checked when entering the loop, and then checks the condition after performing a further, at least one cycle is performed following format

do{
    循环体
}whlie(条件)

The median number of algorithms

    User input x

    N is initialized to 0

    x = x / 10, bits removed

  1. n++
  2. If n> 0, 3 back

    Otherwise, the result is n

The above code transformation

#include  

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

Is 0 when the input bits may be displayed,

Fourth, the calculation cycle

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

Guess the number game

Want to make a digital computer, and then let the user to guess a user input for each array, tell him is big or small, until the user guessed so far, and tell the user to guess how many times

This uses a random number

#include 
#include 
#include 

int main(){
    srand(time(0));
    int a = rand();
    
    // 但是这个随机数有点大,这里取余100 
    printf("%d\n" , a%100);
    
    return 0;
}

Specific implementation is as follows:

#include 
#include 
#include 

int main(){
    srand(time(0));
    
    int number = rand()%100+1;
    int a = 0;
    int count = 0;
    // 用来作弊 
    // printf("%d\n",number); 
    
    while(number != a){
        printf("亲,请输入100以内的数字:");
        scanf("%d", &a);
        if(a > number){
            printf("亲,你输入的数字大了\n");
        }else if(a < number){
            printf("亲,你输入的数字小了\n");
        }
        count++; 
    }
    printf("恭喜你,输入正确!你一共输入了%d次" , count);
    return 0;
}

The average count

Allowing users to enter a series of positive integers, and enter the end -1 for input, and then calculate the average of these numbers

    int number;
    int count = 0;
    int sum = 0;
    
    printf("请输入数字(-1结束):");
    scanf("%d" , &number);
    while(number != -1){
        printf("请输入数字(-1结束):");
        count++;
        sum += number;
        scanf("%d" , &number);
    }
    
    printf("你一共输入了%d次,平均数为%d", count , sum/count) ;

Fifth, the inverse integer

Do integer operations for a 10%, just to get his single-digit

Do integer operations on a / 10, you get rid of his single-digit

Requirements: enter a positive integer, the number of reverse input

    int number;
    int digit;
    int ret = 0;
    printf("请输入数字:");
    scanf("%d" , &number) ;
    
    while(number != 0){
        digit = number%10;
        //printf("%d",digit);
        ret = ret * 10 + digit;
        printf("x=%d,digit=%d,ret=%d\n", number, digit, ret);
        number /= 10;
    }
    printf("%d",ret);
    

Six, for circulation

Like a for loop counting cycle, a counter is set, initialize it, and before the counter reaches a certain value, the loop is repeatedly executed, and each performs a cycle, a constant calculator adjustment steps.

Calculation of n!

#include 

int main(){
    int n;
    scanf("%d" , &n);
    int fact = 1;
    
//  for(int i = 1; i<=n; i++){
//      fact *= i;
//  }
    
    for(int i = n; i>1; i--){
        fact *= i;
    }
    
    printf("%d!=%d" , n , fact);
    
    return 0;
}

Loop calculation and selection

for (the initial operation; conditions; round operation) {

Loop body;

}

for each of the expressions are omitted, the semicolon should not be spared

#include 

int main(){
    int i;
    for(i = 0; i<5; i++){
        printf("i=%d\n" , i);
    }
    printf("\n最后的i=%d\n",i);
    
    return 0;
}
i=0
i=1
i=2
i=3
i=4
最后的i=5

    If there is a fixed number of times, for loop

    If you must perform once with do_while

    While other cases with

Seven cycle control

Prime numbers: only divisible by themselves and 1, excluding 1

    
    int x;
    scanf("%d" , &x);
    int i;
    int isPrime = 1;
    for(i=2; i

Nested loop

Prime number less than 100

#include  

int main(){
    int x;
    
    for( x=1; x<=100; x++){
        int i;
        int isPrime = 1;
        for(i=2; i

This may be, and the 50 primes

#include  

int main(){
    int x;
    int cnt = 0;
    // for( x=1; cnt<50; x++){
    x = 1;
    while(cnt < 50){
        int i;
        int isPrime = 1;
        for(i=2; i

Jump from a nested loop

break and that it can only continue the cycle where the layer,

goto: Jump directly to your designated location

Minato coins

How to 10 cents, 20 cents, 50 cents coin Couchu amount less than $ 10, Relay break, too much trouble

    int x;
    int exit = 0;
    int one, two, five;
    printf("请输入金额:"); 
    scanf("%d", &x);
    for(one = 1; one < x *10; one++){
        for(two=1; two < x*10/2; two++){
            for(five=1; five < x*10/5; five++){
                if(one + two*2 + five*5 == x*10){
                    printf("可以用%d个1角和%d个2角和%d个5角的组成%d元\n",one ,two,five,x);
                    exit = 1;
                    break;
                }
            }
            if (exit == 1) break;
        }
        if (exit == 1) break;
    
    }

goto, when there are multiple layers can use this cycle, other times not to use

#include  

int main(){
    int x;
    int one, two, five;
    printf("请输入金额:"); 
    scanf("%d", &x);
    for(one = 1; one < x *10; one++){
        for(two=1; two < x*10/2; two++){
            for(five=1; five < x*10/5; five++){
                if(one + two*2 + five*5 == x*10){
                    printf("可以用%d个1角和%d个2角和%d个5角的组成%d元\n",one ,two,five,x);
                    // 直接跳到你指定的位置 
                    goto out;
                }
            }
        }
    
    }
out:
    return 0;
}

Eight, cycle applications

N terms before summing

$$
f(n) = 1+1/2+1/3 +....+1/n
$$

#include  

int main(){
    int n;
    int i;
    double sum = 0.0;
    
    scanf("%d" , &n);
    
    for(i=1; i<=n; i++){
        sum += 1.0/i;
        printf("%f\n", sum);
    }

    return 0;
}

But this?
    $$
    f (n) = 1-1 / 2 + 1 / 3-1 / 4 + ... + 1 / n
    $$
    This method is more and more, here I wrote a

#include  

int main(){
    
    int n;
    int i;
    double sum = 0.0;
    
    scanf("%d" , &n);
    
    for(i=1; i<=n; i++){
        if(i%2 == 0){
            sum -= 1.0/i;
        }else{
            sum += 1.0/i;
        }
        printf("%f\n", sum);
    }

    return 0;
    
}

Integer factorization

    Enter a non-negative integer, its positive sequence output each digit

    Input: 1314

    Output: 1314

#include  

int main(){

    int x;
    printf("输入任意位数数字:\n");
    scanf("%d" , &x);

    // 记录位数
    int mask = 1;
    // 输入的值赋值给t,用于记录输入数字的位数
    int t = x;
    while( t > 9){
        t /= 10;
        mask *= 10;
    }


    // 用户输入进来的值,以及用户输入值的位数
    printf("x=%d, mask=%d\n",x, mask );

    do{
        int d = x / mask;

        printf("%d",d );
        // 最后一位不用输出空格
        if (mask > 9)
        {
            printf(" ");
        }

        // 得到他的个位数
        x %= mask;
        // 去掉它的个位数
        mask /= 10;

    }while( mask >0);

    printf("\n");


    return 0;
}

Greatest common divisor

    And a number of two input b, output their greatest common divisor

    Input: 1218

    Output: 6

Ordinary computing

    int a,b;
    int min;

    printf("请输入两个数字:");
    scanf("%d %d" , &a, &b);
    if(a < b){
        min = a;
    }else{
        min = b;
    }

    int ret = 0;
    int i;
    for (i = 1; i <= min; i++)
    {
        if ( a%i == 0)
        {
            if ( b%i == 0)
            {
                ret = i;
            }
        }
    }

    printf("%d和%d的最大公约数是%d\n", a , b , ret );

But there are even more powerful

    If b is equal to 0, the end of the calculation, is the greatest common divisor of A

    Otherwise, the calculation of the remainder of a divided by b, so that a is equal to b, and b is equal to the remainder

    The first step back

t (remainder)

a b
12 18 12
18 12 6
12 6 0
6 0

code show as below:

#include  

int main(){

    int a,b;
    int t;
    scanf("%d %d" , &a , &b);

    while(b != 0){
        t = a % b;
        a = b;
        b = t;121
        printf("%d,%d\n", a ,b);
    }

    printf("最大的公约数是%d\n", a);

    return 0;
}

Leave a Reply