本题要求实现一个计算m~n(m 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求对两个整数a和b,输出其中较大的数。 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现符号函数sign(x)。 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现判断给定整数奇偶性的函数。 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现一个函数,对给定平面任意两点坐标 函数接口定义: 其中用户传入的参数为平面上两个点的坐标( 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现一个判断素数的简单函数、以及利用该函数计算给定区间内素数和的函数。 函数接口定义: 其中函数 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 水仙花数是指一个N位正整数(N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:153=1 ^3 + 5 ^3 + 3 ^3 。 函数接口定义: 函数 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现两个函数:一个函数判断给定正整数的各位数字之和是否等于5;另一个函数统计给定区间内有多少个满足上述要求的整数,并计算这些整数的和。 函数接口定义: 函数 进行输出。题目保证0< 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现一个计算x ^n (n≥0)的函数。 函数接口定义: 函数 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 主函数中通过键盘输入年份,调用函数fun判断该年是否为闰年。 函数接口定义: 其中 裁判测试程序样例: /* 请在这里填写答案 */ 本题要求编写函数,将输入字符串的前3个字符移到最后。 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现一个在数组中查找指定元素的简单函数。 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现一个对数组进行循环右移的简单函数:一个数组a中存有n(>0)个整数,将每个整数循环向右移m(≥0)个位置,即将a中的数据由(a_0a_1··· a_{n-1}变换为(a_{n-m} ··· a_{n-1}a_0a_1 ··· a_{n-m-1})(最后m个数循环移至最前面的m个位置)。 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 报数游戏是这样的:有n个人围成一圈,按顺序从1到n编好号。从第一个人开始报数,报到m( 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现一个删除字符串中的指定字符的简单函数。 函数接口定义: 其中 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 本题要求实现一个函数,统计给定字符串中的大写字母、小写字母、空格、数字以及其它字符各有多少。 函数接口定义: 其中 的格式输出。 裁判测试程序样例: /* Your function will be put here / 本题要求实现一个函数,使用指针传递对结构体参数进行输入赋值。 函数接口定义: 其中 裁判测试程序样例: /* 请在这里填写答案 */ 本题要求实现一个计算复数之积的简单函数。 函数接口定义: 裁判测试程序样例: /* 你的代码将被嵌在这里 */ 输入n(n<50)个学生的成绩信息,再输入一个学生的学号、课程以及成绩,在自定义函数update_score()中修改该学生指定课程的成绩。 函数接口定义: 其中p是结构指针,n是学生个数,course是课程序号,score是修改后的课程成绩。函数须返回指定学生的顺序位置,如果查无此人,返回-1。 裁判测试程序样例: /* 请在这里填写答案 */ 定义如下时间结构类型 注:为了节约内存,时数最大为23,分数和秒数最大为59,因此时数、分数和秒数均采用最短的整数类型(char)。 函数原型 说明:参数 time 为指示时间结构变量的指针,hour、minute 和 second 分别为时、分和秒数。若参数 hour、minute 和 second 正确有效,则函数将 time 所指时间结构变量设置成该时间,否则报告错误,不修改时间结构变量的值 裁判程序 提示:利用前面练习中的 IsValidTime 函数判断时间。 输入样例1 学生结构体定义如下: 其中 函数接口定义: 其中 裁判测试程序样例: /* 你所编写的函数代码将被嵌在这里 */ 关于日期的结构定义如下: 编写两个函数,一个计算自公元1年1月1日到指定的日期共经历了多少天。另一个是前一个函数的逆函数:由自公元1年1月1日历经指定的天数后的日期(返回年月日构成的日期)。 函数接口定义: 裁判测试程序样例: /* 请在这里填写答案 */ 定义如下点结构类型 说明:x 和 y 分别为点的横坐标和纵坐标。 输入样例int sum( int m, int n );
m
和n
是用户传入的参数,保证有m#include
输入样例:
-5 8
输出样例:
sum = 21int sum( int m, int n ){
int ans=0;
for(int i=m;i<=n;i++) ans+=i;
return ans;
}
6-2 找两个数中最大者
int max( int a, int b );
a
和b
是用户传入的参数,函数返回的是两者中较大的数。#include
输入样例:
-5 8
输出样例:
max = 8int max( int a, int b ){
return a>b?a:b;
}
6-3 符号函数
int sign( int x );
x
是用户传入的整型参数。符号函数的定义为:若x
大于0,sign(x) = 1
;若x
等于0,sign(x)
= 0;否则,sign(x)
= −1。#include
输入样例:
10
输出样例:
sign(10) = 1int sign( int x ){
if(x>0) return 1;
else if (x==0) return 0;
else return -1;
}
6-4 判断奇偶性
int even( int n );
n
是用户传入的整型参数。当n
为偶数时,函数返回1;n
为奇数时返回0。注意:0是偶数。#include
输入样例1:
-6
输出样例1:
-6 is even.
输入样例2:
5
输出样例2:
5 is odd.int even( int n ){
if(n%2==0) return 1;
else return 0;
}
6-5 使用函数计算两点间的距离
(x_1,y_1 )和(x_2 ,y_2),求这两点之间的距离。double dist( double x1, double y1, double x2, double y2 );
x1
, y1
)和(x2
, y2
),函数dist应返回两点间的距离。#include
输入样例:
10 10 200 100
输出样例:
dist = 210.24double dist( double x1, double y1, double x2, double y2 ){
return sqrt(pow(x1-x2,2)+(pow(y1-y2,2)));
}
6-6 使用函数求素数和
素数就是只能被1和自身整除的正整数。注意:1不是素数,2是素数。int prime( int p );
int PrimeSum( int m, int n );
prime
当用户传入参数p
为素数时返回1,否则返回0;函数PrimeSum
返回区间[m
, n
]内所有素数的和。题目保证用户传入的参数m
≤n
。#include
输入样例:
-1 10
输出样例:
Sum of ( 2 3 5 7 ) = 17// 质数定义为在大于1的自然数中,
// 除了1和它本身以外不再有其他因数的数称为质数
int prime( int p ){
if(p<=1) return 0;
int flag=1;
for(int i=2;i*i<=p;i++){
if(p%i==0) {
flag=0;
break;
}
}
return flag;
}
int PrimeSum( int m, int n ){
int ans=0;
if(m<=0) m=1;
for(int i=m;i<=n;i++){
if(prime(i)) ans+=i;
}
return ans;
}
6-7 使用函数输出水仙花数
本题要求编写两个函数,一个判断给定整数是否水仙花数,另一个按从小到大的顺序打印出给定区间(m,n)内所有的水仙花数。int narcissistic( int number );
void PrintN( int m, int n );
narcissistic
判断number
是否为水仙花数,是则返回1,否则返回0。
函数PrintN
则打印开区间(m
, n
)内所有的水仙花数,每个数字占一行。题目保证100≤m
≤n
≤10000。#include
输入样例:
153 400
输出样例:
153 is a narcissistic number
370
371# include
6-8 统计各位数字之和是5的数
int is( int number );
void count_sum( int a, int b );
is
判断number
的各位数字之和是否等于5,是则返回1,否则返回0。
函数count_sum
利用函数is
统计给定区间[a
, b
]内有多少个满足上述要求(即令is
返回1)的整数,并计算这些整数的和。最后按照格式count = 满足条件的整数个数, sum = 这些整数的和
a
≤b
≤10000。#include
输入样例:
104 999
输出样例:
104 is counted.
count = 15, sum = 3720int is( int number ){
int sum=0;
while(number){
sum+=number%10;
number/=10;
}
if(sum==5) return 1;
else return 0;
}
void count_sum( int a, int b ){
int count=0,sum=0;
for(int i=a;i<=b;i++){
if(is(i)) {
count++;
sum+=i;
}
}
printf("count = %d, sum = %d",count,sum);
}
6-9 简单实现x的n次方
double mypow( double x, int n );
mypow
应返回x
的n
次幂的值。题目保证结果在双精度范围内。#include
输入样例:
0.24 4
输出样例:
0.003318double mypow( double x, int n ){
double ans=1;
while(n--) ans*=x;
return ans;
}
6-10 函数判断闰年
int fun(int year);
year
是用户传入的参数。 year
的值不超过int
的范围。 year
是闰年函数须返回 1 ,否则返回0.。#include
输入样例:
2000
输出样例:
yes
输入样例:
1800
输出样例:
noint fun(int year){
if((year%4==0 && year%100!=0)|| year%400==0) return 1;
else return 0;
}
6-11 移动字母
void Shift( char s[] );
char s[]
是用户传入的字符串,题目保证其长度不小于3;函数Shift
须将按照要求变换后的字符串仍然存在s[]
里。#include
输入样例:
abcdef
输出样例:
defabcvoid Shift( char s[] ){
int len=strlen(s),n=3;
while(n--){
char temp=s[0];
for(int i=1;i<len;i++){
s[i-1]=s[i];
}
s[len-1]=temp;
}
}
6-12 在数组中查找指定元素
int search( int list[], int n, int x );
list[]
是用户传入的数组;n
(≥0)是list[]
中元素的个数;x
是待查找的元素。如果找到,则函数search
返回相应元素的最小下标(下标从0开始),否则返回−1。#include
输入样例1:
5
1 2 2 5 4
2
输出样例1:
index = 1
输入样例2:
5
1 2 2 5 4
0
输出样例2:
Not foundint search( int list[], int n, int x ){
for(int i=0;list[i]!='\0';i++){
if(list[i]==x) return i;
}
return -1;
}
6-13 数组循环右移
void ArrayShift( int a[], int n, int m );
a[]
是用户传入的数组;n
是数组的大小;m
是右移的位数。函数ArrayShift
须将循环右移后的数组仍然存在a[]
中。#include
输入样例:
6 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4void ArrayShift( int a[], int n, int m ){
while(m--){
int temp=a[n-1];
for(int i=n-1;i>0;i--){
a[i]=a[i-1];
}
a[0]=temp;
}
}
6-14 报数
void CountOff( int n, int m, int out[] );
n
是初始人数;m是游戏规定的退出位次(保证为小于n的正整数)。函数CountOff
将每个人的退出顺序编号存在数组out[]
中。因为C语言数组下标是从0开始的,所以第i个位置上的人是第out[i-1]
个退出的。#include
输入样例:
11 3
输出样例:
4 10 1 7 5 2 11 9 3 6 8void CountOff( int n, int m, int out[] ){
for(int i=0;i<n;i++) out[i]=-1; // 将数组全部设置为1,表示未退出的状态
int count=0; // 报数
int index=0; // 索引,范围0 - n-1
int rank=1; // 退出的编号,1 - n
while(rank<=n){
if(out[index]==-1) count++;
if(count==m){
count=0; // 重置
out[index]=rank++;
}
index++; // 必须执行完前两步后才能操作index
if(index>n-1) index=0; // 控制索引范围
}
}
6-15 删除字符
void delchar( char *str, char c );
char *str
是传入的字符串,c
是待删除的字符。函数delchar
的功能是将字符串str
中出现的所有c
字符删除。#include
输入样例:
a
happy new year
输出样例:
hppy new yervoid delchar( char *str, char c ){
int cnt=0;
for(int i=0;str[i]!='\0';i++){
if(str[i]!=c) str[cnt++]=str[i]; // 原地删除
}
str[cnt]='\0'; // 字符串结束标志
}
6-16 分类统计各类字符个数
void StringCount( char *s );
char *s
是用户传入的字符串。函数StringCount
须在一行内按照大写字母个数 小写字母个数 空格个数 数字个数 其它字符个数
#include
输入样例:
aZ&?
093 Az
输出样例:
2 2 1 3 4// 大写字母个数 小写字母个数 空格个数 数字个数 其它字符个数
void StringCount( char *s ){
int a=0,b=0,c=0,d=0,e=0;
for(int i=0;s[i]!='\0';i++){
if(s[i]>='A' && s[i]<='Z') a++;
else if(s[i]>='a' && s[i]<='z') b++;
else if(s[i]==' ') c++;
else if(s[i]>='0' && s[i]<='9') d++;
else e++;
}
printf("%d %d %d %d %d",a,b,c,d,e);
}
6-17 结构体指针参数传递
void Func(struct date *p);
struct data *p
是用户传入的结构体类型指针参数。函数实现对该指针所指结构体类型变量进行输入赋值。#include
输入样例:
2023 12 19
输出样例:
2023/12/19void Func(struct date *p){
scanf("%d %d %d",&p->year,&p->month,&p->day);
}
6-18 计算两个复数之积
struct complex multiply(struct complex x, struct complex y);
#include
输入样例:
3 4 5 6
输出样例:
(3+4i) * (5+6i) = -9 + 38istruct complex multiply(struct complex x, struct complex y){
struct complex ans; // 创建结构体
ans.real=x.real*y.real-x.imag*y.imag; // 实部
ans.imag=x.real*y.imag+x.imag*y.real;// 虚部
return ans; // 返回结构体
}
6-19 修改学生成绩
int update_score(struct student *p, int n, int num, int course, int score);
#include
输入样例:
3
101 Zhang 78 87 85
102 Wang 91 88 90
103 Li 75 90 84
102 2 89
输出样例:
在这里给出相应的输出。例如:
102,91,89,90// n是学生个数,num是学号,course是课程序号,score是修改后的课程成绩
// 函数须返回指定学生的顺序位置,如果查无此人,返回-1。!!!审题
int update_score(struct student *p, int n, int num, int course, int score){
for(int i=0;i<n;i++){
if(p[i].num==num){
if(course==1) p[i].math=score;
else if(course==2) p[i].english=score;
else if(course==3) p[i].computer=score;
return i;// 找到此人
}
}
return -1;
}
6-20 时间结构 - 设置
typedef struct
{
char hour, minute, second;
} MYTIME;
void TimeSet(MYTIME *time, int hour, int minute, int second);
#include
8 30 45
输出样例1
8 30 45
输入样例2
24 60 -15
输出样例2
不正确的参数!
0 0 0void TimeSet(MYTIME *time, int hour, int minute, int second){
// if((hour>=0 && hour<=23) && (minute>=0 && minute<=59) && (second>=0 && second<=59)){
if(IsValidTime(hour,minute,second)){
time->hour=hour;
time->minute=minute;
time->second=second;
}
else{
printf("不正确的参数!\n");
// time->hour=0; // 提示:不要修改结构变量的值。
// time->minute=0;
// time->second=0;
}
}
6-21 学生成绩比高低
struct Student{
int sid;
int C;
int English;
};
sid
是学号,C
是C语言课程成绩,English
是英语课程成绩。学生的成绩按照这样的规则比较:
先比较两门课的总成绩,总成绩高的为优;
若总成绩相同,再比较C语言成绩,C语言成绩高的为优;
若C语言成绩也相同,则说明两名学生成绩相等。
编写函数实现成绩的比较。int compareScore(const struct Student *s1, const struct Student *s2);
s1
和s2
是传入的参数,分别指向两名学生的结构体变量。函数返回值为int型,
若s1
所指学生成绩优于s2
所指学生,返回1
;
若s2
所指学生成绩优于s1
所指学生,返回-1
;
若两学生成绩相等,返回0
。#include
输入样例1:
对于样例测试程序的输入格式:
1 95 90
2 90 91
输出样例1:
对于样例测试程序的输出格式:
Greater
输入样例2:
对于样例测试程序的输入格式:
1 90 95
2 95 90
输出样例2:
对于样例测试程序的输出格式:
Lessint compareScore(const struct Student *s1, const struct Student *s2){
int sum1=0,sum2=0;
sum1=s1->C+s1->English;
sum2=s2->C+s2->English;
if(sum1 > sum2) return 1;
else if(sum1 < sum2) return -1;
else{
if(s1->C > s2->C) return 1;
else if(s1->C < s2->C) return -1;
else return 0;
}
}
6-22 日期几何
struct DateG{ int yy,mm,dd;};
struct DateG days2Date(int x);{//from:{1,1,1} + 100-->{1,4,11}
int Days(struct DateG x);// {2,,4,11} return 465 from{1,1,1}
struct DateG{
int yy,mm,dd;
};
char leapYear(int year);//闰年否
struct DateG _DateG(char *st);{//"1919-9-9"-->{1919,9,9}
struct DateG Add(struct DateG x,int y);//{1919,9,9}+50-->{1919,10,29}
char* Date2string(struct DateG x);//{1919,9,9}-->"1919-09-09"
//以上为测试程序提供的测试用函数,略去不表,
//以下两函数是必须自己完成的函数
struct DateG days2Date(int x);{//from:{1,1,1} + 100-->{1,4,11}
int Days(struct DateG x);// {2,,4,11} return 465 from{1,1,1}
int main(){
char st[12];scanf("%s",st);
struct DateG d1=_DateG(st),
d2=Add(d1,60*365);
printf("%s\n",Date2string(d2));
d2=Add(d2,-1000);
printf("%s\n",Date2string(d2));
d2=_DateG("2020-1-23");
printf("%.2f\n",(Days(d2)-Days(d1))/365.0);
return 0;
}
输入样例:
输入用-号分隔的出生年月日,系统确保测试数据的合法性
2001-1-1
输出样例:
共三行输出。分别是:60年(按365天计一年精确计算的)后的退休日期;退休前倒计时1000天的日期;武汉抗疫封城时(2020-1-23)的年龄(按每365天为一岁,保留2位小数)。
[2060-12-17]
[2058-03-23]
19.07int isYear(int x){
return ((x%4==0 && x%100!=0) || x%400==0)?1:0;
}
// 历经x天后的日期
struct DateG days2Date(int x){
struct DateG ans;
ans.yy=1;
ans.mm=1;
ans.dd=1;
// 年
while(x >= (365+isYear(ans.yy))){ // 今年有多少天
x=x-(365+isYear(ans.yy));
ans.yy+=1;
}
// 月
int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
months[2]+=isYear(ans.yy); // 今年2月的天数
while(x>months[ans.mm]){
x-=months[ans.mm];
ans.mm++;
}
// 日
ans.dd+=x;
return ans;
}
// 经历的天数
int Days(struct DateG x){ // 题目错误,形参应该为结构体类型!?!
int ans=0;
for(int i=1;i<x.yy;i++){
ans+=365+isYear(i);
}
int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=1;i<x.mm;i++){
if(i==2) months[i]+=isYear(x.yy);
ans+=months[i];
}
ans+=x.dd-1;
return ans;
}
6-23 求两点间的距离(填空)
typedef struct
{
double x, y;
} POINT;
下面的程序输入两个点的坐标,求两点间的距离并输出。#include
-8.5 6.4
3.7 -1.2**
输出样例
14.3736s=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));