Problem D: 指针2
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 424 Solved: 122
[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
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;
}
Code
#include<stdio.h>
#include<stdlib.h>
int find_max(int *a, int len)
{
int max = *a;
for(int i=1;i<len;i++)
{
if(max<*(a+i)) max = *(a+i);
}
return max;
}
void get_data(int *a, int len)
{
for(int i=0;i<len;i++)
{
scanf("%d",a+i);
}
}
int get_length(char *s)
{
char *p = s;
int count=0;
for(int i=0;*(p+i)!='\0';i++)
{
if((*(p+i)>='a' &&*(p+i)<='z') || (*(p+i)>='A' &&*(p+i)<='Z')) count++;
}
return count;
}