summary:基本上是在考指针的基本用法——形参传地址
Problem ID | Title | |
Y | 1878 Problem A | 指针1 |
Y | 1879 Problem B | 指针2 |
Y | 1880 Problem C | 指针3 |
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1288 Solved: 634
[Submit][Status]
这是一个关于指针的题目~ 放心不是很难
要求写三个函数:
max():返回三个参数的最大值
min():返回三个参数的最小值
swap():交换两个参数的值
参数的形式呢,就请你好好看看主函数调用这三个函数时是怎么传入的参数,相信聪明的你肯定分分中A掉这个题呀~
输入为三个整数,空格隔开
输出由主函数控制
3 2 1
The max value is 3 The min value is 1 The original order is 3 2 1 The new order is 1 2 3
如果你的程序一运行就崩了,或者一输入就崩了,你就先好好的看看指针去吧~
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
#include
#include
#include
#include
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 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;
}
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 753 Solved: 246
[Submit][Status]
这是关于指针的第二道题目,难度略有进阶
要求你写三个函数:
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]这样的方式
输入有三行
第一行一个数字,N,代表要输入的数据的个数
第二行有N个数字,代表输入的数据
第三行为一个字符串
输出由主函数控制
5 1 2 3 4 5 abcde
The max value of this array is 5 The length of the letter in the string is 5
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
#include
#include
#include
#include
void get_data(int *a, int len)
{
int i;
for(i=0;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;
}
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 655 Solved: 317
[Submit][Status]
编写str_len(char *s) 与 str_cpy(char *s1, char *s)函数,
分别实现获取字符串长度 与 拷贝s到s1的功能
禁用string.h头文件
输入由主函数控制,为一行字符串,中间没有空格
输出依然由主函数控制
abide
5 abide
大体框架是这样的 比如
int str_len(){
while(){
}
return ;
}
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
// 计算字符串长度的函数
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
#include
#include
//#include
#include
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