题目内容:
英文中有很多的回文词,回文词的拼法十分有趣,无论是从前往后拼读,还是从后往前拼读,他们的拼法和词义都不变。例如: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!
#include
#define N 100
int main( )
{
char a[N];
char *pStart, *pEnd;
printf("Input string:");
scanf("%s", a);
pStart = a;
pEnd = a;
while(*pEnd!='\0')
pEnd++;
pEnd--;
while(pEnd-pStart>1)
{
if(*pStart!=*pEnd)
{
printf("No!\n");
return 0;
}
pEnd--;
pStart++;
}
printf("Yes!\n");
return 0;
}
题目内容:
某班有最多不超过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!
#include
#define N 30
void Descending(long number[], float score[], int n);
void AscendingNum(long number[], float score[], int n);
float Search(long number[], float score[], int n, long search);
void ResultAnalysis(long number[], float score[], int n);
int main( )
{
int n, choice, i;
long number[N], search;
float score[N], sum, average, result;
printf("Input student number(n<30):\n");
scanf("%d", &n);
do{
printf("Management for Students' scores\n");
printf("1.Input record\n");
printf("2.Caculate total and average score of course\n");
printf("3.Sort in descending order by score\n");
printf("4.Sort in ascending order by number\n");
printf("5.Search by number\n");
printf("6.Statistic analysis\n");
printf("7.List record\n");
printf("0.Exit\n");
printf("Please Input your choice:\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("Input student's ID, name and score:\n");
for(i=0;i<n;i++)
{
scanf("%ld%f", &number[i],&score[i]);
}
break;
}
case 2:
{
sum = 0;
average = 0;
for(i=0;i<n;i++)
{
sum+=score[i];
}
average = sum/n;
printf("sum=%.0f,aver=%.2f\n", sum, average);
break;
}
case 3:
{
printf("Sort in descending order by score:\n");
Descending(number, score, n);
break;
}
case 4:
{
printf("Sort in ascending order by number:\n");
AscendingNum(number, score, n);
break;
}
case 5:
{
printf("Input the number you want to search:\n");
scanf("%ld", &search);
result = Search(number, score, n, search);
if(result==0)
printf("Not found!\n");
else
printf("%ld\t%.0f\n", search, result);
break;
}
case 6:
{
ResultAnalysis(number, score, n);
break;
}
case 7:
{
for(i=0;i<n;i++)
{
printf("%ld\t%.0f\n", number[i], score[i]);
}
break;
}
case 0: break;
default: printf("Input error!\n");
}
}while(choice!=0);
printf("End of program!\n");
return 0;
}
void Descending(long number[], float score[], int n)
{
int i, j, temp, a;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(score[i]<score[j])
{
temp = score[i];
score[i]=score[j];
score[j]=temp;
a = number[i];
number[i]=number[j];
number[j]=a;
}
}
}
for(i=0;i<n;i++)
printf("%ld\t%.0f\n", number[i], score[i]);
}
void AscendingNum(long number[], float score[], int n)
{
int i, j, temp, a;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(number[i]>number[j])
{
temp = score[i];
score[i]=score[j];
score[j]=temp;
a = number[i];
number[i]=number[j];
number[j]=a;
}
}
}
for(i=0;i<n;i++)
printf("%ld\t%.0f\n", number[i], score[i]);
}
float Search(long number[], float score[], int n, long search)
{
int i;
for(i=0;i<n;i++)
{
if(number[i]==search)
return score[i];
}
return 0;
}
void ResultAnalysis(long number[], float score[], int n)
{
int a=0, b=0, c=0, d=0, e=0, full=0, i;
for(i=0;i<n;i++)
{
if(score[i]<60)
e++;
else if(score[i]<70)
d++;
else if(score[i]<80)
c++;
else if(score[i]<90)
b++;
else if(score[i]<100)
a++;
else
full++;
}
printf("<60\t%d\t%.2f%%\n", e, (float)e/n*100);
printf("60-69\t%d\t%.2f%%\n", d, (float)e/n*100);
printf("70-79\t%d\t%.2f%%\n", c, (float)c/n*100);
printf("80-89\t%d\t%.2f%%\n", b, (float)b/n*100);
printf("90-99\t%d\t%.2f%%\n", a, (float)a/n*100);
printf("100\t%d\t%.2f%%\n", full, (float)full/n*100);
}
题目内容:
下面程序的功能是,从键盘输入两个字符串,分别存放在字符数组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
#include
#include
#define N 80
char* MyStrcat(char *dest, char *source);//函数返回连接后的字符串的首地址
int main( )
{
char d[N], s[N];
char *result=NULL;
printf("Input the first string:\n");
gets(d);
printf("Input the second string:\n");
gets(s);
result = MyStrcat(d, s);
printf("The result is : %s\n", result);
return 0;
}
char* MyStrcat(char *dest, char *source)
{
char *r=dest;
while (*dest!='\0')
{
dest++;
}
while(*source!='\0')
{
*dest=*source;
dest++;
source++;
}
*dest='\0';
return r;
}
题目内容:
下面程序的功能是输出如示例所示形式的数据,目前程序中存在错误,找到错误的原因后,请修改正确,并按照给出的程序运行结果示例检查你的程序。
程序中用到的函数原型如下:
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<n; i++)
{
for (j=1; j<=i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
更改后:
#include
#define ARR_SIZE 4
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=0; i<n; i++)
{
a[i][0] = 1;
a[i][i] = 1;
}
for (i=2; i<n; i++)
{
for (j=1; 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=0; i<n; i++)
{
for (j=0; j<=i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
}
题目内容:
买买提将养的一缸金鱼分五次出售:第一次卖出全部的一半加二分之一条;第二次卖出余下的三分之一加三分之一条;第三次卖出余下的四分之一加四分之一条;第四次卖出余下的五分之一加五分之一条;最后卖出剩下的11条。问原来鱼缸中共有几条鱼?
#include
int main()
{
int i, x=11;
for(i=5;i>=2;i--)
{
x = (x*i+1)/(i-1);
}
printf("There are %d fishes at first.\n", x);
return 0;
}
题目内容:
从键盘任意输入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
(其实这题里面我不是很懂指针变量用来干什么)
#include
#define N 50
int FindMax(int num[], int n, int *pMaxPos);//函数返回最大值,pMaxPos返回最大值所在的下标
int FindMin(int num[], int n, int *pMinPos);//函数返回最小值,pMaxPos返回最小值所在的下标
int main()
{
int num[N], i;
int *pMaxPos=num, *pMinPos=num;
int max, min;
printf("Input 10 numbers:\n");
for(i=0;i<10;i++)
{
scanf("%d", &num[i]);
}
max = FindMax(num, 10, pMaxPos);
min = FindMin(num, 10, pMinPos);
printf("Max=%d,Position=%d,Min=%d,Position=%d\n", num[max], max, num[min], min);
return 0;
}
int FindMax(int num[], int n, int *pMaxPos)
{
int i;
int max=0;
for(i=1;i<n;i++)
{
if(num[i]>num[max])
max = i;
}
pMaxPos+=max;
return max;
}
int FindMin(int num[], int n, int *pMinPos)
{
int i;
int min=0;
for(i=1;i<n;i++)
{
if(num[i]<num[min])
min = i;
}
pMinPos+=min;
return min;
}
题目内容:
编程打印具有如下形式的杨辉三角形,其中输出数据的行数n从键盘输入,并且n<=10。
程序运行结果示例1:
Input n (n<=10):
5↙
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include
#define N 10
int main()
{
int n, i, j, a[N][N];
printf("Input n (n<=10):\n");
scanf("%d", &n);
for (i=0; i<n; i++)
{
a[i][0] = 1;
a[i][i] = 1;
}
for (i=2; i<n; i++)
{
for (j=1; j<=i-1; j++)
{
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
for (i=0; i<n; i++)
{
for (j=0; j<=i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
return 0;
}
题目内容:
从键盘输入一个句子(假设字符数小于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!
#include
#include
#define N 100
int Inverse(char str1[], char str2[][N]);
int main()
{
char a[N], b[N][N], punctuation, word, i;
int n;
printf("Input a sentence:");
gets(a);
n = strlen(a);
punctuation = a[n-1];
a[n-1]='\0';
word = Inverse(a, b);
for(i=word;i>0;i--)
printf("%s ", b[i]);
printf("%s%c\n", b[0], punctuation);
}
int Inverse(char str1[], char str2[][N])
{
int i, j, k;
j = 0;
for(i=0;str1[i]!='\0';i++)
{
for(k=0;str1[i]!=' '&&str1[i]!='\0';i++,k++)
{
str2[j][k]=str1[i];
}
if(str1[i]=='\0')
break;
str2[j][k]='\0';
j++;
}
return j;
}
题目内容:
从键盘输入一串字符(假设字符数少于8个),以回车表示输入结束,编程将其中的数字部分转换为整型数并以整型的形式输出。
函数原型为 int Myatoi(char str[]);
其中,形参数组str[]对应用户输入的字符串,函数返回值为转换后的整型数。
解题思路的关键是:1)判断字符串中的字符是否是数字字符;2)如何将数字字符转换为其对应的数字值;3)如何将每一个转换后的数字值加起来形成一个整型数。
程序运行结果示例1:
Input a string:7hg09y↙
709
程序运行结果示例2:
Input a string:9w2k7m0↙
9270
程序运行结果示例3:
Input a string:happy↙
0
#include
#include
#define N 100
int Myatoi(char str[]);
int main()
{
char ch[N];
int number;
printf("Input a string:");
scanf("%7s", ch);
number = Myatoi(ch);
printf("%d\n", number);
}
int Myatoi(char str[])
{
int i, num[N], j=0, sum=0, k, x;
for(i=0;str[i]!='\0';i++)
{
switch(str[i])
{
case '1': num[j]=1; j++; break;
case '2': num[j]=2; j++; break;
case '3': num[j]=3; j++; break;
case '4': num[j]=4; j++; break;
case '5': num[j]=5; j++; break;
case '6': num[j]=6; j++; break;
case '7': num[j]=7; j++; break;
case '8': num[j]=8; j++; break;
case '9': num[j]=9; j++; break;
case '0': num[j]=0; j++; break;
default: break;
}
}
num[j]='\0';
x=1;
for(k=j-1;k>=0;k--)
{
sum+=num[k]*x;
x*=10;
}
return sum;
}
题目内容:
用字符数组作函数参数,编程实现在从键盘输入的字符串(假设长度小于80)中查找与指定的子串,并输出该子串在字符串中首次出现的位置,如果该字符不存在,则输出"Not found!"。
函数原型:int SearchString(char s[], char d[])
函数功能:在字符数组s中查找子串d,返回d在s中首次出现的位置,若找不到,则返回-1。
程序运行结果示例1:
Input a string:How are you!↙
Input another string:are↙
Searching results:5
程序运行结果示例2:
Input a string:hello↙
Input another string:are↙
Not found!
程序运行结果示例3:
Input a string:You are a student.↙
Input another string:you↙
Not found!
#include
#include
#define N 100
int SearchString(char s[], char d[])
{
int i, n, x, j, flag;
x = strlen(d);
n=strlen(s);
for(i=0;i<n;i++)
{
if(s[i]==d[0])
{
flag=1;
for(j=1;j<x;j++)
{
if(s[i+j]!=d[j])
{
flag=0;
break;
}
}
if(flag==1)
return i+1;
}
}
return -1;
}
int main()
{
char str1[N],str2[N];
int x;
printf("Input a string:");
gets(str1);
printf("Input another string:");
gets(str2);
x = SearchString(str1, str2);
if(x==-1)
printf( "Not found!\n");
else
printf("Searching results:%d\n", x);
}
题目内容:
输入一串字符(字符数小于80),以回车表示输入结束,编程计算并输出这串字符中连续重复次数最多的字符和重复次数。如果重复次数最多的字符有两个,则输出最后出现的那一个。
已知函数原型:
//函数功能:统计字符串中连续重复次数最多的字符及其重复的次数
//函数参数:str指向待统计的字符串,指针形参tag返回重复字符最后出现的下标位置
//函数返回值:返回字符重复的次数
int CountRepeatStr(char str[], int *tag);
求解思路:设置一个计数器,遍历字符串中的所有字符,若str[i] == str[i+1],则计数器加1,同时判断计数器的值是否大于记录的最大重复次数max,若大于,则用计数器的值更新max,并记录该字符最后出现的位置i+1.若str[i] != str[i+1],则计数器重新初始化为1。遍历结束时,函数返回max的值。
程序运行结果示例1:
Input a string:
2344455555↙
5:5
程序运行结果示例2:
Input a string:
sgf222257↙
2:4
#include
#include
#define N 100
int CountRepeatStr(char str[], int *tag);
int main()
{
char str[N];
int *tag, a;
tag=&a;
int x;
printf("Input a string:\n");
gets(str);
x = CountRepeatStr(str, tag);
printf("%c:%d\n", str[a], x);
}
int CountRepeatStr(char str[], int *tag)
{
int i, n, max=1, count=1;
n = strlen(str);
for(i=0;i<n;i++)
{
if(str[i]==str[i+1])
{
count++;
*tag=i+1;
}
else
count=1;
if(max<count)
{
max=count;
}
}
return max;
}
题目内容:
凯撒密码是罗马扩张时期朱利斯•凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令,其原理很简单,就是通过将字母表中的字母移动一定位置而实现加密。例如,每个字母按字母表顺序向后移3位,如a加密后变成d,b加密后变成e,……x加密后变成a,y加密后变成b,z加密后变成c。请编写一个程序,将用户从键盘输入的文本字符串(只包含a~z的字符且长度小于100)进行加密后输出。
函数原型:void Caesar(char c[]);
函数功能:计算凯撒密码
程序的运行结果示例1:
Input a string:baidu↙
edlgx
程序的运行结果示例2:
Input a string:xyz↙
abc
#include
#include
#define N 100
void Caesar(char c[])
{
int n, i;
n = strlen(c);
for(i=0;i<n;i++)
{
c[i]+=3;
}
}
int main()
{
char c[N];
printf("Input a string:");
gets(c);
Caesar(c);
puts(c);
return 0;
}