[] Data structure and algorithm study notes - "algorithm notes" -3

function

  • The basic syntax of the function
    returns the type of the function name (parameter type parameter) {
    function body
    }

  • Value is passed: the parameters in parentheses are each called function parameter, the actual parameters of the call is the argument

  • Array as a function argument: When actual call just write the name of the array.
    Modification in the function of the array elements is equivalent to the modification of the original array element (which is different from the ordinary local variables)
    However, while the array can be used as parameters, but appear as the return type is not allowed.
    Practice
    exercises 7-5 string in reverse storage

Write a title describes a function string stored in reverse order. Enter a string in the main function, by calling the function, after the string to the string stored in reverse order, and outputs. Enter
his string. Output of the input string character string stored in reverse order. A separate line.

#include <cstdio>
#include<cstring>

void k(char s1[], char s2[])
{
	int n = strlen(s1);
	for (int i = 0; i < n; i++)
	{
		 s2[i]= s1[n-i-1] ;
	}
}
 
int main()
{
	char str1[100], str2[100] = {};
	scanf("%s", str1);
	k(str1, str2);
	printf("%s",str2);
	return 0;
}

7-7 Copy vowel Problem string

Title Description
write a function which copies a string vowel to another string. Enter a string in the main function, by calling this function, to give a string of a string of the vowel component and outputs.
Enter a string (a line of characters).

The output string string composed of all vowels. End of line wrap.

- [x] has encountered a problem

#include <cstdio>
#include<cstring>

void vowels(char s1[], char s2[])
{
	int n = strlen(s1),j=0;
	for (int i = 0; i < n; i++)
	{
		if ((s1[i] == 'a') || (s1[i] == 'e') || (s1[i] == 'i') || (s1[i] == 'o') || (s1[i] == 'u'))
		{
			s2[j] = s1[i];
			j++;
		}
	}
}
 
int main()
{
	char str1[100], str2[100];//*
	scanf("%s", str1);
	vowels(str1, str2);
	printf("%s", str2);
	return 0;
}

Str2 not initialized in the main function [100] = {}; in the case, the final result will be garbled input (correct conversion string followed by a bunch of "hot")
solution: to initialize str2, but still do not know how to explain the problem.

pointer

  • A pointer is an unsigned integer type
  • Pointer variable used to store pointers, after a data type / * preceded by a pointer variable indicates that this is
	int *p;//C写法
	int* p;//C++写法
	int* p1, p2, p3;//只有p是指针变量
	int *p1, *p2, *p3;//三个都是指针变量
  • Which, int * is a variable type, p is behind the variable name, the address is assigned to p, not * p
  • * & Fetch address-character content identifier
  • Pointer types can be used as the type of function parameters, then regarded as the address of a variable passed to the function, if you make changes to address the elements in this function, the original data will be changed indeed, this is called address delivered.
#include <cstdio>
#include<cstring>

void change(int* p) {
	*p = 233;
}

int main()
{
	int a = 1;
	int* p = &a;
	change(p);
	printf("%d\n",a);
	return 0;
}

As used herein, * p data stored in the address modification, i.e. a change itself, changing the value of a.

  • Using the pointer as an argument, the exchange of two numbers
#include <cstdio>
#include<cstring>

void swap(int* pa,int* pb) {
	int temp;
	temp = *pa;
	*pa = *pb;
	*pb = temp;
}

int main()
{
	int a = 1, b = 2;
	int* pa = &a;
	int* pb = &b;
	swap(pa, pb);
	printf("a=%d,b=%d\n",a,b);
	return 0;
}
  • No reference copy, just to the original variable from an alias
    - [] has encountered a problem: What is the reference?

Exercise
C-10.1

Description Title
input two integers a and b, according to the first big small sequentially outputs a and b. Please pay attention to the way variables were compared using a pointer and output.

Space-separated input two integers a and b.

A and b output by the first big small order, separated by spaces. Note that the output end of the line wrap.

#include <cstdio>
#include<cstring>

void func(int* pa,int* pb) {
	int temp;
	if (*pa <= *pb)
	{
		temp = *pa;
		*pa = *pb;
		*pb = temp;
	}
}

