1
Arbitrary input of three integers, programming to sort the three integers from small to large.
Implementation process:
(1) Define the data type. In this example, a, b, c and t are all basic integers.
(2) Use the input function to obtain any three values and assign them to a, b and c.
(3) Use the if statement to make conditional judgment. If a is greater than b, exchange the values of a and b with the help of the intermediate variable t, and compare a and c, b and c by analogy. The final result is the ascending arrangement of a, b and c.
(4) Use the output function to output the values of a, b and c in turn
#include <stdio.h> int main() { int a,b,c,t; /*Define four basic integer variables a, b, c and t*/ printf("Please input a,b,c:\n"); /*Ordinary characters in double quotation marks are output as is and wrapped*/ scanf("%d,%d,%d",&a,&b,&c); /*Enter any 3 numbers*/ if(a>b) /*If a is greater than b, the values of a and b can be exchanged with the help of the intermediate variable t*/ { t = a; a = b; b = t; } if(a>c) /*If a is greater than c, the values of a and c can be interchanged with the help of intermediate variable t*/ { t = a; a = c; c = t; } if(b>c) /*If b is greater than c, the intermediate variable t is used to realize the exchange of b and c values*/ { t = b; b = c; c = t; } printf("The order of the number is:\n"); printf("%d,%d,%d",a,b,c); /*The output function sequentially outputs the values of a, b and c*/ return 0; }
2
Prime numbers are also called prime numbers. A prime number is a number that cannot be divided by any integer except 1 and itself. For example, 17 is a prime number because it cannot be divided by any integer from 2 to 16.
Train of thought 1): therefore, to judge whether an integer m is a prime number, you only need to remove m from each integer between 2 and M-1. If it cannot be divided, then M is a prime number.
#include <stdio.h> int main(){ int a=0; // Number of prime numbers int num=0; // Integer entered printf("Enter an integer:"); scanf("%d",&num); for(int i=2;i<num;i++){ if(num%i==0){ a++; // Prime number plus 1 } } if(a==0){ printf("%d It's a prime.\n", num); }else{ printf("%d Not prime.\n", num); } return 0; }
3
Monkey eating peach problem: the monkey picked several peaches on the first day and ate half of them immediately. It was not fun, so he ate another one. The next morning, he ate half of the remaining peaches from the first day and ate one more. After that, I ate the remaining half and one every morning. When I wanted to eat again on the 10th morning, I found that there was only one peach left. Write a program to ask how many peaches the monkey picked on the first day.
Implementation process:
(1) Define day, x1 and X2 as basic integers, and assign initial values 9 and 1 to day and x2.
(2) Use the while statement to push the number of peaches picked on the first day from the back to the front.
(3) Output results.
(4) The program code is as follows:
#include <stdio.h> int main() { intday,x1,x2; /*Define day, x1 and X2 as basic integers*/ day=9; x2=1; while(day>0) { x1=(x2+1)*2; /*The number of peaches on the first day is twice the number of peaches plus 1 on the second day*/ x2=x1; day--; /*The number of days decreases because it is pushed back and forward*/ } printf("the total is %d\n",x1); /* Total number of output peaches*/ return 0; }
4
Armstrong number, commonly known as daffodil number, refers to a three digit number, and the cubic sum of each digit is equal to the number itself. For example: 153 = 13 + 53 + 33, so 153 is a number of daffodils. Find the number of all daffodils.
Algorithmic thought
For the problem of Armstrong number, according to the definition of daffodil number, it is necessary to separate single digit, ten digit and hundred digit. Then calculate and judge according to its nature. If the conditions are met, it will be printed out, otherwise it will not be printed out.
Therefore, the Armstrong number problem can be solved by circular statements. Let the cyclic variable be i, the initial value be 100, and i change from 100 to 1000; Judge whether the conditions are true in turn. If they are true, they will be output; otherwise, they will not be output.
The algorithm idea is as follows:
① Separate the single digit, and the arithmetic expression is: j=i%10.
② Separate the ten digits, and the arithmetic expression is: k=i/10%10.
③ Separate the hundreds, and the arithmetic expression is: n=i/100.
④ Judge whether the conditions are tenable. If yes, proceed to step ⑤; If not, proceed to step ⑥.
⑤ Print out the results.
⑥ i self increasing 1.
⑦ Go to ① and execute until i equals 1000.
The judgment condition is: jjj+kkk+nnn==i.
#include <stdio.h> int main() { int i,j,k,n; for(i=100;i<1000;i++) { j=i%10; k=i/10%10; n=i/100; if(j*j*j+k*k*k+n*n*n==i) printf("%5d\n",i); } return 0; }
C language for palindrome prime
Any integer, when read from left to right and read from right to left are the same and prime, is called palindrome prime. Find all palindrome prime numbers within 1000.
Algorithmic thought
The key point of the example is to judge whether a number is a palindrome prime number. To output all palindrome prime numbers within 1000, first judge whether this number is a prime number; If yes, further judge whether the number is two or three digits. If it is two digits, judge whether the single digit and ten digit are the same; If it is three digits, it is necessary to judge whether the single digit and the hundred digit are the same. If it is the same, it will be judged as palindrome prime, otherwise continue to judge next time.
① Define a function sushu, which is used to judge whether a number is a prime number.
② For the number judged as prime number, judge whether it is two digits.
If it is two digits, then judge whether the single digit and ten digit are the same. If they are the same, print out; If not, execute ④; If it is not two digits, execute ③.
③ If it is three digits, judge whether the single digit and hundred digit are the same. If the same, print out; If not, execute ④.
④ The cyclic control variable i increases by 1.
⑤ Until i increases from 1000.
#include <stdio.h> int sushu(int i) { int j; if(i<=1) return 0; if(i==2) return 1; for(j=2;j<i;j++) { if(i%j==0) return 0; else if(i!=j+1) continue; else return 1; } } int main() { int i; for(i=10;i<1000;i++) if(sushu(i)==1) if(i/100==0) { if(i/10==i%10) printf("%5d",i); if(i%5==0) printf("\n"); } else if(i/100==i%10) printf("%5d",i); if(i%5==0) printf("\n"); return 0; }
#include <stdio.h> #include <math.h> float collect(float s,float t,int m,float (*p)(float x)); float fun1(float x); float fun2(float x); float fun3(float x); float fun4(float x); int main() { int n,flag; float a,b,v=0.0; printf("Input the count range(from A to B)and the number of sections.\n"); scanf("%f%f%d",&a,&b,&n); printf("Enter your choice: '1' for fun1,'2' for fun2,'3' for fun3,'4' for fun4==>"); scanf("%d",&flag); if(flag==1) v=collect(a,b,n,fun1); else if(flag==2) v=collect(a,b,n,fun2); else if(flag==3) v=collect(a,b,n,fun3); else v=collect(a,b,n,fun4); printf("v=%f\n",v); return 0; } float collect(float s,float t,int n,float (*p)(float x)) { int i; float f,h,x,y1,y2,area; f=0.0; h=(t-s)/n; x=s; y1=(*p)(x); for(i=1;i<=n;i++) { x=x+h; y2=(*p)(x); area=(y1+y2)*h/2; y1=y2; f=f+area; } return (f); } float fun1(float x) { float fx; fx=x*x-2.0*x+2.0; return(fx); } float fun2(float x) { float fx; fx=x*x*x+3.0*x*x-x+2.0; return(fx); } float fun3 (float x) { float fx; fx=x*sqrt(1+cos(2*x)); return(fx); } float fun4(float x) { float fx; fx=1/(1.0+x*x); return(fx); }
C language Hanoi Tower problem, using C language to realize Hanoi Tower
The tower of Hanoi problem means that there are three needles A, B and C on A board. There are 64 discs of different sizes sleeved on needle A, which are arranged in the order of large ones in the bottom and small ones in the top. To move these 64 discs from needle A to needle C, only one disc can be moved at A time, and needle B can be used in the moving process. But at any time, the disc on any needle must keep the big disc at the bottom and the small disc at the top. Input the number of discs to be moved from the keyboard and give the moving process.
Algorithmic thought
For the tower of Hanoi problem, when only one disk is moved, the disk is directly moved from needle A to needle C. If the moving disc is n (n > 1), it is divided into several steps: move (n-1) discs from needle A to needle B (with the help of needle C); The last disc on needle A moves to needle C; (n-1) discs on needle B move to needle C (with the aid of needle A). Each time you do it, the number of moving discs is one less, decreasing step by step. Finally, when n is 1, the whole moving process is completed.
Therefore, a recursive function can be designed to solve the Hanoi Tower problem, and the whole moving process of the disk can be realized by recursion. The solving process of the problem is the simulation of the actual operation.
#include <stdio.h> int main() { int hanoi(int,char,char,char); int n,counter; printf("Input the number of diskes: "); scanf("%d",&n); printf("\n"); counter=hanoi(n,'A','B','C'); return 0; } int hanoi(int n,char x,char y,char z) { int move(char,int,char); if(n==1) move(x,1,z); else { hanoi(n-1,x,z,y); move(x,n,z); hanoi(n-1,y,x,z); } return 0; } int move(char getone,int n,char putone) { static int k=1; printf("%2d:%3d # %c---%c\n",k,n,getone,putone); if(k++%3==0) printf("\n"); return 0; }
Number of palindromes in C language (detailed version)
Problem description
Print all numbers (also known as palindromes) whose square has symmetric properties that do not exceed n (take n < 256).
problem analysis
For the number n to be determined, after calculating its square (stored in a), compare the highest and lowest, secondary high and secondary low... According to the definition of "palindrome number". If they are equal to each other, they are palindromes. This algorithm needs to know the number of bits of the square number, and then decompose and compare each bit. This method is more applicable to the number with known bits and not too many bits.
This problem can be solved by array. Decompose each bit of (a) after the square, temporarily store it in the array in the order from low to high, and then re combine the elements in the array into a number in the order of subscripts from large to small (e.g. n=15, a=225 and k=522), if k is equal to n × N, then it can be determined that n is the palindrome number.
Algorithm design
Splits an integer from low to high. For an integer (let the variable name be a), regardless of its number of digits, if you want to split the lowest bit, you only need to calculate the modulus of 10 a%10. To split the second lowest bit, you must first find a way to treat the original second lowest bit as the lowest bit. By quoting 10 with the original number, you can get a new number formed by the number other than the lowest bit, and the lowest bit of the new number is the second lowest bit of the original number, According to the method of splitting the lowest bit, a/10 and a% 10 are obtained from the second lowest bit. The algorithm is the same for the number on other bits.
A problem to be solved by using this method is, under what circumstances can we split all the numbers? When only the highest digit of the original number is left (that is, when the new number is a single digit), and then the quotient of 10, the result must be 0. This condition can be used to judge whether the splitting is completed. According to the meaning of the question, the data split each time should be stored in the array, the lowest bit of the original number should be stored in the position with subscript 0, the second lowest bit should be stored in the position with subscript 1... And so on