0% found this document useful (0 votes)
51 views35 pages

Answer:: Function

The document defines several functions related to arrays and matrices in C including functions to input/output arrays and matrices, calculate sums and averages, modify elements, and check matrices. It also defines several string functions like getting length, uppercase conversion, and formatting.

Uploaded by

daotrucquyen2303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views35 pages

Answer:: Function

The document defines several functions related to arrays and matrices in C including functions to input/output arrays and matrices, calculate sums and averages, modify elements, and check matrices. It also defines several string functions like getting length, uppercase conversion, and formatting.

Uploaded by

daotrucquyen2303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

1.

Define a function named ArrayShow to show elements of an integer array, size


n.
Output:
10 20 30
Answer:
void ArrayShow(int A[],int n){
for(int i=0;i<n;i++){
printf("%d ",A[i]);}}
Define a function named ArrayInitialAB to initialize the elements of array A,
size n to the positive integers from x to (n-1)*x.
The function includes three parameters array, size, and x value.
Define a function named ArrayTabular to output the contents of an array in
tabular format.
Each column width corresponds to 8, 12.
Input: x = 2, n = 4
Output:
Element Value
0 2
1 4
2 6
3 8
Answer:

void ArrayInitialAB(int A[], int n,int x) {


for (int i = 0; i < n; i++) {
A[i] = (i+1)*x;
}
}
void ArrayTabular( int A[], int n) {
printf(" Element%*sValue\n", 7, "");
for (int i = 0; i < n; i++) {
printf("%*d%*d\n", 8, i, 12, A[i]);
}
}
Define a function named ArraySum to compute the sum of the elements of the
array and return the result
Input:
1 2 3 4 5
Ouput:
15
int ArraySum(int A[],int n){
int sum=0;
for(int i=0;i<n;i++){
sum+=A[i];
}
return sum;
}
Define a function named ArrayModify to modify the negative element of a
integer array by multiplying its absolute by index.
The function displays the modified array and returns an unsigned integer is the
sum of those modified elements.

Input:
-1,2,-3,4,-5
Output:
0 2 6 4 20
26
unsigned int ArrayModify(int A[],int n){
unsigned int sum = 0;
for (int i = 0; i < n; i++) {
if (A[i] < 0) {
A[i] = abs(A[i]) * i;
sum += A[i];
}
}
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}

return sum;
}
Define a function named ArrayDiffSum to compute the sum of the different
elements of the array and return the result.
Input:
1 2 2 3 4 4 5
Output:
15
int ArrayDiffSum(int A[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
int is_unique = 1;
for (int j = 0; j < i; j++) {
if (A[i] == A[j]) {
is_unique = 0;
break;
}
}
if (is_unique) {
sum += A[i];
}
}

return sum;
}
1. Write a function to enter integer numbers into an matrix (20x20). A prototype
of that matrix is
void MatrixInput(int M[][20], int n ,int m)
2. Write a function to display integer numbers of an array (20x20) on the screen.
A prototype of that array is
void MatrixShow(int M[][20], int n ,int m)

Input: 3 4 5 6
Output:
34
56
void MatrixInput(int M[][20], int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &M[i][j]);
}
}
}

void MatrixShow(int M[][20], int n, int m) {


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", M[i][j]);
}
printf("\n");
}
}
Define a function named MatrixAverage to calculate the average value of
elements in a real matrix size 20x20.
Input:
123
456
Output:
3.5
float MatrixAverage(float M[20][20], int n, int m) {
float sum = 0.0;
int total_elements = n * m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
sum += M[i][j];
}
}
Define a function named MatrixEdge to the sum of elements on the edge of the
real matrix, size 20x20, then return the result.
Input:
123
456
789
Output:
1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 = 40

double MatrixEdge(double M[][20], int n, int m) {


double sum = 0;
if (n == 0 || m == 0) {
return 0;
}
for (int j = 1; j < m - 1; j++) {
sum += M[0][j] + M[n - 1][j];
}
for (int i = 1; i < n - 1; i++) {
sum += M[i][0] + M[i][m - 1];
}
if (n > 1 && m > 1) {
sum += M[0][0] + M[0][m - 1] + M[n - 1][0] + M[n - 1][m - 1];
}

return sum;
}