int main()
{
	int a , b ;
	scanf("%d %d", &a, &b);
	int* pa = &a;
	int* pb = &b;
	func(pa, pb);
	printf("%d %d\n",a,b);
	return 0;
}

10.2 C language

Description Title input a, b, C three integers, according to the first big small sequentially outputs a, b and c. Please pay attention to the way variables were compared using a pointer and output.

Input space separated three integers a, b and c.

Output a, b and c according to the first big small order, separated by spaces. Note that the output end of the line wrap.

#include <cstdio>
#include <cstring>

void change(int* p1, int* p2)
{
	int temp;
	temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}

int main()
{
	int a , b,c ;
	scanf("%d %d %d", &a, &b,&c);
	int* pa = &a;
	int* pb = &b;
	int* pc = &c;
	if (*pb < *pc) change(pb, pc);
	if (*pa < *pb) change(pa, pb);
	if (*pb < *pc) change(pb, pc);
	printf("%d %d %d\n",a,b,c);
	return 0;
}

C-10.10

Description String Defines a given topic char * a = "I love China !", Reads integer n,
the output carrying a a = a + n after this assignment corresponding to a character string pointer.

Enter an integer n, to ensure that 0 <= n <13.

Output string were described in the subject after a corresponding assignment. Note that the output end of the line wrap.

#include <cstdio>
#include <cstring>


int main()
{
	char str[14] = "I love China!";
	char* a = str;
	int t;
	scanf("%d", &t);
	for (int i = t; i < 13; i++)
	{
		printf("%c", *(a + i));
	}
	return 0;
}

C-10.15

Description Title string input 3, in ascending order of output.
The method requires the use of a pointer processing.
3 input lines, each line of a string. Ensure that the length of each string does not exceed 20.
Output in order from small to large outputs three strings, each string line.
Note that the output end of the line wrap.

- [ ] Encounter problems

#include "stdafx.h"
#include <cstdio>
#include <cstring>

void change(char* str1,char* str2)
{
	char temp;
	temp = *str1;
	*str1 = *str2;
	*str2 = temp;
}

int main()
{
	char stra[10], strb[10], strc[10];
	scanf("%s\n%s\n%s",stra,strb,strc);
	char *a=stra, *b=strb, *c=strc;
	if (strcmp(a, b) > 0)	change(a, b);
	if (strcmp(b, c) > 0)	change(b, c);
	if (strcmp(a, b) > 0)	change(a, b);
	printf("%s\n%s\n%s", stra, strb, strc);
	return 0;
}

C-10.16

Description Title
input integers 10, wherein the first number and the minimum number of transducers,
the number and the maximum number of the last change. It requires three functions implemented by,
respectively, the 10 digits, the process, the number of output 10.
The method requires the use of a pointer processing.
Input spaces 10 separated by an integer.
10 for output after the description of the operation subject integers, each integer of the output after a space. Note that the output end of the line wrap.

#include <cstdio>
#include <cstring>

void input(int* a)
{
	for (int i = 0; i <= 9; i++)
	{
		scanf("%d", a+i);//这里到底要不要取址
	}

}

void change(int* a)
{
	/**(a + 9)=999;*/
	int *min,*max;
	min =a; max = (a + 9);//这里之前写错了
	for (int i = 0; i <= 9; i++)
	{
		if (*(a+i) <= *min)		min = (a + i);
		if (*(a + i) >= *max)	max = (a + i);
	}
	int temp;
	temp = *a;
	*a = *min;
	*min = temp;
	temp = *(a+9);
	*(a + 9) = *max;
	*max = temp;
}

void output(int* a)
{
	for (int i = 0; i <= 9; i++)
	{
		printf("%d ",*( a + i));//这里到底要不要取址
	}
}

int main()
{
	int a[10];
	int *p = a;
	input(p);
	change(p);
	output(p);
	return 0;
}
Published 43 original articles · won praise 4 · Views 1224

Guess you like

Origin blog.csdn.net/weixin_42176221/article/details/99691333