第十周练兵区——编程题——不计入总分

1有趣的“回文”检测(4分)

英文中有很多的回文词,回文词的拼法十分有趣,无论是从前往后拼读,还是从后往前拼读,他们的拼法和词义都不变。例如:dad(爸爸),mum(妈妈),noon(中午),eve(前夕),eye(眼睛),pop(流行),deed(行为),level(水平)等。简单地说,“回文”就是指顺读和倒读都一样的字符串。现在请你编程输入一个单词,判断它是否是回文。

提示:

(1)设置两个指针pStart和pEnd,让pStart指向字符串首部,让pEnd指向字符串尾部。

(2)利用循环从字符串两边对指针所指字符进行比较,当对应的两字符相等且两指针未超越对方时,使指针pStart向前移动一个字符位置(加1),使指针pEnd向后移动一个字符位置(减1),一旦发现两字符不等或两指针已互相超越(不可能是回文),则立即停止循环。

(3)根据退出循环时两指针的位置,判断字符串是否为回文。

程序的两次运行结果如下:

第1次

Input string:ABCCBA↙

Yes!

第2次

Input string:student↙

No!

输入提示信息:“Input string:”

输入格式: 用gets()函数

输出格式:

输出信息,不是回文:“No!\n”

输出信息,是回文:“Yes!\n”