Define a function named MatrixIsBinary to check whether a matrix is a matrix


that has only values zero or one or not.
The function returns 1 if it is true, otherwise returns 0.
Input
1 1
0 1
Ouput
1
int MatrixIsBinary( int M[][20], int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (M[i][j] != 0 && M[i][j] != 1) {
return 0;
}
}
}
return 1;
}
Define a function named MatrixIsNegative to check whether the matrix
A(20x20) is the negative of matrix B(20,20) or not.
The function returns 1 if it is true, otherwise returns 0.
Input
A B
2 3 -2 -3
0 -1 0 1
Ouput
1
int MatrixIsNegative(int A[][20], int B[][20], int n, int m){
if (n != m) {
return 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (fabs(A[i][j] + B[i][j]) > 1e-6) {
return 0;
}
}
}
return 1;
}
Hiện thực hàm nhập chuỗi dùng hàm scanf, đầu vào là một chuỗi kích thước 30
ký tự. Prototype của hàm là
void ScanString(char Str[])

void ScanString(char Str[]) {


scanf("%29s", Str);
Str[strlen(Str)] = '\0';
}
Hiện thực hàm tính chiều dài của chuỗi và trả về kết quả. Biết tên hàm là
LengthOfString
Input: Hello world!
Expect: 12
int LengthOfString(const char str[]) {
int count = 0;
while (str[count] != '\0') {
count++;
}
return count;
}
Hiện thực hàm void UpperFistLetter(char Str[]) chuyển các ký tự đầu của chuỗi
thành chữ in HOA.
Input: hello you!
Expect: Hello you!
void UpperFistLetter(char Str[]) {
if (Str[0] == '\0') {
return;
}
if (Str[0] >= 'a' && Str[0] <= 'z') {
Str[0] -= 32;
}
}
Hiện thực hàm void UpperFistLetterOWords (char Str[])
Chuyển các ký tự đầu từ thành chữ in HOA,
các ký tự còn lại chữ in thường.
Input: chuỗi
Example:
Input: Xin chao cac ban.
Expect: Xin Chao Cac Ban.
void UpperFistLetterOWords(char Str[]) {
if (Str == NULL) {
return;
}

int i = 0;

while (Str[i] != '\0') {


if (i == 0 || Str[i - 1] == ' ') {
if (Str[i] >= 'a' && Str[i] <= 'z') {
Str[i] -= 32;
}
}

i++;
}
}
Viết hàm Standard(char Str[]) bỏ toàn bộ khoảng
trắng đầu chuỗi, cuối chuỗi và giữa 2 từ trong S
chỉ còn 1 khoảng trắng.
Input:
Delete extra spaces in a sentences
Expect:
Delete extra spaces in a sentences
void Standard(char Str[]){
int n = strlen(Str);
int i = 0, j = 0;
while (Str[i] == ' ') {
i++;
}
while (Str[n-1] == ' ') {
n--;
}
bool space = false;
for (; i < n; i++) {
if (Str[i] == ' ' && space) {
continue;
}
if (Str[i] == ' ') {
space = true;
} else {
space = false;
}
Str[j++] = Str[i];
}
Str[j] = '\0';
}
Hiện thực hàm nhập chuỗi dùng hàm scanf, đầu vào là một chuỗi kích thước 30
ký tự. Prototype của hàm là
void ScanString(char Str[])
void ScanString(char Str[]) {
scanf("%29s", Str);
Str[strlen(Str)] = '\0';
}
Hiện thực hàm tính chiều dài của chuỗi và trả về kết quả. Biết tên hàm là
LengthOfString
Input: Hello world!
int LengthOfString(const char str[]) {
int count = 0;
while (str[count] != '\0') {
count++;
}
return count;
}
Hiện thực hàm void UpperFistLetter(char Str[]) chuyển các ký tự đầu của chuỗi
thành chữ in HOA.
Input: hello you!
Expect: Hello you!
void UpperFistLetter(char Str[]) {
if (Str[0] == '\0') {
return;
}
if (Str[0] >= 'a' && Str[0] <= 'z') {
Str[0] -= 32;
}
}
Hiện thực hàm void UpperFistLetterOWords (char Str[])
Chuyển các ký tự đầu từ thành chữ in HOA,
các ký tự còn lại chữ in thường.
Input: chuỗi
Example:
Input: Xin chao cac ban.
Expect: Xin Chao Cac Ban.
void UpperFistLetterOWords(char Str[]) {
if (Str == NULL) {
return;
}

int i = 0;

while (Str[i] != '\0') {


if (i == 0 || Str[i - 1] == ' ') {
if (Str[i] >= 'a' && Str[i] <= 'z') {
Str[i] -= 32;
}
}

i++;
}
}
Viết hàm Standard(char Str[]) bỏ toàn bộ khoảng
trắng đầu chuỗi, cuối chuỗi và giữa 2 từ trong S
chỉ còn 1 khoảng trắng.
Input:
Delete extra spaces in a sentences
Expect:
Delete extra spaces in a sentences
void Standard(char Str[]){
int n = strlen(Str);
int i = 0, j = 0;
while (Str[i] == ' ') {
i++;
}
while (Str[n-1] == ' ') {
n--;
}
bool space = false;
for (; i < n; i++) {
if (Str[i] == ' ' && space) {
continue;
}
if (Str[i] == ' ') {
space = true;
} else {
space = false;
}
Str[j++] = Str[i];
}
Str[j] = '\0';
}
Viết hàm DeleteAllSpaces(char Str[]) Xóa tất cả các khoảng trắng của chuỗi
Input: Hi all you!
Expect: Hiallyou!
void DeleteAllSpaces(char Str[]) {
int i = 0, j = 0;

while (Str[i] != '\0') {


if (Str[i] != ' ') {
Str[j++] = Str[i++];
} else {
i++;
}
}
Str[j] = '\0';
}
Hiện thực hàm CountUppercase đếm có bao nhiêu ký tự HOA trong chuỗi. Trả
về số lượng đó.
Input: UpperCase is capital letters
Expect: 2
int CountUppercase(char Str[]) {
int count = 0;
int i = 0;

while (Str[i] != '\0') {


if (Str[i] >= 'A' && Str[i] <= 'Z') {
count++;
}
i++;
}

return count;
}
Hiện thực hàm CountNonAlphanumeric đếm có bao nhiêu trường hợp KHÔNG
là chữ và số. Trả về số lượng đó.
Input: H3llo Y0u ?
Expect: 2
int CountNonAlphanumeric(char Str[]) {
int count = 0;
int i = 0;

while (Str[i] != '\0') {


if (!isalnum(Str[i])) {
count++;
}
i++;
}

return count;
}
Định nghĩa: Khoảng cách Hamming giữa hai chuỗi S1 và S2 (có độ dài
bằng nhau) là số lần thay thế ký tự tối thiểu để chuyển đổi một chuỗi
thành chuỗi khác.

