【嵙大oj】课堂练习 指针 1878 1879 1880

summary:基本上是在考指针的基本用法——形参传地址

Problem IDTitle
Y1878 Problem A指针1
Y1879 Problem B指针2
Y1880 Problem C指针3

1.1878

Problem A: 指针1

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1288  Solved: 634
[Submit][Status]

Description

这是一个关于指针的题目~ 放心不是很难

要求写三个函数:

max():返回三个参数的最大值

min():返回三个参数的最小值

swap():交换两个参数的值

参数的形式呢,就请你好好看看主函数调用这三个函数时是怎么传入的参数,相信聪明的你肯定分分中A掉这个题呀~

Input

输入为三个整数,空格隔开

Output

输出由主函数控制

Sample Input

3 2 1

Sample Output

The max value is 3 The min value is 1 The original order is 3 2 1 The new order is 1 2 3

HINT

如果你的程序一运行就崩了,或者一输入就崩了,你就先好好的看看指针去吧~

Append Code

append.c,

int main() {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
 
    printf("The max value is %d\n\n", max(a, b, c));
 
    printf("The min value is %d\n\n", min(&a, &b, &c));
 
    printf("The original order is %d %d %d\n\n", a, b, c);
 
    if (a > b)
        swap(&a, &b);
    if (a > c)
        swap(&a, &c);
    if (b > c)
        swap(&b, &c);
 
    printf("The new order is %d %d %d", a, b, c);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
 
int max(int a,int  b,int c) //传值用不到指针
{
    int max=a;
    if(b>max)
        max=b;
    if(c>max)
        max=c;
    return max;
}
int min(int *a,int *b,int *c)//传地址的话形参变量就是指针
{
    int min=*a;
    if(*b<min)
        min=*b;
    if(*c<min)
        min=*c;
    return min;
}
void swap(int *a,int *b)//*a表示值
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
}
int main() {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
 
    printf("The max value is %d\n\n", max(a, b, c));
 
    printf("The min value is %d\n\n", min(&a, &b, &c));
 
    printf("The original order is %d %d %d\n\n", a, b, c);
 
    if (a > b)
        swap(&a, &b);
    if (a > c)
        swap(&a, &c);
    if (b > c)
        swap(&b, &c);
 
    printf("The new order is %d %d %d", a, b, c);
    return 0;
}

2.1879

Problem B: 指针2

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 753  Solved: 246
[Submit][Status]

Description

这是关于指针的第二道题目,难度略有进阶

要求你写三个函数:

1.find_max(int *a, int len) 作用是返回长度为len的数组a中的最大值,参数必须是这个

2.get_data(int *a, int len)作用是往向a中输入len个数据

3.get_length(char *s) 返回字符串s中的字母的个数

大家尽量去理解指针的概念,不要一味的用s[1]这样的方式

Input

输入有三行

第一行一个数字,N,代表要输入的数据的个数

第二行有N个数字,代表输入的数据

第三行为一个字符串

Output

输出由主函数控制

Sample Input

5 1 2 3 4 5 abcde

Sample Output

The max value of this array is 5 The length of the letter in the string is 5

HINT

Append Code

append.c,

int main() {
    int *a;
    a = (int *) malloc(sizeof(int) * 20);
    int len;
    scanf("%d", &len);
    get_data(a, len);
    int max = find_max(a, len);
    printf("The max value of this array is %d\n", max);
    free(a);
 
    char s[25];
    scanf("%s", s);
    int length = get_length(s);
    printf("The length of the letter in the string is %d", length);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
void get_data(int *a, int len)
{
    int i;
    for(i=0;i<len;i++)
    {
        scanf("%d",a+i); 
    }
}
int find_max(int *a, int len)
    {
        int i;
        int max=*a;
        for(i=1;i<len;i++)
        {
            if(*(a+i) > max)
                max=*(a+i);
        }
        return max;
    }
//这个函数较重要
int get_length(char *s)
{
    int cnt=0;
    while(*s!='\0')
    {
        if(isalpha(*s))//用来判断是不是字母
            cnt++;
        s++;
    }
    return cnt;
}
int main() {
    int *a;
    a = (int *) malloc(sizeof(int) * 20);
    int len;
    scanf("%d", &len);
    get_data(a, len);
    int max = find_max(a, len);
    printf("The max value of this array is %d\n", max);
    free(a);
 
    char s[25];
    scanf("%s", s);
    int length = get_length(s);
    printf("The length of the letter in the string is %d", length);
    return 0;
}

3.1880

Problem C: 指针3

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 655  Solved: 317
[Submit][Status]

Description

编写str_len(char *s) 与 str_cpy(char *s1, char *s)函数,

分别实现获取字符串长度 与 拷贝s到s1的功能

禁用string.h头文件

Input

输入由主函数控制,为一行字符串,中间没有空格

Output

输出依然由主函数控制

Sample Input

abide

Sample Output

5 abide

HINT

大体框架是这样的 比如

int str_len(){

      while(){

      }

      return ;

Append Code

append.c,

int main() {
    char s[100];
    scanf("%s", s);
    int len = str_len(s);
    printf("%d\n", len);
    char s1[100];
    str_cpy(s1, s);
    printf("%s\n", s1);
    return 0;
}
#include <stdio.h>
 
// 计算字符串长度的函数
int str_len(char *s) {
    int length = 0;
    while (*s != '\0') { // 遍历字符串直到遇到'\0'
        length++;
        s++;
    }
    return length;
}
 
// 拷贝字符串的函数
void str_cpy(char *s1, char *s) {
    while (*s != '\0') { // 遍历字符串直到遇到'\0'
        *s1 = *s; // 将当前字符拷贝
        s++;
        s1++;
    }
    *s1 = '\0'; // 在目标字符串末尾加上'\0'
}
 
int main() {
    char s[100];
    scanf("%s", s);
    int len = str_len(s);
    printf("%d\n", len);
    char s1[100];
    str_cpy(s1, s);
    printf("%s\n", s1);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//#include <string.h>
#include <ctype.h>



int str_len(char *s)
{
	int cnt=0;
	while(*s!='\0')
	{
		cnt++;
		s++;//指针右移一位 
	}
	return cnt;
}
void str_cpy(char *s1, char *s)//拷贝s到s1 
{
	int i=0;
	int len=str_len(s);
	for(i=0;i<len;i++)//这个操作也行吧 但是没有指针直接右移聪明 
	{
		*(s1+i)=*(s+i);
	 } 
	 *(s1+len)='\0';//一定要记得在目标字符串末尾+0 
}
int main() {
    char s[100];
    scanf("%s", s);
    int len = str_len(s);
    printf("%d\n", len);
    char s1[100];
    str_cpy(s1, s);
    printf("%s\n", s1);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值