#include
#include
int f(char *pStart,char *pEnd,int n)
{
  int i;
  for(i=0;i

2学生成绩管理系统V1.0(4分)

某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,用一维数组作函数参数编程实现如下学生成绩管理:

(1)录入每个学生的学号和考试成绩;

(2)计算课程的总分和平均分;

(3)按成绩由高到低排出名次表;

(4)按学号由小到大排出成绩表;

(5)按学号查询学生排名及其考试成绩;

(6)按优秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比;

(7)输出每个学生的学号、考试成绩。

程序运行结果示例:

Input student number(n<30):

6↙

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

1↙

Input student’s ID, name and score:

11003001 87↙

11003005 98↙

11003003 75↙

11003002 48↙

11003004 65↙

11003006 100↙

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

2↙

sum=473,aver=78.83

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

3↙

Sort in descending order by score:

11003006 100

11003005 98

11003001 87

11003003 75

11003004 65

11003002 48

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

4↙

Sort in ascending order by number:

11003001 87

11003002 48

11003003 75

11003004 65

11003005 98

11003006 100

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

5↙

Input the number you want to search:

11003004

11003004 65

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

6↙

<60 1 16.67%

60-69 1 16.67%

70-79 1 16.67%

80-89 1 16.67%

90-99 1 16.67%

100 1 16.67%

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

7↙

11003001 87

11003002 48

11003003 75

11003004 65

11003005 98

11003006 100

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

8↙

Input error!

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

0↙

End of program!

输入格式:

( 1 )录入学生的人数:

             **输入数据格式:"%d"

             **提示信息:"Input student number(n<30):\n"

( 2 )录入每个学生的学号和考试成绩:

           **输入数据格式:"%ld%f"

           **提示信息:"Input student's ID, name and score:\n"

输出格式:

菜单项的输出显示:

Management for Students’ scores

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by number

5.Search by number

6.Statistic analysis

7.List record

0.Exit

Please Input your choice:

计算课程的总分和平均分:

          **输出总分与平均分格式:"sum=%.0f,aver=%.2f\n"

按成绩由高到低排出名次表:

          **输出格式:"%ld\t%.0f\n"

          **提示信息:"Sort in descending order by score:\n"

按学号由小到大排出成绩表:

          **输出格式:"%ld\t%.0f\n"

          **提示信息:"Sort in ascending order by number:\n"

按学号查询学生排名及其考试成绩:

           **如果未查到此学号的学生,提示信息:"Not found!\n"

           **如果查询到该学生,输出格式:"%ld\t%.0f\n"

按优秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比:

            **成绩<60输出格式:"<60\t%d\t%.2f%%\n"

            **成绩=100输出格式:"%d\t%d\t%.2f%%\n"

            **其他输出百分比格式:"%d-%d\t%d\t%.2f%%\n" 
#include
#include
#include
void a(long num[],float s[],int n)
{
	int i;
    for(i=0;is[k])
				k=j;
		}
        if(k!=i)
		{
		    g(&num[k],&num[i]);
			f(&s[k],&s[i]);
		} 	
	}
}
void d(long num[],float s[],int n)
{
	int i,j,k;
	for(i=0;i= 0 &&s[i]<60) t[0]++;
        else if (s[i] < 70)            t[1]++;
        else if (s[i] < 80)            t[2]++;
        else if (s[i] < 90)            t[3]++;
        else if (s[i] < 100)           t[4]++;
        else if (s[i] == 100)        t[5]++;
    }
    for (i = 0; i <= 5; i++)
    {                              
        if (i == 0) printf("<60\t%d\t%.2f%%\n", t[i], (float)t[i] / n * 100);
        else if (i == 5) printf("%d\t%d\t%.2f%%\n",
                                    (i + 5) * 10, t[i], (float)t[i] / n * 100);
        else    printf("%d-%d\t%d\t%.2f%%\n",
                           (i + 5) * 10, (i + 5) * 10 + 9, t[i], (float)t[i] / n * 100);
    }
}                              
main()
{
	int i,n;
	float sum=0,aver;
	float s[30];
	long  num[30];
	char ch;
	printf("Input student number(n<30):\n");
	scanf("%d",&n);
	while(1)
	{
		ch=b();
	switch(ch)
	{
	case 1:
		printf("Input student's ID, name and score:\n");
		a(num,s,n);
		break;
	case 2:
		for(i=0;i

3程序改错——1(4分)

下面程序的功能是,从键盘输入两个字符串,分别存放在字符数组d和s中,通过调用子函数MyStrcat( )将这两个字符串连接起来,并将连接后的字符串存放在字符数组r中,同时输出连接后的字符串。已知每个字符数组的最大长度为80。下面给出的程序存在错误,找到错误的原因后,请修改正确。并按照给出的程序运行结果示例检查你的程序。

已知函数原型:char* MyStrcat(char *dest, char *source);//函数返回连接后的字符串的首地址

#include 
#include 
int main(void)
{
        char *first, *second, *result;
        printf("Input the first string:\n");
        gets(first);
        printf("Input the second string:\n");
        gets(second);
        result = MyStrcat(first, second);
        printf("The result is : %s\n", result);
        return 0;
}
char* MyStrcat(char *dest, char *source)
{
        int i = 0;
        while (*(dest+i)!='\0')   i++;
        for (; *(source+i)!='\0'; i++)
        {
            *(dest+i) = *(source+i);   
        }
        return dest;
}

程序运行结果示例1:

Input the first string:

fri↙

Input the second string:

end↙

The result is : friend

程序运行结果示例2:

Input the first string:

home↙

Input the second string:

work↙

The result is : homework

输入提示信息:“Input the first string:\n”

输入提示信息:“Input the second string:\n”

输入格式: 使用gets()函数

输出格式:“The result is : %s\n”

#include 
#include 
char* MyStrcat(char *dest, char *source);
int main(void)
{
        char *first, *second, *result;
		char d[81], s[81];
        printf("Input the first string:\n");
        gets(d);
        printf("Input the second string:\n");
        gets(s);
		first = &d[0];
		second = &s[0];
        result = MyStrcat(first, second);
        printf("The result is : %s\n", result);
        return 0;
}
char* MyStrcat(char *dest, char *source)
{
	int i = 0, p = 0;
	while (*(dest+i)!='\0')   i++;
	for (; *(source+p)!='\0'; i++, p++)
	{
		*(dest+i) = *(source+p);
	}
	*(dest+i) = '\0';
	return dest;
}

4程序改错——2(4分)

下面程序的功能是输出如示例所示形式的数据,目前程序中存在错误,找到错误的原因后,请修改正确,并按照给出的程序运行结果示例检查你的程序。

程序中用到的函数原型如下:

void YH(int a[][ARR_SIZE], int n);

void PrintYH(int a[][ARR_SIZE], int n);

程序运行结果示例:

1

1 1

1 2 1

1 3 3 1

#include
#define  ARR_SIZE  5
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{
        int  a[ARR_SIZE][ARR_SIZE];
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
}
void YH(int a[][ARR_SIZE], int n)
{
        int  i, j ;
        for (i=1; i<=n; i++)
        {  
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<=n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }       
        }
}
void PrintYH(int a[][ARR_SIZE], int n)
{
        int i , j ;
        for (i=1; i

输入格式: 无

输出格式: “%4d”

#include
#define  ARR_SIZE  5
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{
        int  a[ARR_SIZE][ARR_SIZE];
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
}
void YH(int a[][ARR_SIZE], int n)
{
        int  i, j ;
        for (i=1; i

5出售金鱼(4分)

买买提将养的一缸金鱼分五次出售:第一次卖出全部的一半加二分之一条;第二次卖出余下的三分之一加三分之一条;第三次卖出余下的四分之一加四分之一条;第四次卖出余下的五分之一加五分之一条;最后卖出剩下的11条。问原来鱼缸中共有几条鱼?

输入格式: 无

输出格式:“There are %d fishes at first.\n”

#include
int f(int x1){
	int i,x2=11;
	for(i=5;i>1;i--){
		x1=(x2*i+1)/(i-1);
		x2=x1;
	}
	return x1;
}
main(){
	int n;
printf("There are %d fishes at first.\n",f(n));
   return 0;
}

6找最值(4分)

从键盘任意输入10个整数,用指针变量作函数参数编程计算最大值和最小值,并返回它们所在数组中的位置。函数原型如下所示:

int FindMax(int num[], int n, int *pMaxPos);//函数返回最大值,pMaxPos返回最大值所在的下标

int FindMin(int num[], int n, int *pMinPos);//函数返回最小值,pMaxPos返回最小值所在的下标

程序运行结果示例:

Input 10 numbers:

-1 2 3 45 92 8 9 12 7 8↙

Max=92,Position=4,Min=-1,Position=0

输入提示信息:“Input 10 numbers:\n”

输入格式: “%d”

输出格式:“Max=%d,Position=%d,Min=%d,Position=%d\n”

#include
int FindMax(int num[], int n, int *pMaxPos);
int FindMin(int num[], int n, int *pMinPos);
main()
{
	int n=10,i,num[100],max,min,MaxPos,MinPos;
	printf("Input 10 numbers:\n");
	for(i=0;inum[i]){
		min=num[i];
		*pMinPos=i;}
	}
	return min;
}

7杨辉三角形(4分)

编程打印具有如下形式的杨辉三角形,其中输出数据的行数n从键盘输入,并且n<=10。

程序运行结果示例1:

Input n (n<=10):

5↙

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

程序运行结果示例2:

Input n (n<=10):

7↙

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

输入提示信息:“Input n (n<=10):\n”

输入格式: “%d”

输出格式:

输出数据格式:"%4d"

数据换行: “\n”

#include
main()
{
	int n,a[11][11],i,j;
	printf("Input n (n<=10):\n");
	scanf("%d",&n);
    for(i=1;i<=n;i++){
		for(j=1;j<=i;j++){
			if(j==1||j==i)
				a[i][j]=1;
			else 
				a[i][j]=a[i-1][j-1]+a[i-1][j];
		}
	}
	for(i=1;i<=n;i++){
		for(j=1;j<=i;j++)
			printf("%4d",a[i][j]);
			printf("\n");
	}
	return 0;
}

8颠倒句子中的单词顺序(4分)

从键盘输入一个句子(假设字符数小于100个),句子中的单词之间用空格分隔,句子必须以一个标点符号作为结尾,句子开头和末尾标点符号前均没有空格,以回车表示输入结束,请编程颠倒句中的单词顺序并输出。

函数原型:int Inverse(char str1[], char str2[][N])

程序运行结果示例1:

Input a sentence:you can cage a swallow can’t you?↙

you can’t swallow a cage can you?

程序运行结果示例2:

Input a string:you are my sunshine!↙

sunshine my are you!

程序运行结果示例3:

Input a sentence:I love you!↙

you love I!

输入提示信息:“Input a sentence:”

输入格式: 用gets()函数

输出格式:

每个单词的输出格式:"%s " (注意: %s后面有一个空格)

最后一个单词和标点符号的输出格式:"%s%c\n"

#include
#include
#define N 100
char *r(char *str,int len);
char *rs(char *str);
main()
{
	char str[N];
	char n;
	printf("Input a sentence:");
	gets(str);
	n=str[strlen(str)-1];
    str[strlen(str)-1]='\0';
    printf("%s%c\n",rs(str),n);
	return 0;
}
char *rs(char *str)
{
	char *pbegin=str;
	char *pend=str;
	if(str==NULL)
	return str;
	r(str,strlen(str));
	while(*pend!='\0')
	{
		while(*pend!='\0'&&*pend!=' '){
			pend++;}
		r(pbegin,pend-pbegin);
		if(*pend=='\0')
			break;
		pend++;
		pbegin=pend;
	}
   return str;
}
char *r(char *str,int len)
{
	char *plast=str+len-1;
	char *pbegin=str;
	char temp;
	if(str==NULL||len<=0)
		return str;
	while(pbegin

你可能感兴趣的:(第十周练兵区——编程题——不计入总分)