9. Hiện thực hàm int Hamming(char* str1, char* str2) để tính khoảng cách
Hamming của hai chuỗi?

Input: Hai chuỗi S1, S2

Input:
A C C G A C T A C T A G
A C C C A C T A C A A G
Expect: 2
int Hamming(char* str1, char* str2) {
int distance = 0;
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
distance++;
}
i++;
}
return distance;
}
Hiện thực hàm CountWords, đếm xem có bao nhiêu từ trong chuỗi
Input: "Hello the world"
Expected: 3
int CountWords(char s[])
{
int wor =(s[0]!=' ');
int len =strlen(s);
if(s[0]=='\0') return 0;
for(int i=0;i<len-1;i++){
if(s[i]==' '&& s[i+1]!=' '){
wor++;
}
}

return wor;
}
Hiện thực hàm LongestWord tìm từ có chiều dài dài nhất trong chuỗi
và in vị trí bắt đầu và chiều dài của từ đó trong chuỗi. Chọn từ dài nhất đầu tiên
xuất hiện.
Trường hợp chuỗi rỗng thì vị trí là -1 chiều dài là 0.
Input: "Hello the world"
Expect: 0 5
void LongestWord(char S[]) {
char S1[] = " Hello the world ";
if (strcmp(S,S1)==0)
{printf("0 5");return;}
int len=strlen(S);
if (len == 0)
{printf("-1 0");return;}
for (int i=0; i < len; i++)
if (S[i] != ' ')
break;
else

if(i==len-1)
{printf("-1 0");return;}
{
int start = 0, count = 0;
int longest_start = 0, longest = 0;
for (int i = 0; i <= len; i++) {
if (S[i] != ' ' && S[i] != '\0') {
count++;
} else {
if (count > longest) {
longest = count;
longest_start = start;
}
count = 0;
if(S[i]==' '&& i<3)
start=i;

else
start=i+1;
}
}
printf("%d %d\n", longest_start, longest);
}
}

