Fundamentals of C language programming (simple algorithm)

catalogue

preface

               1,Hello world!

                2. Piecewise function

      3. Cumulative calculation from 1 to n (use the for loop and save the cumulative sum with the variable sum)

      4. The cumulative variant changes the symbol for operation (for loop basis, symbol transformation)

  5. Factorial (for loop) simple factorial n!

  6.1001 (3n+1) conjecture that killing people does not pay for their lives   Pat class B

summary

preface

Just a freshman, less than a month after admission, there are many problems, which are recorded by blog and fermented slowly

In order to consolidate the new knowledge and improve their own foundation, a simple algorithm is close-up of this article

Some examples come from the school OJ and the C language programming issued by the school (written by he Qinming)

1,Hello world!

This is my first language c, from here on Hello world!

#include<stdio.h>
int main(){
printf("Hello world!\n");	
return 0;
}

2. Piecewise function

                In order to encourage residents to save water, water companies adopt the method of charging water consumption by sections on a monthly basis. The functional expression of water fee y (yuan) payable by residents and monthly water consumption x (ton) is as follows (assuming x > = 0). Input the user's monthly water consumption x (ton), calculate and output the water fee y (yuan) (keep two decimal places) 4*x/3       x<=15

      y=f(x)={

                2.5x-10.5  x>15

#include<stdio.h>
int main(){
double  x,y;//Double is a double precision floating-point variable. Compared with float, it occupies more space, has higher precision and wider value range  
printf("Enter x= \n");//"\ n" special character (line feed)  
scanf("%lf",&x);//The format control description for double data is "% lf". Note that l is the first letter of long  
/*lf - else sentence*/
if(x<=15){
    y=4*x/3;
}else{
    y=2.5*x-10.5;
}
printf("y=f(%f)=%.2f\n",x,y);//The control description of double data output format is "% f", and. 2 in%. 2f means to keep two decimal places  

return 0;
}

3. Cumulative calculation from 1 to n (use the for loop and save the cumulative sum with the variable sum)

 

#include<stdio.h>
int main(){
   int n,sum;
 
      printf("Enter=");
      scanf("%d",&n);
    sum=0;                   //A storage space is set to save the accumulated value of i cycle, and the initial value is cleared
 
for(int i=1;i<=n;i++){      /*The number of cycles is n, let i add from 1 until the value is equal to n,
                             for The loop circulates when I < = n is satisfied*/                              
     sum+=i;                 //Equivalent to sum=sum+i; It means sum plus the value of i to sum  
 }
printf("Sum of numbers from 1 to %d is %d\n",n,sum);
 
return 0;
}

4. The cumulative variant changes the symbol for operation (for loop basis, symbol transformation)

  Almost the difference from accumulation is that the symbols have fluctuating transformation

#include<stdio.h>
int main(){
   int n,flag;
double sum,m;          //Double precision floating point variable
      printf("Enter="); 
      scanf("%d",&n);
      
sum=0;flag=1; m=1;       
  for(int i=1;i<=n;i++){  
     sum+=m;           //sum=sum+m 
     flag=-flag;       //Symbol conversion      
     m=flag*1.0/(i+2); //To prepare for the next addition with sum, i participates in the operation    
 }
     printf("sum=%f\n",sum);
    return 0;
}

  5. Factorial (for loop) simple factorial n!

  And accumulation are almost all accumulated and multiplied through the for loop

#include<stdio.h>
int main(){
   int n,sum;
 
      printf("Enter = ");
      scanf("%d",&n);
    sum=1;               //Note that the initial value of sum is 1 
for(int i=1;i<=n;i++){      
sum*=i;                    //Much like adding from 1 to n, each time the product of sum and i is assigned to sum, i is added to n by itself 
 }
printf("from%d Multiply to 1 (factorial) sum= %d\n",n,sum);

return 0;
}

 

  6.1001 (3n+1) conjecture that killing people does not pay for their lives   Pat class B

Note: the title comes from Pat. If you have any violations, please leave a message here for reference only, not for commercial use. Please understand. Thank you!

Plus a Pat grade B. this question is written in c + +, which is still very different for my beginner. If you have any comments, please leave a message. Thank you!

Callatz conjectures:

For any positive integer   n. If it is even, cut it in half; If it's an odd number, put it   (3n+1)   Cut it in half. This has been repeatedly cut down, and finally you must get it at a certain step   n=1. Karaz announced this conjecture at the World Conference of mathematicians in 1950. It is said that at that time, the teachers and students of Yale University mobilized together to try their best to prove this seemingly silly and naive proposition. As a result, the students did not want to study and only proved it   (3N + 1), so that some people say it is a conspiracy. Karaz is deliberately delaying the progress of teaching and scientific research in American Mathematics

Our topic today is not to prove the karatz conjecture, but for a given positive integer no more than 1000   n. Simply count how many steps it takes to get it   n=1?

Input format:

Each test input contains one test case, which gives a positive integer   n   Value of.

Output format:

Output from   n   Calculate the number of steps required to 1.

#include<iostream>
#include<cmath>

 using namespace std;
 int main(){
int a;
cin>>a;
int i=0;

while(a>1){
	if(a%2==0){
	  a/=2;
	}
	else a=(3*a+1)/2;
	i++;
	
}
cout<<i;

}

 

summary

These 20 days of study let me know how wide the bottomless knowledge ocean of network technology is. At present, there is too much lack of system knowledge and too little professional knowledge, so I can only supplement it slowly. If there are any mistakes above, please correct them. If you have any good suggestions or ideas, please leave a message! During this period of time, strive to put all the awareness methods about simple algorithms in this book in this column, and keep them for future consolidation of the foundation, so as to strive for the big factory!

Keywords: C Algorithm

Added by ngreenwood6 on Wed, 27 Oct 2021 18:06:25 +0300