注意:这篇文章上次更新于1848天前,文章内容可能已经过时。
This article was last updated1848 days ago, the content may be outdated.
函数题
Function Problems
练习5-1 求m到n之和
本题要求实现一个计算m~n(m<n)之间所有整数的和的简单函数。
函数接口定义:
int sum( int m, int n );
其中m和n是用户传入的参数,保证有m<n。函数返回的是m~n之间所有整数的和。
裁判测试程序样例:
Exercise 5-1 Sum from m to n
This problem requires implementing a simple function that calculates the sum of all integers between m and n (m<n).
Function interface definition:
int sum( int m, int n );
where m and n are parameters passed in by the user, and it is guaranteed that m<n. The function returns the sum of all integers between m and n.
Sample judge program:
1 |
|
输入样例:
-5 8
输出样例:
sum = 21
代码:
Input sample:
-5 8
Output sample:
sum = 21
Code:
1 | int sum(int m,int n) |
练习5-2 找两个数中最大者
本题要求对两个整数a和b,输出其中较大的数。
函数接口定义:
int max( int a, int b );
其中a和b是用户传入的参数,函数返回的是两者中较大的数。
裁判测试程序样例:
Exercise 5-2 Find the Larger of Two Numbers
This problem requires outputting the larger of two integers a and b.
Function interface definition:
int max( int a, int b );
where a and b are parameters passed in by the user, and the function returns the larger of the two.
Sample judge program:
1 |
|
/* 你的代码将被嵌在这里 */
输入样例:
-5 8
输出样例:
max = 8
代码:
/* 你的代码将被嵌在这里 */
Input sample:
-5 8
Output sample:
max = 8
Code:
1 | int max(int m,int n) |
练习5-3 数字金字塔
本题要求实现函数输出n行数字金字塔。
函数接口定义:
void pyramid( int n );
其中n是用户传入的参数,为[1, 9]的正整数。要求函数按照如样例所示的格式打印出n行数字金字塔。注意每个数字后面跟一个空格。
裁判测试程序样例:
Exercise 5-3 Digital Pyramid
This problem requires implementing a function that outputs an n-row digital pyramid.
Function interface definition:
void pyramid( int n );
where n is a parameter passed in by the user, a positive integer in [1, 9]. The function is required to print an n-row digital pyramid in the format shown in the sample. Note that each number is followed by a space.
Sample judge program:
1 |
|
输入样例:
5
输出样例:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
代码:
Input sample:
5
Output sample:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Code:
1 | void pyramid(int n) |
习题5-1 符号函数
本题要求实现符号函数sign(x)。
函数接口定义:
int sign( int x );
其中x是用户传入的整型参数。符号函数的定义为:若x大于0,sign(x) = 1;若x等于0,sign(x) = 0;否则,sign(x) = −1。
裁判测试程序样例:
Exercise 5-1 Sign Function
This problem requires implementing the sign function sign(x).
Function interface definition:
int sign( int x );
where x is an integer parameter passed in by the user. The sign function is defined as: if x is greater than 0, sign(x) = 1; if x equals 0, sign(x) = 0; otherwise, sign(x) = −1.
Sample judge program:
1 |
|
输入样例:
10
输出样例:
sign(10) = 1
代码:
Input sample:
10
Output sample:
sign(10) = 1
Code:
1 | int sign(int x) |
习题5-2 使用函数求奇数和
本题要求实现一个函数,计算N个整数中所有奇数的和,同时实现一个判断奇偶性的函数。
函数接口定义:
int even( int n );
int OddSum( int List[], int N );
其中函数even将根据用户传入的参数n的奇偶性返回相应值:当n为偶数时返回1,否则返回0。函数OddSum负责计算并返回传入的N个整数List[]中所有奇数的和。
裁判测试程序样例:
Exercise 5-2 Use a Function to Find the Sum of Odd Numbers
This problem requires implementing a function that calculates the sum of all odd numbers among N integers, and also implementing a function that determines whether a number is even or odd.
Function interface definition:
int even( int n );
int OddSum( int List[], int N );
where the function even returns a value according to the parity of the parameter n passed in by the user: it returns 1 when n is even, otherwise 0. The function OddSum is responsible for calculating and returning the sum of all odd numbers among the N integers in the passed-in List[].
Sample judge program:
#include <stdio.h>
#define MAXN 10
int even( int n );
int OddSum( int List[], int N );
int main()
{
int List[MAXN], N, i;
scanf("%d", &N);
printf("Sum of ( ");
for ( i=0; i<N; i++ ) {
scanf("%d", &List[i]);
if ( even(List[i])==0 )
printf("%d ", List[i]);
}
printf(") = %d\n", OddSum(List, N));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
6
2 -3 7 88 0 15
输出样例:
Sum of ( -3 7 15 ) = 19
代码:
Input sample:
6
2 -3 7 88 0 15
Output sample:
Sum of ( -3 7 15 ) = 19
Code:
1 | int even(int n) |
习题5-3 使用函数计算两点间的距离
本题要求实现一个函数,对给定平面任意两点坐标(x1 ,y1 )和(x2 ,y2),求这两点之间的距离。
函数接口定义:
double dist( double x1, double y1, double x2, double y2 );
其中用户传入的参数为平面上两个点的坐标(x1, y1)和(x2, y2),函数dist应返回两点间的距离。
裁判测试程序样例:
Exercise 5-3 Use a Function to Calculate the Distance between Two Points
This problem requires implementing a function that calculates the distance between two arbitrary points (x1 ,y1 ) and (x2 ,y2) on a given plane.
Function interface definition:
double dist( double x1, double y1, double x2, double y2 );
where the parameters passed in by the user are the coordinates of two points (x1, y1) and (x2, y2) on the plane, and the function dist should return the distance between the two points.
Sample judge program:
#include <stdio.h>
#include <math.h>
double dist( double x1, double y1, double x2, double y2 );
int main()
{
double x1, y1, x2, y2;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
printf("dist = %.2f\n", dist(x1, y1, x2, y2));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
10 10 200 100
输出样例:
dist = 210.24
代码:
Input sample:
10 10 200 100
Output sample:
dist = 210.24
Code:
1 | double dist( double x1, double y1, double x2, double y2 ) |
习题5-4 使用函数求素数和
本题要求实现一个判断素数的简单函数、以及利用该函数计算给定区间内素数和的函数。
素数就是只能被1和自身整除的正整数。注意:1不是素数,2是素数。
函数接口定义:
int prime( int p );
int PrimeSum( int m, int n );
其中函数prime当用户传入参数p为素数时返回1,否则返回0;函数PrimeSum返回区间[m, n]内所有素数的和。题目保证用户传入的参数m≤n。
裁判测试程序样例:
Exercise 5-4 Use a Function to Find the Sum of Prime Numbers
This problem requires implementing a simple function that determines whether a number is prime, and a function that uses it to calculate the sum of prime numbers in a given interval.
A prime number is a positive integer that can only be divided by 1 and itself. Note: 1 is not a prime number, and 2 is a prime number.
Function interface definition:
int prime( int p );
int PrimeSum( int m, int n );
where the function prime returns 1 when the parameter p passed in by the user is a prime number, otherwise 0; the function PrimeSum returns the sum of all prime numbers in the interval [m, n]. The problem guarantees that the parameters passed in by the user satisfy m≤n.
Sample judge program:
#include <stdio.h>
#include <math.h>
int prime( int p );
int PrimeSum( int m, int n );
int main()
{
int m, n, p;
scanf("%d %d", &m, &n);
printf("Sum of ( ");
for( p=m; p<=n; p++ ) {
if( prime(p) != 0 )
printf("%d ", p);
}
printf(") = %d\n", PrimeSum(m, n));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
-1 10
输出样例:
Sum of ( 2 3 5 7 ) = 17
代码:
Input sample:
-1 10
Output sample:
Sum of ( 2 3 5 7 ) = 17
Code:
1 | int prime(int n) |
习题5-5 使用函数统计指定数字的个数
本题要求实现一个统计整数中指定数字的个数的简单函数。
函数接口定义:
int CountDigit( int number, int digit );
其中number是不超过长整型的整数,digit为[0, 9]区间内的整数。函数CountDigit应返回number中digit出现的次数。
裁判测试程序样例:
Exercise 5-5 Use a Function to Count the Occurrences of a Specified Digit
This problem requires implementing a simple function that counts the occurrences of a specified digit in an integer.
Function interface definition:
int CountDigit( int number, int digit );
where number is an integer not exceeding long int, and digit is an integer in the interval [0, 9]. The function CountDigit should return the number of occurrences of digit in number.
Sample judge program:
#include <stdio.h>
int CountDigit( int number, int digit );
int main()
{
int number, digit;
scanf("%d %d", &number, &digit);
printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
-21252 2
输出样例:
Number of digit 2 in -21252: 3
代码:
Input sample:
-21252 2
Output sample:
Number of digit 2 in -21252: 3
Code:
1 | int CountDigit( int number, int digit ) |
习题5-6 使用函数输出水仙花数
水仙花数是指一个N位正整数(N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:153=13+53+33。 本题要求编写两个函数,一个判断给定整数是否水仙花数,另一个按从小到大的顺序打印出给定区间(m,n)内所有的水仙花数。
函数接口定义:
int narcissistic( int number );
void PrintN( int m, int n );
函数narcissistic判断number是否为水仙花数,是则返回1,否则返回0。
函数PrintN则打印开区间(m, n)内所有的水仙花数,每个数字占一行。题目保证100≤m≤n≤10000。
裁判测试程序样例:
Exercise 5-6 Use a Function to Output Narcissistic Numbers
A narcissistic number is an N-digit positive integer (N≥3) whose sum of the N-th powers of its digits equals itself. For example: 153=13+53+33. This problem requires writing two functions: one determines whether a given integer is a narcissistic number, and the other prints all narcissistic numbers in the given interval (m, n) in ascending order.
Function interface definition:
int narcissistic( int number );
void PrintN( int m, int n );
The function narcissistic determines whether number is a narcissistic number: it returns 1 if so, otherwise 0.
The function PrintN prints all narcissistic numbers in the open interval (m, n), with each number on its own line. The problem guarantees that 100≤m≤n≤10000.
Sample judge program:
#include <stdio.h>
int narcissistic( int number );
void PrintN( int m, int n );
int main()
{
int m, n;
scanf("%d %d", &m, &n);
if ( narcissistic(m) ) printf("%d is a narcissistic number\n", m);
PrintN(m, n);
if ( narcissistic(n) ) printf("%d is a narcissistic number\n", n);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
153 400
输出样例:
153 is a narcissistic number
370
371
代码:
Input sample:
153 400
Output sample:
153 is a narcissistic number
370
371
Code:
1 | int narcissistic( int number ) |
习题5-7 使用函数求余弦函数的近似值
本题要求实现一个函数,用下列公式求cos(x)的近似值,精确到最后一项的绝对值小于e:cos(x)=x0 /0!−x2 /2!+x4 /4!−x6 /6!+⋯
函数接口定义:
double funcos( double e, double x );
其中用户传入的参数为误差上限e和自变量x;函数funcos应返回用给定公式计算出来、并且满足误差要求的cos(x)的近似值。输入输出均在双精度范围内。
裁判测试程序样例:
Exercise 5-7 Use a Function to Find the Approximate Value of the Cosine Function
This problem requires implementing a function that uses the following formula to find the approximate value of cos(x), accurate until the absolute value of the last term is less than e: cos(x)=x0 /0!−x2 /2!+x4 /4!−x6 /6!+⋯
Function interface definition:
double funcos( double e, double x );
where the parameters passed in by the user are the error limit e and the independent variable x; the function funcos should return the approximate value of cos(x) calculated by the given formula and satisfying the error requirement. Both input and output are within the double-precision range.
Sample judge program:
#include <stdio.h>
#include <math.h>
double funcos( double e, double x );
int main()
{
double e, x;
scanf("%lf %lf", &e, &x);
printf("cos(%.2f) = %.6f\n", x, funcos(e, x));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
0.01 -3.14
输出样例:
cos(-3.14) = -0.999899
代码:
Input sample:
0.01 -3.14
Output sample:
cos(-3.14) = -0.999899
Code:
1 | double funcos( double e, double x ) |
习题6-1 分类统计字符个数
本题要求实现一个函数,统计给定字符串中英文字母、空格或回车、数字字符和其他字符的个数。
函数接口定义:
void StringCount( char s[] );
其中 char s[] 是用户传入的字符串。函数StringCount须在一行内按照
letter = 英文字母个数, blank = 空格或回车个数, digit = 数字字符个数, other = 其他字符个数
的格式输出。
裁判测试程序样例:
Exercise 6-1 Count Character Types by Category
This problem requires implementing a function that counts the number of English letters, spaces or carriage returns, digit characters and other characters in a given string.
Function interface definition:
void StringCount( char s[] );
where char s[] is the string passed in by the user. The function StringCount must output in one line in the format
letter = number of English letters, blank = number of spaces or carriage returns, digit = number of digit characters, other = number of other characters
Sample judge program:
#include <stdio.h>
#define MAXS 15
void StringCount( char s[] );
void ReadString( char s[] ); /* 由裁判实现,略去不表 */
int main()
{
char s[MAXS];
ReadString(s);
StringCount(s);
return 0;
}
/* Your function will be put here */
输入样例:
aZ &
09 Az
输出样例:
letter = 4, blank = 3, digit = 2, other = 1
代码:
Input sample:
aZ &
09 Az
Output sample:
letter = 4, blank = 3, digit = 2, other = 1
Code:
1 | void StringCount( char s[] ) |
习题6-2 使用函数求特殊a串数列和
给定两个均不超过9的正整数a和n,要求编写函数求a+aa+aaa++⋯+aa⋯a(n个a)之和。
函数接口定义:
int fn( int a, int n );
int SumA( int a, int n );
其中函数fn须返回的是n个a组成的数字;SumA返回要求的和。
裁判测试程序样例:
Exercise 6-2 Use a Function to Find the Sum of a Special Series of a’s
Given two positive integers a and n, both not exceeding 9, write a function to find the sum of a+aa+aaa++⋯+aa⋯a (n a’s).
Function interface definition:
int fn( int a, int n );
int SumA( int a, int n );
where the function fn must return the number composed of n a’s; SumA returns the required sum.
Sample judge program:
1 |
|
输入样例:
2 3
输出样例:
fn(2, 3) = 222
s = 246
代码:
Input sample:
2 3
Output sample:
fn(2, 3) = 222
s = 246
Code:
1 | int fn(int a,int n) |
习题6-3 使用函数输出指定范围内的完数
本题要求实现一个计算整数因子和的简单函数,并利用其实现另一个函数,输出两正整数m和n(0<m≤n≤10000)之间的所有完数。所谓完数就是该数恰好等于除自身外的因子之和。例如:6=1+2+3,其中1、2、3为6的因子。
函数接口定义:
int factorsum( int number );
void PrintPN( int m, int n );
其中函数factorsum须返回int number的因子和;函数PrintPN要逐行输出给定范围[m, n]内每个完数的因子累加形式的分解式,每个完数占一行,格式为“完数 = 因子1 + 因子2 + … + 因子k”,其中完数和因子均按递增顺序给出。如果给定区间内没有完数,则输出一行“No perfect number”。
裁判测试程序样例:
Exercise 6-3 Use a Function to Output Perfect Numbers in a Specified Range
This problem requires implementing a simple function that calculates the sum of the factors of an integer, and using it to implement another function that outputs all perfect numbers between two positive integers m and n (0<m≤n≤10000). A perfect number is a number that equals exactly the sum of its factors excluding itself. For example: 6=1+2+3, where 1, 2 and 3 are the factors of 6.
Function interface definition:
int factorsum( int number );
void PrintPN( int m, int n );
where the function factorsum must return the sum of the factors of int number; the function PrintPN outputs the factorization of each perfect number in the given range [m, n] in the form of a sum of factors, one perfect number per line, in the format “perfect number = factor 1 + factor 2 + … + factor k”, where both the perfect numbers and the factors are given in increasing order. If there are no perfect numbers in the given interval, output a line “No perfect number”.
Sample judge program:
1 |
|
输入样例1:
6 30
输出样例1:
6 is a perfect number
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
输入样例2:
7 25
输出样例2:
No perfect number
代码:
Input sample 1:
6 30
Output sample 1:
6 is a perfect number
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
Input sample 2:
7 25
Output sample 2:
No perfect number
Code:
1 | int factorsum( int number ) |
习题6-4 使用函数输出指定范围内的Fibonacci数
本题要求实现一个计算Fibonacci数的简单函数,并利用其实现另一个函数,输出两正整数m和n(0<m≤n≤10000)之间的所有Fibonacci数。所谓Fibonacci数列就是满足任一项数字是前两项的和(最开始两项均定义为1)的数列。
函数接口定义:
int fib( int n );
void PrintFN( int m, int n );
其中函数fib须返回第n项Fibonacci数;函数PrintFN要在一行中输出给定范围[m, n]内的所有Fibonacci数,相邻数字间有一个空格,行末不得有多余空格。如果给定区间内没有Fibonacci数,则输出一行“No Fibonacci number”。
裁判测试程序样例:
Exercise 6-4 Use a Function to Output Fibonacci Numbers in a Specified Range
This problem requires implementing a simple function that calculates Fibonacci numbers, and using it to implement another function that outputs all Fibonacci numbers between two positive integers m and n (0<m≤n≤10000). A Fibonacci sequence is a sequence in which any term is the sum of the previous two terms (the first two terms are both defined as 1).
Function interface definition:
int fib( int n );
void PrintFN( int m, int n );
where the function fib must return the n-th Fibonacci number; the function PrintFN outputs all Fibonacci numbers in the given range [m, n] in one line, with a space between adjacent numbers and no extra space at the end of the line. If there are no Fibonacci numbers in the given interval, output a line “No Fibonacci number”.
Sample judge program:
#include <stdio.h>
int fib( int n );
void PrintFN( int m, int n );
int main()
{
int m, n, t;
scanf("%d %d %d", &m, &n, &t);
printf("fib(%d) = %d\n", t, fib(t));
PrintFN(m, n);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例1:
20 100 7
输出样例1:
fib(7) = 13
21 34 55 89
输入样例2:
2000 2500 8
输出样例2:
fib(8) = 21
No Fibonacci number
代码:
Input sample 1:
20 100 7
Output sample 1:
fib(7) = 13
21 34 55 89
Input sample 2:
2000 2500 8
Output sample 2:
fib(8) = 21
No Fibonacci number
Code:
1 | int fib( int n ) |
习题6-5 使用函数验证哥德巴赫猜想
本题要求实现一个判断素数的简单函数,并利用该函数验证哥德巴赫猜想:任何一个不小于6的偶数均可表示为两个奇素数之和。素数就是只能被1和自身整除的正整数。注意:1不是素数,2是素数。
函数接口定义:
int prime( int p );
void Goldbach( int n );
其中函数prime当用户传入参数p为素数时返回1,否则返回0;函数Goldbach按照格式“n=p+q”输出n的素数分解,其中p≤q均为素数。又因为这样的分解不唯一(例如24可以分解为5+19,还可以分解为7+17),要求必须输出所有解中p最小的解。
裁判测试程序样例:
Exercise 6-5 Use a Function to Verify Goldbach’s Conjecture
This problem requires implementing a simple function that determines whether a number is prime, and using it to verify Goldbach’s conjecture: any even number not less than 6 can be expressed as the sum of two odd prime numbers. A prime number is a positive integer that can only be divided by 1 and itself. Note: 1 is not a prime number, and 2 is a prime number.
Function interface definition:
int prime( int p );
void Goldbach( int n );
where the function prime returns 1 when the parameter p passed in by the user is a prime number, otherwise 0; the function Goldbach outputs the prime decomposition of n in the format “n=p+q”, where p≤q and both are prime numbers. Since such decomposition is not unique (for example, 24 can be decomposed as 5+19, or as 7+17), it is required to output the solution with the smallest p among all solutions.
Sample judge program:
#include <stdio.h>
#include <math.h>
int prime( int p ); void Goldbach( int n );
int main() {
int m, n, i, cnt;
scanf("%d %d", &m, &n);
if ( prime(m) != 0 ) printf("%d is a prime number\n", m);
if ( m < 6 ) m = 6;
if ( m%2 ) m++;
cnt = 0;
for( i=m; i<=n; i+=2 ) {
Goldbach(i);
cnt++;
if ( cnt%5 ) printf(", ");
else printf("\n");
}
return 0; }
/* 你的代码将被嵌在这里 */
输入样例:
89 100
输出样例:
89 is a prime number
90=7+83, 92=3+89, 94=5+89, 96=7+89, 98=19+79
100=3+97,
代码:
Input sample:
89 100
Output sample:
89 is a prime number
90=7+83, 92=3+89, 94=5+89, 96=7+89, 98=19+79
100=3+97,
Code:
1 | int prime( int p ) |
习题6-6 使用函数输出一个整数的逆序数
本题要求实现一个求整数的逆序数的简单函数。
函数接口定义:
int reverse( int number );
其中函数reverse须返回用户传入的整型number的逆序数。
裁判测试程序样例:
Exercise 6-6 Use a Function to Output the Reversed Number of an Integer
This problem requires implementing a simple function that finds the reversed number of an integer.
Function interface definition:
int reverse( int number );
where the function reverse must return the reversed number of the integer number passed in by the user.
Sample judge program:
#include <stdio.h>
int reverse( int number );
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", reverse(n));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
-12340
输出样例:
-4321
代码:
Input sample:
-12340
Output sample:
-4321
Code:
1 | int reverse( int number ) |
练习8-2 计算两数的和与差
本题要求实现一个计算输入的两数的和与差的简单函数。
函数接口定义:
void sum_diff( float op1, float op2, float *psum, float *pdiff );
其中op1和op2是输入的两个实数,psum和pdiff是计算得出的和与差。
裁判测试程序样例:
Exercise 8-2 Calculate the Sum and Difference of Two Numbers
This problem requires implementing a simple function that calculates the sum and difference of two input numbers.
Function interface definition:
void sum_diff( float op1, float op2, float *psum, float *pdiff );
where op1 and op2 are the two input real numbers, and *psum and *pdiff are the calculated sum and difference.
Sample judge program:
#include <stdio.h>
void sum_diff( float op1, float op2, float *psum, float *pdiff );
int main()
{
float a, b, sum, diff;
scanf("%f %f", &a, &b);
sum_diff(a, b, &sum, &diff);
printf("The sum is %.2f\nThe diff is %.2f\n", sum, diff);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
4 6
输出样例:
The sum is 10.00
The diff is -2.00
参考代码:
Input sample:
4 6
Output sample:
The sum is 10.00
The diff is -2.00
Reference code:
1 | void sum_diff( float op1, float op2, float *psum, float *pdiff ){ |
练习8-8 移动字母
本题要求编写函数,将输入字符串的前3个字符移到最后。
函数接口定义:
void Shift( char s[] );
其中char s[]是用户传入的字符串,题目保证其长度不小于3;函数Shift须将按照要求变换后的字符串仍然存在s[]里。
裁判测试程序样例:
Exercise 8-8 Move Letters
This problem requires writing a function that moves the first 3 characters of an input string to the end.
Function interface definition:
void Shift( char s[] );
where char s[] is the string passed in by the user, and the problem guarantees its length is not less than 3; the function Shift must store the transformed string in s[] as required.
Sample judge program:
#include <stdio.h>
#include <string.h>
#define MAXS 10
void Shift( char s[] );
void GetString( char s[] ); /* 实现细节在此不表 */
int main()
{
char s[MAXS];
GetString(s);
Shift(s);
printf("%s\n", s);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
abcdef
输出样例:
defabc
代码:
Input sample:
abcdef
Output sample:
defabc
Code:
1 | void Shift( char s[] ){ |
习题8-1 拆分实数的整数与小数部分
本题要求实现一个拆分实数的整数与小数部分的简单函数。
函数接口定义:
void splitfloat( float x, int *intpart, float *fracpart );
其中x是被拆分的实数(0≤x<10000),intpart和fracpart分别是将实数x拆分出来的整数部分与小数部分。
裁判测试程序样例:
Exercise 8-1 Split the Integer and Fractional Parts of a Real Number
This problem requires implementing a simple function that splits the integer and fractional parts of a real number.
Function interface definition:
void splitfloat( float x, int *intpart, float *fracpart );
where x is the real number to be split (0≤x<10000), and *intpart and *fracpart are respectively the integer part and fractional part split from the real number x.
Sample judge program:
#include <stdio.h>
void splitfloat( float x, int *intpart, float *fracpart );
int main()
{
float x, fracpart;
int intpart;
scanf("%f", &x);
splitfloat(x, &intpart, &fracpart);
printf("The integer part is %d\n", intpart);
printf("The fractional part is %g\n", fracpart);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
2.718
输出样例:
The integer part is 2
The fractional part is 0.718
代码:
Input sample:
2.718
Output sample:
The integer part is 2
The fractional part is 0.718
Code:
1 | void splitfloat( float x, int *intpart, float *fracpart ){ |
习题8-2 在数组中查找指定元素
本题要求实现一个在数组中查找指定元素的简单函数。
函数接口定义:
int search( int list[], int n, int x );
其中list[]是用户传入的数组;n(≥0)是list[]中元素的个数;x是待查找的元素。如果找到
则函数search返回相应元素的最小下标(下标从0开始),否则返回−1。
裁判测试程序样例:
Exercise 8-2 Find a Specified Element in an Array
This problem requires implementing a simple function that finds a specified element in an array.
Function interface definition:
int search( int list[], int n, int x );
where list[] is the array passed in by the user; n (≥0) is the number of elements in list[]; x is the element to be found. If found,
the function search returns the smallest index of the corresponding element (indices start from 0), otherwise it returns −1.
Sample judge program:
#include <stdio.h>
#define MAXN 10
int search( int list[], int n, int x );
int main()
{
int i, index, n, x;
int a[MAXN];
scanf("%d", &n);
for( i = 0; i < n; i++ )
scanf("%d", &a[i]);
scanf("%d", &x);
index = search( a, n, x );
if( index != -1 )
printf("index = %d\n", index);
else
printf("Not found\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例1:
5
1 2 2 5 4
2
输出样例1:
index = 1
输入样例2:
5
1 2 2 5 4
0
输出样例2:
Not found
代码:
Input sample 1:
5
1 2 2 5 4
2
Output sample 1:
index = 1
Input sample 2:
5
1 2 2 5 4
0
Output sample 2:
Not found
Code:
1 | int search( int list[], int n, int x ){ |
习题8-3 数组循环右移
本题要求实现一个对数组进行循环右移的简单函数:一个数组a中存有n(>0)个整数,将每个整数循环向右移m(≥0)个位置,即将a中的数据由(a0
a1⋯an−1)变换为( an−m⋯an−1a0a1⋯an−m−1 )(最后m个数循环移最前面的m个位置)。
函数接口定义:
int ArrayShift( int a[], int n, int m );
其中 a[] 是用户传入的数组;n是数组的大小;m是右移的位数。函数 ArrayShift 须将循环右移后的数组仍然存在a[]中。
裁判测试程序样例:
Exercise 8-3 Cyclic Right Shift of an Array
This problem requires implementing a simple function that performs a cyclic right shift on an array: an array a contains n (>0) integers, and each integer is cyclically shifted right by m (≥0) positions, that is, the data in a is transformed from (a0
a1⋯an−1) to ( an−m⋯an−1a0a1⋯an−m−1 ) (the last m numbers are moved cyclically to the first m positions).
Function interface definition:
int ArrayShift( int a[], int n, int m );
where a[] is the array passed in by the user; n is the size of the array; m is the number of positions to shift right. The function ArrayShift must store the cyclically shifted array in a[].
Sample judge program:
1 |
|
输入样例:
6 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4
代码:
Input sample:
6 2
1 2 3 4 5 6
Output sample:
5 6 1 2 3 4
Code:
1 | int ArrayShift( int a[], int n, int m ){ |
习题 8-4 报数
报数游戏是这样的:有n个人围成一圈,按顺序从1到n编好号。从第一个人开始报数,报到m(<n)的人退出圈子;下一个人从1开始报数,报到m的人退出圈子。如此下去,直到留下最后一个人。
本题要求编写函数,给出每个人的退出顺序编号。
函数接口定义:
void CountOff( int n, int m, int out[] );
其中n是初始人数;m是游戏规定的退出位次(保证为小于n的正整数)。函数CountOff将每个人的退出顺序编号存在数组out[]中。因为C语言数组下标是从0开始的,所以第i个位置上的人是第out[i-1]个退出的。
裁判测试程序样例:
Exercise 8-4 Counting-off Game
The counting-off game works like this: there are n people sitting in a circle, numbered from 1 to n in order. Starting from the first person, people count off; the person who counts to m (<n) leaves the circle; the next person starts counting from 1 again, and the person who counts to m leaves the circle. This continues until only one person remains.
This problem requires writing a function that gives the exit order number of each person.
Function interface definition:
void CountOff( int n, int m, int out[] );
where n is the initial number of people; m is the exit position specified by the game (guaranteed to be a positive integer less than n). The function CountOff stores the exit order number of each person in the array out[]. Because C array indices start from 0, the person at position i is the out[i-1]-th to exit.
Sample judge program:
1 |
|
输入样例:
11 3
输出样例:
4 10 1 7 5 2 11 9 3 6 8
代码:
Input sample:
11 3
Output sample:
4 10 1 7 5 2 11 9 3 6 8
Code:
1 | void CountOff(int n, int m, int out[]) |
习题8-5 使用函数实现字符串部分复制
本题要求编写函数,将输入字符串t中从第m个字符开始的全部字符复制到字符串s中。
函数接口定义:
void strmcpy( char *t, int m, char *s );
函数strmcpy将输入字符串char *t中从第m个字符开始的全部字符复制到字符串char *s中。若m超过输入字符串的长度,则结果字符串应为空串。
裁判测试程序样例:
Exercise 8-5 Use a Function to Partially Copy a String
This problem requires writing a function that copies all characters starting from the m-th character of the input string t to the string s.
Function interface definition:
void strmcpy( char *t, int m, char *s );
The function strmcpy copies all characters starting from the m-th character of the input string char *t to the string char *s. If m exceeds the length of the input string, the resulting string should be an empty string.
Sample judge program:
1 |
|
输入样例:
7
happy new year
输出样例:
new year
代码:
Input sample:
7
happy new year
Output sample:
new year
Code:
1 | void strmcpy( char *t, int m, char *s ){ |
习题8-6 删除字符
本题要求实现一个删除字符串中的指定字符的简单函数。
函数接口定义:
void delchar( char *str, char c );
其中char *str是传入的字符串,c是待删除的字符。函数delchar的功能是将字符串str中出现的所有c字符删除。
裁判测试程序样例:
Exercise 8-6 Delete a Character
This problem requires implementing a simple function that deletes a specified character from a string.
Function interface definition:
void delchar( char *str, char c );
where char *str is the string passed in, and c is the character to be deleted. The function delchar deletes all occurrences of character c in the string str.
Sample judge program:
1 |
|
输入样例:
a
happy new year
输出样例:
hppy new yer
代码:
Input sample:
a
happy new year
Output sample:
hppy new yer
Code:
1 | void delchar( char *str, char c ){ |
习题8-8 判断回文字符串
本题要求编写函数,判断给定的一串字符是否为“回文”。所谓“回文”是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是回文。
函数接口定义:
bool palindrome( char *s );
函数palindrome判断输入字符串char *s是否为回文。若是则返回 true ,否则返回 false 。
裁判测试程序样例:
Exercise 8-8 Determine Whether a String Is a Palindrome
This problem requires writing a function that determines whether a given string is a “palindrome”. A “palindrome” is a string that reads the same forward and backward. For example, “XYZYX” and “xyzzyx” are both palindromes.
Function interface definition:
bool palindrome( char *s );
The function palindrome determines whether the input string char *s is a palindrome. If so, it returns true , otherwise it returns false .
Sample judge program:
1 |
|
输入样例1:
thisistrueurtsisiht
输出样例1:
Yes
thisistrueurtsisiht
输入样例2:
thisisnottrue
输出样例2:
No
thisisnottrue
代码:
Input sample 1:
thisistrueurtsisiht
Output sample 1:
Yes
thisistrueurtsisiht
Input sample 2:
thisisnottrue
Output sample 2:
No
thisisnottrue
Code:
1 | bool palindrome( char *s ){ |
习题8-9 分类统计各类字符个数
本题要求实现一个函数,统计给定字符串中的大写字母、小写字母、空格、数字以及其它字符各有多少。
函数接口定义:
void StringCount( char *s );
其中 char *s 是用户传入的字符串。函数 StringCount 须在一行内按照
大写字母个数 小写字母个数 空格个数 数字个数 其它字符个数
的格式输出。
裁判测试程序样例:
Exercise 8-9 Count Characters of Various Types
This problem requires implementing a function that counts how many uppercase letters, lowercase letters, spaces, digits and other characters there are in a given string.
Function interface definition:
void StringCount( char *s );
where char *s is the string passed in by the user. The function StringCount must output in one line in the format
number of uppercase letters number of lowercase letters number of spaces number of digits number of other characters
Sample judge program:
1 |
|
输入样例:
aZ&*?
093 Az
输出样例:
2 2 1 3 4
代码:
Input sample:
aZ&*?
093 Az
Output sample:
2 2 1 3 4
Code:
1 | void StringCount( char *s ){ |
习题9-2 计算两个复数之积
本题要求实现一个计算复数之积的简单函数。
函数接口定义:
struct complex multiply(struct complex x, struct complex y);
其中 struct complex 是复数结构体,其定义如下:
struct complex{
int real;
int imag;
};
裁判测试程序样例:
Exercise 9-2 Calculate the Product of Two Complex Numbers
This problem requires implementing a simple function that calculates the product of two complex numbers.
Function interface definition:
struct complex multiply(struct complex x, struct complex y);
where struct complex is the complex number structure, defined as follows:
struct complex{
int real;
int imag;
};
Sample judge program:
1 |
|
输入样例:
3 4 5 6
输出样例:
(3+4i) * (5+6i) = -9 + 38i
代码:
Input sample:
3 4 5 6
Output sample:
(3+4i) * (5+6i) = -9 + 38i
Code:
1 | struct complex multiply(struct complex x, struct complex y){ |
习题9-6 按等级统计学生成绩
本题要求实现一个根据学生成绩设置其等级,并统计不及格人数的简单函数。
函数接口定义:
int set_grade( struct student *p, int n );
其中 p 是指向学生信息的结构体数组的指针,该结构体的定义为:
struct student{
int num;
char name[20];
int score;
char grade;
};
n 是数组元素个数。学号 num 、姓名 name 和成绩 score 均是已经存储好的。 set_grade 函数需要根据学生的成绩 score 设置其等级 grade 。等级设置:85-100为A,70-84为B,60-69为C,0-59为D。同时, set_grade 还需要返回不及格的人数。
裁判测试程序样例:
Exercise 9-6 Grade Student Scores by Level
This problem requires implementing a simple function that sets a grade for a student based on their score and counts the number of students who failed.
Function interface definition:
int set_grade( struct student *p, int n );
where p is a pointer to the array of student information structures, defined as:
struct student{
int num;
char name[20];
int score;
char grade;
};
n is the number of elements in the array. The student ID num , name name and score score are all already stored. The set_grade function needs to set the grade grade of each student according to their score score . Grade setting: 85-100 is A, 70-84 is B, 60-69 is C, 0-59 is D. At the same time, set_grade also needs to return the number of students who failed.
Sample judge program:
1 |
|
输入样例:
10
31001 annie 85
31002 bonny 75
31003 carol 70
31004 dan 84
31005 susan 90
31006 paul 69
31007 pam 60
31008 apple 50
31009 nancy 100
31010 bob 78
输出样例:
The count for failed (<60): 1
The grades:
31001 annie A
31002 bonny B
31003 carol B
31004 dan B
31005 susan A
31006 paul C
31007 pam C
31008 apple D
31009 nancy A
31010 bob B
代码:
Input sample:
10
31001 annie 85
31002 bonny 75
31003 carol 70
31004 dan 84
31005 susan 90
31006 paul 69
31007 pam 60
31008 apple 50
31009 nancy 100
31010 bob 78
Output sample:
The count for failed (<60): 1
The grades:
31001 annie A
31002 bonny B
31003 carol B
31004 dan B
31005 susan A
31006 paul C
31007 pam C
31008 apple D
31009 nancy A
31010 bob B
Code:
1 | int set_grade( struct student *p, int n ){ |
练习10-1 使用递归函数计算1到n之和
本题要求实现一个用递归计算1+2+3+…+n的和的简单函数。
函数接口定义:
int sum( int n );
该函数对于传入的正整数n返回1+2+3+…+n的和;若n不是正整数则返回0。题目保证输入输出在长整型范围内。建议尝试写成递归函数。
裁判测试程序样例:
Exercise 10-1 Use a Recursive Function to Calculate the Sum from 1 to n
This problem requires implementing a simple function that uses recursion to calculate the sum of 1+2+3+…+n.
Function interface definition:
int sum( int n );
This function returns the sum of 1+2+3+…+n for a positive integer n passed in; if n is not a positive integer, it returns 0. The problem guarantees that input and output are within the long integer range. It is recommended to try writing it as a recursive function.
Sample judge program:
1 |
|
输入样例1:
10
输出样例1:
55
输入样例2:
0
输出样例2:
0
代码:
Input sample 1:
10
Output sample 1:
55
Input sample 2:
0
Output sample 2:
0
Code:
1 | int sum( int n ){ |
习题10-1 判断满足条件的三位数
本题要求实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如144、676)的个数。
函数接口定义:
int search( int n );
其中传入的参数int n是一个三位数的正整数(最高位数字非0)。函数search返回[101, n]区间内所有满足条件的数的个数。
裁判测试程序样例:
Exercise 10-1 Determine Three-digit Numbers That Meet the Condition
This problem requires implementing a function that counts the number of perfect squares among three-digit numbers in a given interval that have two identical digits (such as 144, 676).
Function interface definition:
int search( int n );
where the parameter int n passed in is a positive three-digit number (the most significant digit is not 0). The function search returns the number of numbers in the interval [101, n] that meet the condition.
Sample judge program:
1 |
|
输入样例:
500
输出样例:
count=6
代码:
Input sample:
500
Output sample:
count=6
Code:
1 | int search( int n ){ |
习题10-2 递归求阶乘和
本题要求实现一个计算非负整数阶乘的简单函数,并利用该函数求 1!+2!+3!+…+n! 的值。
函数接口定义:
double fact( int n );
double factsum( int n );
函数 fact 应返回 n 的阶乘,建议用递归实现。函数 factsum 应返回 1!+2!+…+ n! 的值。题目保证输入输出在双精度范围内。
裁判测试程序样例:
Exercise 10-2 Recursively Find the Sum of Factorials
This problem requires implementing a simple function that calculates the factorial of a non-negative integer, and using it to find the value of 1!+2!+3!+…+n!.
Function interface definition:
double fact( int n );
double factsum( int n );
The function fact should return the factorial of n ; recursion is recommended. The function factsum should return the value of 1!+2!+…+ n! . The problem guarantees that input and output are within the double-precision range.
Sample judge program:
1 |
|
输入样例1:
10
输出样例1:
fact(10) = 3628800
sum = 4037913
输入样例2:
0
输出样例2:
fact(0) = 1
sum = 0
代码:
Input sample 1:
10
Output sample 1:
fact(10) = 3628800
sum = 4037913
Input sample 2:
0
Output sample 2:
fact(0) = 1
sum = 0
Code:
1 | double fact( int n ){ |
习题10-3 递归实现指数函数
本题要求实现一个计算xn(n≥1)的函数。
函数接口定义:
double calc_pow( double x, int n );
函数 calc_pow 应返回 x 的 n 次幂的值。建议用递归实现。题目保证结果在双精度范围内。
裁判测试程序样例:
Exercise 10-3 Recursively Implement the Exponential Function
This problem requires implementing a function that calculates xn (n≥1).
Function interface definition:
double calc_pow( double x, int n );
The function calc_pow should return the value of x raised to the power of n. Recursion is recommended. The problem guarantees that the result is within the double-precision range.
Sample judge program:
1 |
|
输入样例:
2 3
输出样例:
8
代码:
Input sample:
2 3
Output sample:
8
Code:
1 | double calc_pow( double x, int n ){ |
习题10-4 递归求简单交错幂级数的部分和
本题要求实现一个函数,计算下列简单交错幂级数的部分和:
f(x,n)=x−x2+x3 −x4+⋯+(−1)n−1xn
函数接口定义:
double fn( double x, int n );
其中题目保证传入的n是正整数,并且输入输出都在双精度范围内。函数fn应返回上述级数的部分和。建议尝试用递归实现。
裁判测试程序样例:
Exercise 10-4 Recursively Find the Partial Sum of a Simple Alternating Power Series
This problem requires implementing a function that calculates the partial sum of the following simple alternating power series:
f(x,n)=x−x2+x3 −x4+⋯+(−1)n−1xn
Function interface definition:
double fn( double x, int n );
where the problem guarantees that the passed-in n is a positive integer, and both input and output are within the double-precision range. The function fn should return the partial sum of the above series. Recursion is recommended.
Sample judge program:
1 |
|
输入样例:
0.5 12
输出样例:
0.33
代码:
Input sample:
0.5 12
Output sample:
0.33
Code:
1 | double fn( double x, int n ) |
习题10-5 递归计算Ackermenn函数
本题要求实现Ackermenn函数的计算,其函数定义如下:

函数接口定义:
int Ack( int m, int n );
其中 m 和 n 是用户传入的非负整数。函数 Ack 返回Ackermenn函数的相应值。题目保证输入输出都在长整型范围内。
裁判测试程序样例:
Exercise 10-5 Recursively Calculate the Ackermann Function
This problem requires implementing the calculation of the Ackermann function, whose definition is as follows:

Function interface definition:
int Ack( int m, int n );
where m and n are non-negative integers passed in by the user. The function Ack returns the corresponding value of the Ackermann function. The problem guarantees that both input and output are within the long integer range.
Sample judge program:
1 |
|
输入样例:
2 3
输出样例:
9
代码:
Input sample:
2 3
Output sample:
9
Code:
1 | int Ack( int m, int n ){ |
习题10-6 递归求Fabonacci数列
本题要求实现求Fabonacci数列项的函数。Fabonacci数列的定义如下:
f(n)=f(n−2)+f(n−1) (n≥2),其中f(0)=0,f(1)=1。
函数接口定义:
int f( int n );
函数 f 应返回第 n 个Fabonacci数。题目保证输入输出在长整型范围内。建议用递归实现。
裁判测试程序样例:
Exercise 10-6 Recursively Find Terms of the Fibonacci Sequence
This problem requires implementing a function that finds terms of the Fibonacci sequence. The Fibonacci sequence is defined as follows:
f(n)=f(n−2)+f(n−1) (n≥2), where f(0)=0 and f(1)=1.
Function interface definition:
int f( int n );
The function f should return the n -th Fibonacci number. The problem guarantees that both input and output are within the long integer range. Recursion is recommended.
Sample judge program:
1 |
|
输入样例:
6
输出样例:
8
代码:
Input sample:
6
Output sample:
8
Code:
1 | int f( int n ){ |
习题10-7 十进制转换二进制
本题要求实现一个函数,将正整数n转换为二进制后输出。
函数接口定义:
void dectobin( int n );
函数 dectobin 应在一行中打印出二进制的 n 。建议用递归实现。
裁判测试程序样例:
Exercise 10-7 Convert Decimal to Binary
This problem requires implementing a function that converts a positive integer n to binary and outputs it.
Function interface definition:
void dectobin( int n );
The function dectobin should print the binary n in one line. Recursion is recommended.
Sample judge program:
1 |
|
输入样例:
10
输出样例:
1010
代码:
Input sample:
10
Output sample:
1010
Code:
1 | void dectobin( int n ){ |
习题10-8 递归实现顺序输出整数
本题要求实现一个函数,对一个整数进行按位顺序输出。
函数接口定义:
void printdigits( int n );
函数 printdigits 应将 n 的每一位数字从高位到低位顺序打印出来,每位数字占一行。
裁判测试程序样例:
Exercise 10-8 Recursively Output an Integer Digit by Digit
This problem requires implementing a function that outputs an integer digit by digit in order.
Function interface definition:
void printdigits( int n );
The function printdigits should print each digit of n in order from the most significant to the least significant, with each digit on its own line.
Sample judge program:
1 |
|
输入样例:
12345
输出样例:
1
2
3
4
5
代码:
Input sample:
12345
Output sample:
1
2
3
4
5
Code:
1 | void printdigits( int n ){ |
习题11-1 输出月份英文名
本题要求实现函数,可以返回一个给定月份的英文名称。
函数接口定义:
char *getmonth( int n );
函数 getmonth 应返回存储了 n 对应的月份英文名称的字符串头指针。如果传入的参数 n 不是一个代表月份的数字,则返回空指针NULL。
裁判测试程序样例:
Exercise 11-1 Output the English Name of a Month
This problem requires implementing a function that can return the English name of a given month.
Function interface definition:
char *getmonth( int n );
The function getmonth should return the string head pointer storing the English month name corresponding to n . If the parameter n passed in is not a number representing a month, return the null pointer NULL.
Sample judge program:
1 |
|
输入样例1:
5
输出样例1:
May
输入样例2:
15
输出样例2:
wrong input!
代码:
Input sample 1:
5
Output sample 1:
May
Input sample 2:
15
Output sample 2:
wrong input!
Code:
1 | char *getmonth( int n ){ |
习题11-2 查找星期
本题要求实现函数,可以根据下表查找到星期,返回对应的序号。
| 序号 | 星期 |
|---|---|
| 0 | Sunday |
| 1 | Monday |
| 2 | Tuesday |
| 3 | Wednesday |
| 4 | Thursday |
| 5 | Friday |
| 6 | Saturday |
函数接口定义:
int getindex( char *s );
函数 getindex 应返回字符串 s 序号。如果传入的参数 s 不是一个代表星期的字符串,则返回-1。
裁判测试程序样例:
Exercise 11-2 Look Up the Day of the Week
This problem requires implementing a function that can look up the day of the week according to the table below and return the corresponding index.
| Index | Day of the week |
|---|---|
| 0 | Sunday |
| 1 | Monday |
| 2 | Tuesday |
| 3 | Wednesday |
| 4 | Thursday |
| 5 | Friday |
| 6 | Saturday |
Function interface definition:
int getindex( char *s );
The function getindex should return the index of the string s . If the parameter s passed in is not a string representing a day of the week, return -1.
Sample judge program:
1 |
|
输入样例1:
Tuesday
输出样例1:
2
输入样例2:
today
输出样例2:
wrong input!
代码:
Input sample 1:
Tuesday
Output sample 1:
2
Input sample 2:
today
Output sample 2:
wrong input!
Code:
1 | int getindex( char *s ){ |
习题 11-3 习题11-3 计算最长的字符串长度
本题要求实现一个函数,用于计算有n个元素的指针数组s中最长的字符串的长度。
函数接口定义:
int max_len( char *s[], int n );
其中n个字符串存储在s[]中,函数max_len应返回其中最长字符串的长度。
裁判测试程序样例:
Exercise 11-3 Calculate the Length of the Longest String
This problem requires implementing a function that calculates the length of the longest string in a pointer array s with n elements.
Function interface definition:
int max_len( char *s[], int n );
where the n strings are stored in s[], and the function max_len should return the length of the longest string among them.
Sample judge program:
1 |
|
输入样例:
4
blue
yellow
red
green
输出样例:
6
代码:
Input sample:
4
blue
yellow
red
green
Output sample:
6
Code:
1 | int max_len( char *s[], int n ){ |
习题 11-4 字符串的连接
本题要求实现一个函数,将两个字符串连接起来。
函数接口定义:
char *str_cat( char *s, char *t );
函数str_cat应将字符串t复制到字符串s的末端,并且返回字符串s的首地址。
裁判测试程序样例:
Exercise 11-4 Concatenating Strings
This problem requires implementing a function that concatenates two strings.
Function interface definition:
char *str_cat( char *s, char *t );
The function str_cat should copy the string t to the end of the string s and return the starting address of the string s.
Sample judge program:
1 |
|
输入样例:
abc
def
输出样例:
abcdef
abcdef
代码:
Input sample:
abc
def
Output sample:
abcdef
abcdef
Code:
1 | char *str_cat( char *s, char *t ){ |
习题11-5 指定位置输出字符串
本题要求实现一个函数,对给定的一个字符串和两个字符,打印出给定字符串中从与第一个字符匹配的位置开始到与第二个字符匹配的位置之间的所有字符。
函数接口定义:
char *match( char *s, char ch1, char ch2 );
函数match应打印s中从ch1到ch2之间的所有字符,并且返回ch1的地址。
裁判测试程序样例:
Exercise 11-5 Output a String at Specified Positions
This problem requires implementing a function that, for a given string and two characters, prints all characters in the given string from the position matching the first character to the position matching the second character.
Function interface definition:
char *match( char *s, char ch1, char ch2 );
The function match should print all characters in s from ch1 to ch2 and return the address of ch1.
Sample judge program:
1 |
|
输入样例1:
program
r g
输出样例1:
rog
rogram
输入样例2:
program
z o
输出样例2:
(空行)
(空行)
输入样例3:
program
g z
输出样例3:
gram
gram
代码:
Input sample 1:
program
r g
Output sample 1:
rog
rogram
Input sample 2:
program
z o
Output sample 2:
(empty line)
(empty line)
Input sample 3:
program
g z
Output sample 3:
gram
gram
Code:
1 | char *match( char *s, char ch1, char ch2 ){ |
习题11-6 查找子串
本题要求实现一个字符串查找的简单函数。
函数接口定义:
char *search( char *s, char *t );
函数search在字符串s中查找子串t,返回子串t在s中的首地址。若未找到,则返回NULL。
裁判测试程序样例:
Exercise 11-6 Find a Substring
This problem requires implementing a simple string search function.
Function interface definition:
char *search( char *s, char *t );
The function search looks for the substring t in the string s and returns the starting address of the substring t in s. If not found, it returns NULL.
Sample judge program:
1 |
|
输入样例1:
The C Programming Language
ram
输出样例1:
10
输入样例2:
The C Programming Language
bored
输出样例2:
-1
代码:
Input sample 1:
The C Programming Language
ram
Output sample 1:
10
Input sample 2:
The C Programming Language
bored
Output sample 2:
-1
Code:
1 | char *search( char *s, char *t ){ |
Exercise 11-7 Linked List of Nodes with Odd Values
This problem requires implementing two functions: one stores the read data as a singly linked list, and the other reorganizes the nodes with odd values in the linked list into a new linked list. The linked list node is defined as follows:
1 | struct ListNode { |
函数接口定义:
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数getodd将单链表L中奇数值的结点分离出来,重新组成一个新的链表。返回指向新链表头结点的指针,同时将L中存储的地址改为删除了奇数值结点后的链表的头结点地址(所以要传入L的指针)。
裁判测试程序样例:
Function interface definition:
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
The function readlist reads a series of positive integers from standard input and builds a singly linked list in the reading order. When −1 is read, it means the input ends, and the function should return a pointer to the head node of the singly linked list.
The function getodd separates the nodes with odd values in the singly linked list L and reorganizes them into a new linked list. It returns a pointer to the head node of the new linked list, and also changes the address stored in L to the head node address of the linked list after the odd-valued nodes are deleted (so the pointer to L is passed in).
Sample judge program:
1 |
|
输入样例:
1 2 2 3 4 5 6 7 -1
输出样例:
1 3 5 7
2 2 4 6
代码:
Input sample:
1 2 2 3 4 5 6 7 -1
Output sample:
1 3 5 7
2 2 4 6
Code:
1 | struct ListNode *readlist(){ |
Exercise 11-8 Deleting Nodes from a Singly Linked List
This problem requires implementing two functions: one stores the read data as a singly linked list, and the other deletes all nodes in the linked list that store a given value. The linked list node is defined as follows:
1 | struct ListNode { |
函数接口定义:
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数deletem将单链表L中所有存储了m的结点删除。返回指向结果链表头结点的指针。
裁判测试程序样例:
Function interface definition:
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
The function readlist reads a series of positive integers from standard input and builds a singly linked list in the reading order. When −1 is read, it means the input ends, and the function should return a pointer to the head node of the singly linked list.
The function deletem deletes all nodes in the singly linked list L that store m. It returns a pointer to the head node of the resulting linked list.
Sample judge program:
1 |
|
输入样例:
10 11 10 12 10 -1
10
输出样例:
11 12
代码:
Input sample:
10 11 10 12 10 -1
10
Output sample:
11 12
Code:
1 | struct ListNode *readlist(){ |