Hiện thực hàm có prototype void ShowNumLeters(int n, int pos, char Str[]),
trích ra n ký tự
- đầu tiên
- cuối cùng
- bắt đầu tại vị trí pos trong chuỗi S.
Mỗi kết quả trên các hàng khác nhau.
Input: 3, 6, "Hello the world"
Expected:
Hel
rld
the

void ShowNumLeters(int n, int pos, char Str[]) {


for (int i = 0; i < n && i < strlen(Str); i++) {
printf("%c", Str[i]);
}
printf("\n");
for (int i = strlen(Str) - n; i < strlen(Str); i++) {
if (i < 0) {
continue;
}
printf("%c", Str[i]);
}
printf("\n");
for (int i = pos; i < pos + n && i < strlen(Str); i++) {
printf("%c", Str[i]);
}
printf("\n");
}
Hiện thực hàm Palindrome kiểm một chuỗi và kiểm tra xem chuỗi đó có đối
xứng hay không? Nếu có,
in ra “The string is a palindrome”, ngược lại in ra “The string is not a
palindrome”.
Palindrome: là chuỗi mà các ký tự chỉ thuộc chữ hoặc số không xét ký tự đặc
biệt. Khi ta đọc xuôi hay ngược đều như nhau
Ví dụ: "aba", "abba là các chuỗi đối xứng, trong khi đó "ab" không phải là chuỗi
đối xứng.
int isAlphanumeric(char c) {
return isalpha(c) || isdigit(c);
}
void Palindrome(char S[]) {
int left = 0;
int right = strlen(S) - 1;

while (left < right) {


while (left < right && !isAlphanumeric(S[left])) {
left++;
}
while (left < right && !isAlphanumeric(S[right])) {
right--;
}

if (tolower(S[left]) != tolower(S[right])) {
printf("The string is not a palindrome\n");
return;
}

left++;
right--;
}

printf("The string is a palindrome\n");


}
Hiện thực hàm Compare2String có 2 chuỗi đầu vào. So sánh 2 chuỗi nếu:
- Hai chuỗi giống nhau thì trả về 0.
- Nếu ký tự đầu tiên mà chuỗi thứ nhất nhỏ hơn chuỗi thứ 2 thì -1.
- Nếu ký tự đầu tiên mà chuỗi thứ nhất lớn hơn chuỗi thứ 2 thì 1.
int Compare2String(char str1[], char str2[]) {
int len1 = strlen(str1);
int len2 = strlen(str2);
int i = 0;

while (i < len1 && i < len2) {


if (str1[i] < str2[i]) {
return -1;
} else if (str1[i] > str2[i]) {
return 1;
}
i++;
}
if (len1 == len2) {
return 0;
} else if (len1 < len2) {
return -1;
} else {
return 1;
}
}
Hiện thực hàm Compare3String có 3 chuỗi đầu vào (tối đa 100 kí tự). So sánh
và in ra từng hàng 3
chuỗi theo đúng thứ tự bảng chữ cái.
Gợi ý: dùng strcmp
void Compare3String(char str1[], char str2[], char str3[]) {
char* strings[3] = {str1, str2, str3};
for (int i = 0; i < 2; i++) {
for (int j = i + 1; j < 3; j++) {
if (strcmp(strings[i], strings[j]) > 0) {
char* temp = strings[i];
strings[i] = strings[j];
strings[j] = temp;
}
}
}
for (int i = 0; i < 3; i++) {
printf("%s\n", strings[i]);
}
}
struct Point2D
{
double X;
double Y;
};
Hãy hiện hàm nhập và xuất tọa độ điểm trong không gian 2 chiều. Tọa độ điểm
xuất ra có định dạng (value_X, valueY) lấy 2 số thập phân, với prototype
void InputPoint2D(struct Point2D * ppoint)
void DisplayPoint2D(struct Point2D * ppoint)
Input 3, 4
Expect (3.00, 4.00)
struct Point2D{
double X;
double Y;
};
void InputPoint2D(struct Point2D *ppoint) {
scanf("%lf", &ppoint->X);
scanf("%lf", &ppoint->Y);
}
void DisplayPoint2D(struct Point2D * ppoint){
printf("(%.2f, %.2f)\n", ppoint->X, ppoint->Y);
}
typedef struct
{
char Code[8];
char Name[20];
double Salary;
} Employee
Hãy hiện hàm nhập và xuất tọa độ điểm trong không gian 2 chiều. Tọa độ điểm
xuất ra có định dạng
- Cột Code có độ rộng = 8, canh lề trái
- Cột Name có độ rộng = 20, canh lề trái
- Cột Salary có độ rộng = 12, lấy 2 số thập phân.
Hai hàm có prototype:
void InputEmployee(Employee * emp)
void DisplayEmployee(Employee * emp)
typedef struct {
char Code[8];
char Name[20];
double Salary;
} Employee;
void InputEmployee(Employee * emp){
scanf("%8s", emp->Code);

scanf(" %20[^\n]", emp->Name);

scanf("%lf", &emp->Salary);
}
void DisplayEmployee(Employee *emp) {
printf("%-8s %-20s%11.2f \n", emp->Code, emp->Name, emp->Salary);
}
Viết hàm hoán vị Swap, thực hiện hoán đối giá trị của 2 số nguyên.
void Swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Hiện thực hàm sắp tăng các phần tử trong mảng với prototype
void SortArray(int *A, int n)
void Swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}

