本文主要是介绍【C/PTA —— 13.指针2(课外实践)】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C/PTA —— 13.指针2(课外实践)
- 一.函数题
- 6-1 鸡兔同笼问题
- 6-2 冒泡排序
- 6-3 字符串反正序连接
- 6-4 计算最长的字符串长度
- 6-5 查找星期
- 二.编程题
- 7-1 C程序设计 实验5-7 数组指针作函数参数
- 7-2 查找奥运五环色的位置
一.函数题
6-1 鸡兔同笼问题
int ChickenRabbit(int* chicken, int* rabbit, int head, int foot)
{int i;int j;if (foot % 2 != 0 && head < 0 && foot < 0){return 0;}for (i = 0; i <= head; i++){j = head - i;if (i * 2 + j * 4 == foot){*chicken = i;*rabbit = j;return 1;}}return 0;
}
6-2 冒泡排序
void bubble(int a[], int n)
{int i, j;for (i = 0; i < n - 1; i++){int flag = 1;for (j = 0; j < n - i - 1; j++){if (a[j] > a[j + 1]){int tmp = a[j];a[j] = a[j + 1];a[j + 1] = tmp;flag = 0;}}if (flag == 1)break;}
}
6-3 字符串反正序连接
#include<string.h>
void fun(char* s, char* t)
{char* str = s;int len = strlen(s);int j = 0;for (int i = len - 1; i >= 0; i--){t[j] = str[i];j++;}t[j] = '\0';strcat(t, s);
}
6-4 计算最长的字符串长度
int max_len(char* s[], int n)
{int max = 0;for (int i = 0; i < n; i++){if (strlen(s[i]) > max)max = strlen(s[i]);}return max;
}
6-5 查找星期
int getindex(char* s)
{if (strcmp(s, "Sunday")==0)return 0;else if (strcmp(s,"Monday")==0)return 1;else if (strcmp(s,"Tuesday")==0)return 2;else if (strcmp(s,"Wednesday")==0)return 3;else if (strcmp(s,"Thursday")==0)return 4;else if (strcmp(s,"Friday")==0)return 5;else if (strcmp(s,"Saturday")==0)return 6;elsereturn -1;
}
二.编程题
7-1 C程序设计 实验5-7 数组指针作函数参数
#include <stdio.h>
#define STUD 30
#define COURSE 5
void Input(int *pScore, int m, int n);
void Total(int *pScore, int sum[], float aver[], int m, int n);
void Print(int *pScore, int sum[], float aver[], int m, int n);
int main()
{ int m, n, score[STUD][COURSE], sum[STUD];float aver[STUD];scanf("%d", &m);scanf("%d", &n);Input(*score, m, n);Total(*score, sum, aver, m, n);Print(*score, sum, aver, m, n);return 0;
}
//函数功能:连续输入m*n个整数存入起始地址为pScore的连续内存单元中
void Input(int *pScore, int m, int n)
{ int i, j;for (i = 0; i < m; i++){ for (j = 0; j < n; j++){ scanf("%d", &pScore[i * n + j]);}}
}
//函数功能:计算起始地址为pScore的连续内存中存储的m*n个数组元素值的总分和平均分
void Total(int *pScore, int sum[], float aver[], int m, int n)
{ int i, j;for (i = 0; i < m; i++){ sum[i] = 0;for (j = 0; j < n; j++){ sum[i] = sum[i] + pScore[i * n + j];}aver[i] = (float) sum[i] / n;}
}
//函数功能:打印总分和平均分
void Print(int *pScore, int sum[], float aver[], int m, int n)
{ int i, j;for (i = 0; i < m; i++){ printf("%d %.1f\n", sum[i], aver[i]);}
}
7-2 查找奥运五环色的位置
#define max 80
#include<string.h>
int getcolor(char* s)
{if (strcmp(s, "red") == 0)return 1;else if (strcmp(s, "blue") == 0)return 2;else if (strcmp(s, "yellow") == 0)return 3;else if (strcmp(s, "green") == 0)return 4;else if (strcmp(s, "black") == 0)return 5;elsereturn -1;
}
int main()
{char str[max];int n;scanf("%s",str);n = getcolor(str);if (n == -1)printf("Not Found\n");elseprintf("%d\n", n);return 0;
}
这篇关于【C/PTA —— 13.指针2(课外实践)】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!