C language-11 basic programming questions (recursion, non-recursion, loop, chain call, Fibonacci number, static keyword)

C language-11 basic programming questions (recursion, non-recursion, loop, chain call, Fibonacci number, static keyword)

1. Use the function implemented above to print prime numbers between 100 and 200. (Use the sqrt function (square root) in math.h)
(The factor of a number always has a square root less than or equal to this number! For example: 16=4 4=2 8=1*16)

#include<stdio.h>
#include<math.h>
void SuShu(){
    
    
	int i, j;
	for (i = 100; i <= 200;++i){
    
    
		for (j = 2; j <= sqrt(i)&&i%j!=0;++j){
    
    }
		if (j>sqrt(i)){
    
    
			printf("%d ",i);
		}
	}
	printf("\n");
}
int main(){
    
    
	SuShu();
	return 0;
}

2. Realize the function to judge whether the year is a lucrative year. (Leap year: can be rounded out by 400 or can be rounded out by 4 but not divisible by 100)

#include<stdio.h>
void Year(int year){
    
    
	if (year%400==0||year%4==0&&year%100!=0){
    
    
		printf("%d是闰年\n",year);
	}
	else{
    
    
		printf("%d不是闰年\n", year);
	}
}
int main(){
    
    
	int year;
	scanf("%d",&year);
	Year(year);
	return 0;
}

3. Implement a function to exchange the contents of two integers. (Key point: the difference between value passing and address passing)

#include<stdio.h>
void Change(int*x,int*y){
    
    
	(*x) = (*x) ^ (*y);
	(*y) = (*x) ^ (*y);
	(*x) = (*x) ^ (*y);
}
int main(){
    
    
	int x, y;
	scanf("%d%d",&x,&y);
	Change(&x,&y);
	printf("x=%d,y=%d\n",x,y);
	return 0;
}

4. Implement a function, print the multiplication formula table, specify the number of rows and columns of the formula table by yourself

#include<stdio.h>
void MUL(int n){
    
    
	for (int i = 1; i <= n;++i){
    
    
		for (int j = i; j <= n;++j){
    
    
			printf("%dx%d=%d ",i,j,i*j);
		}
		printf("\n");
	}
}
int main(){
    
    
	int n;
	scanf("%d",&n);
	MUL(n);
	return 0;
}

5. Recursively realizes printing each digit of an integer (the so-called recursion means that the function calls itself, there is recursion and there is recursion)

#include<stdio.h>
void Print(int n){
    
    
	if (n!=0){
    
    
		printf("%d ",n % 10);
		n /= 10;
		Print(n);
	}
}
int main(){
    
    
	int n;
	scanf("%d",&n);
	Print(n);
	return 0;
}

6. Recursive and non-recursive realization of the factorial of n (without considering the problem of overflow)

#include<stdio.h>
int Fac(int n){
    
    
	if (n == 1){
    
    
		return 1;
	}
	else {
    
    
		return n*Fac(n-1);
	}
}
int main(){
    
    
	int n;
	scanf("%d", &n);
	printf("%d\n",Fac(n));
	return 0;
}

7. Recursive and non-recursive implement strlen separately (carefully understand)
(non-recursive)

#include<stdio.h>
int Strlen(char *p){
    
    
	int len ;
	for (len = 0; *(p + len) != '\0';++len){
    
    }
	return len;
}
int main(){
    
    
	char n[5000];
	scanf("%s", n);
	printf("%d\n",Strlen(n));
	return 0;
}

(Recursive)

#include<stdio.h>
int NoStrlen(char *p){
    
    
	if ((*p) == '\0'){
    
    
		return 0;
	}
	else{
    
    
		return 1 + NoStrlen(p+1);
	}
}
int main(){
    
    
	char n[5000];
	scanf("%s", n);
	printf("%d\n", NoStrlen(n));
	return 0;
}

8. Write a function reverse_string(char * string) (recursive implementation)

#include<stdio.h>
void reverse_string(char * string){
    
    
	if (*string!='\0'){
    
    
		reverse_string(string+1);
		printf("%c",*string);
	}
}
int main(){
    
    
	char n[5000];
	scanf("%s", n);
	reverse_string(n);
	return 0;
}

9. Write a recursive function DigitSum(n), enter a non-negative integer, and return the sum of the numbers that make up it.
For example, if you call DigitSum(1729), it should return 1 + 7 + 2 + 9, and its sum is 19.
Input: 1729, Output: 19

#include<stdio.h>
int DigitSum(int n){
    
    
	if (n == 0){
    
    
		return 0;
	}
	else{
    
    
		return (n % 10) + DigitSum(n/10);
	}
}
int main(){
    
    
	int n;
	scanf("%d", &n);
	printf("%d\n", DigitSum(n));
	return 0;
}

10. Write a function to realize n to the k power, using recursive implementation. (Note that the pow function in math.h is lowercase, and the power function I redefined is Pow. It's not the same, don't make a mistake, Fei is optimistic about the name, hahaha)

#include<stdio.h>
long Pow(int n,int k){
    
    
	static int m = 0;
	if (m == k){
    
    
		return 1;
	}
	else{
    
    
		m++;
		return n*Pow(n,k);
	}
}
int main(){
    
    
	int n,k;
	scanf("%d%d", &n,&k);
	printf("%ld\n", Pow(n,k));
	return 0;
}

11. Recursive and non-recursive realization of the n-th Fibonacci number
(Fibonacci number: the first term, the second term is 1, and the other terms are the sum of the first two items)
Input: 5 Output: 5
Input : 10, output: 55
input: 2, output: 1
{non-recursive}
! ! ! ! ! ! The nth term is equal to the sum of the n-1th term and the n-2th term! ! ! ! ! !

#include<stdio.h>
int FB(int n){
    
    
	int i,num[1000];
	num[0] = num[1] = 1;
	for (i = 2; i < n;++i){
    
    
		num[i] = num[i - 1] + num[i-2];
	}
	return num[i - 1];
}
int main(){
    
    
	int n;
	scanf("%d", &n);
	printf("%d\n", FB(n));
	return 0;
}

(Recursive)
! ! ! ! ! ! The nth term is equal to the sum of the n-1th term and the n-2th term! ! ! ! ! !

#include<stdio.h>
int NFB(int n){
    
    
	if (n == 1 || n == 2){
    
    
		return 1;
	}
	else{
    
    
		return NFB(n - 1) + NFB(n-2);//第n项等于第n-1项和第n-2项的和
	}
}
int main(){
    
    
	int n;
	scanf("%d", &n);
	printf("%d\n", NFB(n));
	return 0;
}

The above are the 11 basic programming questions, and the favorite points of attention are praised by Boou.

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/109510240