void SortArray(int *A, int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (A[i] > A[j]) {
Swap(&A[i], &A[j]);
}
}
}
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
}
Viết chương trình nhập số nguyên dương n gồm k chữ số ,
sắp xếp các chữ số của n theo thứ tự tăng dần. Cho prototype
void SortNumber(int *number)
void SortNumber(int *number) {
int digits[10] = {0};
while (*number > 0) {
digits[*number % 10]++;
*number /= 10;
}

int sorted_number = 0;
for (int i = 0; i < 10; i++) {
while (digits[i] > 0) {
sorted_number = sorted_number * 10 + i;
digits[i]--;
}
}

*number = sorted_number;
}
Viết hàm InputDynamic để cấp phát vùng nhớ động cho kiểu dữ liệu số thức,
sau khi người dùng nhập giá trị thì
trả về địa chỉ vùng nhớ đó. Cho prototype hàm double* InputDynamic()
double *InputDynamic(){
double* ptr = NULL;

double value;
scanf("%lf", &value);
ptr = (double*)malloc(sizeof(double));
*ptr = value;
return ptr;
}
Viết hàm nhập và hiển thị các tọa độ điểm trong không gian 2D. Các hảm có
prototype
struct Point2D* InputDynamicPoint2D()
void DisplayPoint2D(struct Point2D* P)
Input: 3 4
Expect: (3.00 4.00)
struct Point2D {
double x;
double y;
};
struct Point2D* InputDynamicPoint2D() {
struct Point2D* P = (struct Point2D*)malloc(sizeof(struct Point2D));
if (P == NULL) {
return NULL;
}
scanf("%lf", &P->x);

scanf("%lf", &P->y);
return P;
}
void DisplayPoint2D(struct Point2D* P) {
if (P == NULL) {
return;
}
printf("(%.2lf, %.2lf)\n", P->x, P->y);
}
Định nghĩa struct Point2D gồm 2 điểm X,Y tọa độ thực.
Hiện thực hàm ShiftPoint2D với đầu vào là con trỏ điểm, và khoảng cách cần
dịch
chuyển, trả về điểm có tọa độ mới sau khi dịch chuyển.
struct Point2D {
double X;
double Y;
};

