9 C language basic programming questions-pointers, arrays, graphics printing, daffodil numbers

9 C language basic programming questions-pointers, arrays, graphics printing

1. Write a function to print the contents of the arr array, without using array subscripts, using pointers.
arr is an integer one-dimensional array.

#include<stdio.h>
void Print(int *p,int len){
    
    
	int i = 0;
	while (i != len){
    
    
		printf("%d ",*(p+i));
		i++;
	}
	printf("\n");
}
int main(){
    
    
	int num[] = {
    
    10,20,30,0,8,2,66,88,99};
	int len = sizeof(num) / sizeof(num[0]);
	Print(num,len);
	return 0;
}

2. Write a function that can reverse the content of a string.

#include<stdio.h>
#include<string.h>
void IWord(char *str,int len){
    
    
	int start = 0, end = len - 1;
	while (start<end){
    
    
		char p = *(str + start);
		*(str + start) = *(str + end);
		*(str + end) = p;
		start++;
		end--;
	}
}
int main(){
    
    
	char arr[10000];
	scanf("%s",arr);
	int len = strlen(arr);
	IWord(arr,len);
	printf("%s\n",arr);
	return 0;
}

3. Find the sum of the first 5 items of Sn=a+aa+aaa+aaaa+aaaaa, where a is a number.

#include<stdio.h>
int Sn(int *a){
    
    
	int sum = 0;
	for (int i = 0; i < 5;++i){
    
    
		sum += *(a + i);
	}
	return sum;
}
int main(){
    
    
	int a[] = {
    
    2,22,222,2222,22222};
	printf("%d\n",Sn(a));
	return 0;
}

4. Find all the "number of daffodils" between 0 and 100000 and output them.
"Daffodil number" refers to an n-digit number, and the sum of the n-th power of each number is exactly equal to the number itself, such as: 153 = 1 3 + 5 3 + 3^3, then 153 is a "daffodil number".

#include<stdio.h>
#include<math.h>
void Water(){
    
    
	for (int i = 0; i <= 100000;++i){
    
    
		int num = 0;
		int n = i;
		int sum = 0;
		while (n){
    
    
			n = n / 10;
			num++;
		}
		n = i;
		while (n){
    
    
			sum = sum + (int)pow(n % 10, num);
			n = n / 10;
		}
		if (sum==i){
    
    
			printf("%d ",i);
		}
	}
	printf("\n");
}
int main(){
    
    
	Water();
	return 0;
}

5. Print rhombus
Use C language to output the following patterns on the screen:

#include<stdio.h>
void PrintLX1(int n){
    
    //正三角
	for (int i = 1; i <=n;++i){
    
    
		for (int j = 1; j <= n-i;++j){
    
    
			printf(" ");
		}
		for (int j = 1; j <= i;++j){
    
    
			printf("* ");
		}
		printf("\n");
	}
}
void PrintLX2(int n){
    
    //倒三角
	for (int i = 1; i <= n; ++i){
    
    
		for (int j = 1; j <= i; ++j){
    
    
			printf(" ");
		}
		for (int j = 1; j <= n - i; ++j){
    
    
			printf("* ");
		}
		printf("\n");
	}
}
int main(){
    
    
	PrintLX1(7);
	PrintLX2(7);
	return 0;
}

6. Drink soda, 1 bottle of soda is 1 yuan, 2 empty bottles can be exchanged for a bottle of soda, give 20 yuan, how much soda can be (programmed).
(Personal thinking: each bottle of two empty bottles is equivalent to 1 yuan more, but this dollar can only be changed for water)

#include<stdio.h>
int Drink(int Money){
    
    
	int num = 0;
	for (int i = Money; i >=0;--i){
    
    
		if (num%2==0){
    
    
			i++;//每换一瓶,相当于多一块钱
		}
		num++;
	}
	return num;
}
int main(){
    
    
	printf("%d\n",Drink(20));
	return 0;
}

7. Simulate the realization of library function strlen

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

8. Simulate the realization of the library function strcpy

#include<stdio.h>
void Mystrcpy(char *str1,char *str2){
    
    
	for (int i = 0;;++i){
    
    
		*(str1 + i) = *(str2 + i);
		if (*(str2 + i) == '\0'){
    
    
			break;
		}
	}
}
int main(){
    
    
	char str1[50000];
	char str2[50000];
	scanf("%s", str2);
	Mystrcpy(str1,str2);
	printf("复制之后的str1为%s\n",str1);
	return 0;
}

9. Topic:
Enter an integer array and implement a function
to adjust the order of the numbers in the array so that all odd numbers in the array are in the first half of the
array , and all even numbers are in the second half of the array.

#include<stdio.h>
void Ec(int *num,int len){
    
    
	int nu[10000] = {
    
    0};
	int i, j=0;
	for (i = 0; i < len;++i){
    
    
		if (*(num+i)%2!=0){
    
    
			nu[j] = *(num+i);
			j++;
		}
	}
	for (i = 0; i < len; ++i){
    
    
		if (*(num + i) % 2 == 0){
    
    
			nu[j] = *(num + i);
			j++;
		}
	}
	for (i = 0; i < len; ++i){
    
    
		num[i] = nu[i];
	}
}
int main(){
    
    
	int num[] = {
    
     1, 20, 3, 80, 5, 6, 7, 9, 0 };
	int len = sizeof(num) / sizeof(int);
	Ec(num,len);
	for (int i = 0; i < len;++i){
    
    
		printf("%d ",num[i]);
	}
	printf("\n");
	return 0;
}

Guess you like

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