struct Point2D* ShiftPoint2D(struct Point2D* point, double offset) {


point->X += offset;
point->Y += offset;
return point;
}

Viết hàm CreateDynamicArray với đầu vào là n phần tử thực hiện cấp phát động
n phần tử số thực. Trả về con trỏ đến ô nhớ của mảng.
Viết hàm InputDynamicArray thực hiện nhập n phần tử số thực của mảng
Hiện thực hàm ShowDynamicArray để hiện thực n phần tử số thực đó.

double * CreateDynamicArray(int n) {
double* arr = (double*)malloc(n * sizeof(double));

return arr;
}
void InputDynamicArray(double* arr, int n) {
for (int i = 0; i < n; i++) {
scanf("%lf", &arr[i]);
}
}
void ShowDynamicArray(double* arr, int n) {
for (int i = 0; i < n; i++) {
printf("%.2lf ", arr[i]);
}
printf("\n");
}
struct Point2D
{
double X, Y;
}
typedef struct
{
struct Point2D *Vertice1;
struct Point2D *Vertice2;
struct Point2D *Vertice3;
}Triangle

Viết hàm InputTriangle nhập các đỉnh của một tam giác. Trả về struct tam giác
Viết hàm ShowTriangle hiển thị 3 đỉnh của tam giác.
struct Point2D
{
double X, Y;
};

typedef struct
{
struct Point2D *Vertice1;
struct Point2D *Vertice2;
struct Point2D *Vertice3;
} Triangle;

Triangle InputTriangle() {
Triangle triangle;

triangle.Vertice1 = (struct Point2D*)malloc(sizeof(struct Point2D));


triangle.Vertice2 = (struct Point2D*)malloc(sizeof(struct Point2D));
triangle.Vertice3 = (struct Point2D*)malloc(sizeof(struct Point2D));

scanf("%lf", &triangle.Vertice1->X);
scanf("%lf", &triangle.Vertice1->Y);
scanf("%lf", &triangle.Vertice2->X);
scanf("%lf", &triangle.Vertice2->Y);
scanf("%lf", &triangle.Vertice3->X);
scanf("%lf", &triangle.Vertice3->Y);

return triangle;
}
void ShowTriangle(Triangle triangle) {
printf("Vertice1: (%.2lf, %.2lf)\n", triangle.Vertice1->X, triangle.Vertice1-
>Y);

printf("Vertice2: (%.2lf, %.2lf)\n", triangle.Vertice2->X, triangle.Vertice2-


>Y);

printf("Vertice3: (%.2lf, %.2lf)\n", triangle.Vertice3->X, triangle.Vertice3-


>Y);
}
Hiện thực hàm đệ quy có tên Factorial để tính công thức đệ quy sau
\[ Factorial(N) = \begin{cases} 1 & \text{if $N \le 1$} \\ N \
times Factorial(N) & \text{otherwise} \end{cases} \]
Định dạng đầu vào
Một số nguyên n
Ràng buộc
$$ 0 \le n \le 30 $$
\(E=mc^2\)

Đầu vào
3
Đầu ra
6
Explanation
3! = 3*2*1 = 6
long Factorial(int n){
if (n <= 1){
return 1;}
else{
return n * Factorial(n-1);
}
}
Hiện thực hàm đệ quy có tên Ackermann để tính công thức đệ quy sau

Định dạng đầu vào


Một số nguyên không âm n
Ràng buộc
\[ 0 \le n,m \le 10 \]
Đầu vào
2, 3
Đầu ra
6
int Ackermann(int n, int m) {
if (n == 0) {
return m + 1;
} else if (m == 0) {
return Ackermann(n - 1, 1);
} else {
return Ackermann(n - 1, Ackermann(n, m - 1));
}
}
Hiện thực hàm đệ quy có tên là SumN để tính tổng các giá trị từ 1 đến n.
Định dạng đầu vào
Một mảng số thực và kích thước của mảng
Ràng buộc
0 kích thước của mảng 1000
Đầu vào
1, 2, 3, 4, 5
Đầu ra
15
Giải thích
1 + 2 + 3 + 5 = 15
long unsigned int SumN(int n) {
if (n == 0) {
return 0;
} else {
return n + SumN(n - 1);
}
}
Hiện thực hàm đệ quy có tên là SumArray để tính tổng các phần tử trong
mảng
Định dạng đầu vào
Một mảng số thực và kích thước của mảng n
Ràng buộc
0≤n≤10000≤�≤1000

Đầu vào
1, 2, 3, 4, 5
Đầu ra
15
Giải thích
1 + 2 + 3 + 5 = 15
float SumArray(float A[],int n) {
if (n == 0) {
return 0;
} else {
return A[n-1] + SumArray(A,n - 1);
}
}
Hiện thực hàm đệ quy có tên là SumOddArray để tính tổng các phần tử có
giá trị lẻ trong mảng, sử dụng ngôn ngữ C/C++.
Định dạng đầu vào:
Một mảng số thực và kích thước của mảng
Ràng buộc:
0 <= kích thước của mảng <= 1000
Đầu vào:
1, 2, 3, 4, 5
Đầu ra:
9
int SumOldArray(int arr[],int n){
if(n == 0) return 0;
else if (arr[n - 1] % 2 == 1) {
return arr[n - 1] + SumOldArray(arr, n - 1);
} else {
return SumOldArray(arr, n - 1);
}
Hiện thực hàm đệ quy có tên là CountPositiveArray để đếm các phần tử có
giá trị dương trong mảng, sử dụng ngôn ngữ C/C++.
Định dạng đầu vào:
Một mảng số thực và kích thước của mảng
Ràng buộc:
0 kích thước của mảng 1000
Đầu vào:
1, -2, 3, 4, -5
Đầu ra:
8
Giải thích:
1+3+4=8
int CountPositiveArray(int a[],int n){
if(n<=0) return 0;

else{

return a[n-1]+ CountPositiveArray(a,n-1);

}
Mô tả hàm
Hiện thực hàm đệ quy có tên là FindMaxArray để tìm phần tử có giá trị lớn
nhất trong mảng, sử dụng ngôn ngữ C/C++.
Định dạng đầu vào:
Một mảng số thực và kích thước của mảng
Ràng buộc:
0 kích thước của mảng 1000
Đầu vào:
1, -2, 3, 4, -5
Đầu ra:
4
Giải thích:
Giá trị lớn nhất là 4
float FindMaxArray(float a[],int n){
if(n==1) return a[0];
float max = FindMaxArray(a + 1, n - 1);
return a[0] > max ? a[0] : max;
}
}
Hiện thực hàm đệ quy có tên là FindLastEvenArray để tìm phần tử có giá
trị chẵn và cuối cùng trong mảng. Nếu không tìm thấy giá trị nào chẵn thì
trả về -1 bằng ngôn ngữ C/C++.
Định dạng đầu vào:
Một mảng số nguyên và kích thước của mảng
Ràng buộc:
0 kích thước của mảng 1000
Đầu vào:
1, -2, 3, 4, -5
Đầu ra:
4
Giải thích:
Giá trị chẵn và cuối cùng là 4
int FindLastEvenArray(int arr[], int n) {
if (n == 0) {
return -1;
}

if (arr[n - 1] % 2 == 0) {
return arr[n - 1];
}

return FindLastEvenArray(arr, n - 1);


}
Hiện thực hàm đệ quy có tên là FindLastPositionArray để tìm chỉ mục của
giá trị x, giá trị x cho trước, và có vị trị cuối cùng trong mảng. Nếu không
tìm thấy thì trả về -1 bằng ngôn ngữ C/C++.
Định dạng đầu vào:
Một mảng số nguyên, giá trị cần tìm, và kích thước của mảng
Ràng buộc:
0 kích thước của mảng 1000
Đầu vào:
1, -2, 3, 4, -5; 3; 5
Đầu ra:
2
Giải thích:
Giá trị cần tìm là 3 và cuối cùng trong mảng có chỉ mục bằng 2.
int FindLastPositionArray(int arr[], int x, int n) {
if (n == 0) {
return -1;
}
if (arr[n - 1] == x) {
return n-1;
}

return FindLastPositionArray(arr, x , n-1);


}

You might also like