输入5个国名,编程找出并输出按字典顺序排在最前面的国名。
提示:所谓字典顺序就是将字符串按由小到大的顺序排列,因此找出按字典顺序排在最前面的国名指的就是最小的字符串。
程序的运行结果示例:
Input five countries’ names:
America↙
China↙
Japan↙
England↙
Sweden↙
The minimum is:America
#include
#include
#define N 20
int main()
{
char country[5][N], temp[N];
int i, j;
printf("Input five countries' names:\n");
for(i=0; i<5;i++)
gets(country[i]);
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(country[i],country[j])>0)
{
strcpy(temp,country[i]);
strcpy(country[i],country[j]);
strcpy(country[j],temp);
}
}
}
printf("The minimum is:%s\n", country[0]);
return 0;
}
题目内容:
某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,参考前面章节的“学生成绩管理系统V1.0”,用一维数组和函数指针作函数参数编程实现如下菜单驱动的学生成绩管理系统:
(1)录入每个学生的学号和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按成绩由低到高排出名次表;
(5)按学号由小到大排出成绩表;
(6)按学号查询学生排名及其考试成绩;
(7)按优秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比;
(8)输出每个学生的学号、考试成绩。
要求程序运行后显示的菜单如下:
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please enter your choice:
然后,根据用户输入的选项执行相应的操作。
程序运行结果示例:
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
1↙
Input student’s ID 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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
4↙
Sort in ascending order by score:
11003002 48
11003004 65
11003003 75
11003001 87
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
5↙
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
6↙
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
7↙
<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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
8↙
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
9↙
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
0↙
End of program!
其实这题里面我没有太理解应该把函数指针用在那里,就随便加了一个。mooc上显示结果错误,但我没找出来。
#include
#define N 30
void Fun(long number[], float score[], int n, void (*f)(long*, float*, int));
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);
void Ascending(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 score\n");
printf("5.Sort in ascending order by number\n");
printf("6.Search by number\n");
printf("7.Statistic analysis\n");
printf("8.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");
Fun(number, score, n, Descending);
break;
}
case 4:
{
printf("Sort in ascending order by score:\n");
Fun(number, score, n, Ascending);
break;
}
case 5:
{
printf("Sort in ascending order by number:\n");
AscendingNum(number, score, n);
break;
}
case 6:
{
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 7:
{
ResultAnalysis(number, score, n);
break;
}
case 8:
{
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;
}
}
}
}
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);
}
void Ascending(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;
}
}
}
}
void Fun(long number[], float score[], int n, void (*f)(long*, float*, int))
{
int i;
(*f)(number, score, n);
for(i=0;i<n;i++)
printf("%ld\t%.0f\n", number[i], score[i]);
}
题目内容:
用指针数组保存表示每个月份的英文单词以及“Illegal month”的首地址,然后编程实现:从键盘任意输入一个数字表示月份值n,程序输出该月份的英文表示,若n不在1~12之间,则输出“Illegal month”。
程序的运行结果示例1:
Input month number:
3↙
month 3 is March
程序的运行结果示例2:
Input month number:
12↙
month 12 is December
程序的运行结果示例3:
Input month number:
14↙
Illegal month
#include
#define N 30
int main( )
{
char *pmonth[]={"January","February","March","April","May","June","July","August",
"September","October","November","December","Illegal month"};
int month;
printf("Input month number:\n");
scanf("%d", &month);
if(month>12||month<1)
printf("%s\n", pmonth[12]);
else
printf("month %d is %s\n", month, pmonth[month-1]);
return 0;
}
题目内容:
从键盘任意输入m个学生n门课程的成绩,然后计算每个学生各门课的总分sum和平均分aver。下面程序存在极为隐蔽的错误,请分析错误的原因,并修改程序,同时按照给出的程序运行示例检查修改后的程序。
#include
#define STUD 30 //最多可能的学生人数
#define COURSE 5 //最多可能的考试科目数
void Total(int *score, int sum[], float aver[], int m, int n);
void Print(int *score, int sum[], float aver[], int m, int n);
int main(void)
{
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
printf("Enter the total number of students and courses:\n");
scanf("%d%d",&m,&n);
printf("Enter score:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", &score[i][j]);
}
}
Total(*score, sum, aver, m, n);
Print(*score, sum, aver, m, n);
return 0;
}
void Total(int *score, int sum[], float aver[], int m, int n)
{
int i, j;
for (i=0; i<m; i++)
{
sum[i] = 0;
for (j=0; j<n; j++)
{
sum[i] = sum[i] + *(score + i * n + j);
}
aver[i] = (float) sum[i] / n;
}
}
void Print(int *score, int sum[], float aver[], int m, int n)
{
int i, j;
printf("Result:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
printf("%4d\t", *(score + i * n + j));
}
printf("%5d\t%6.1f\n", sum[i], aver[i]);
}
}
程序运行结果示例:
Enter the total number of students and courses:
2 3↙
Enter score:
90↙
95↙
97↙
82↙
73↙
69↙
Result:
90 95 97 282 94.0
82 73 69 224 74.7
更改后:
#include
#define STUD 30 //最多可能的学生人数
#define COURSE 5 //最多可能的考试科目数
void Total(int (*score)[COURSE], int sum[], float aver[], int m, int n);
void Print(int (*score)[COURSE], int sum[], float aver[], int m, int n);
int main()
{
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
printf("Enter the total number of students and courses:\n");
scanf("%d%d",&m,&n);
printf("Enter score:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", &score[i][j]);
}
}
Total(score, sum, aver, m, n);
Print(score, sum, aver, m, n);
return 0;
}
void Total(int (*score)[COURSE], int sum[], float aver[], int m, int n)
{
int i, j;
for (i=0; i<m; i++)
{
sum[i] = 0;
for (j=0; j<n; j++)
{
sum[i] = sum[i] + *(*(score+i)+j);
}
aver[i] = (float) sum[i] / n;
}
}
void Print(int (*score)[COURSE], int sum[], float aver[], int m, int n)
{
int i, j;
printf("Result:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
printf("%4d", score[i][j]);
}
printf("%5d%6.1f\n", sum[i], aver[i]);
}
}
题目内容:
下面主函数调用函数SortString()按奥运会参赛国国名在字典中的顺序对其入场次序进行排序,目前程序存在错误,请修改正确,并按照给出的程序运行示例检查修改后的程序。
#include
#include
#define M 150 /* 最多的字符串个数 */
#define N 10 /* 字符串最大长度 */
void SortString(char *ptr[], int n);
int main()
{
int i, n;
char *pStr[M];
printf("How many countries?\n");
scanf("%d",&n);
getchar(); /* 读走输入缓冲区中的回车符 */
printf("Input their names:\n");
for (i=0; i<n; i++)
{
gets(pStr[i]); /* 输入n个字符串 */
}
SortString(pStr, n); /* 字符串按字典顺序排序 */
printf("Sorted results:\n");
for (i=0; i<n; i++)
{
puts(pStr[i]); /* 输出排序后的n个字符串 */
}
return 0;
}
void SortString(char *ptr[], int n)
{
int i, j;
char *temp = NULL;
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
if (strcmp(ptr[j], ptr[i]) < 0)
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}
}
程序运行结果示例:
How many countries?
5↙
Input their names:
China↙
French↙
America↙
Russia↙
German↙
Sorted results:
America
China
French
German
Russia
更改后:
#include
#include
#define M 150 /* 最多的字符串个数 */
#define N 10 /* 字符串最大长度 */
void SortString(char (*ptr)[N], int n);
int main()
{
int i, n;
char country[M][N];
printf("How many countries?\n");
scanf("%d",&n);
getchar(); /* 读走输入缓冲区中的回车符 */
printf("Input their names:\n");
for (i=0; i<n; i++)
{
gets(country[i]); /* 输入n个字符串 */
}
SortString(country, n); /* 字符串按字典顺序排序 */
printf("Sorted results:\n");
for (i=0; i<n; i++)
{
puts(country[i]); /* 输出排序后的n个字符串 */
}
return 0;
}
void SortString(char (*ptr)[N], int n)
{
int i, j;
char temp[N];
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
if (strcmp(*(ptr+j),*(ptr+i)) < 0)
{
strcpy(temp,*(ptr+i));
strcpy(*(ptr+i),*(ptr+j));
strcpy(*(ptr+j),temp);
}
}
}
}
题目内容:
按如下函数原型编程从键盘输入一个m行n列的二维数组,然后计算数组中元素的最大值及其所在的行列下标值。其中,m和n的值由用户键盘输入。已知m和n的值都不超过10。
void InputArray(int *p, int m, int n);
int FindMax(int *p, int m, int n, int *pRow, int *pCol);//函数返回最大值,pRow和pCol分别返回最大值所在的行列下标
例如,程序的1次运行结果如下:
Input n:
3,4↙
Input 3*4 array:
1 2 3 4↙
5 6 7 8↙
9 0 -1 -2↙
max=9,row=2,col=0
比较需要注意的是作为参数的指针,应该是列指针,所以要注意相应的实参。
#include
#include
#define M 10 /* 最多的字符串个数 */
#define N 10 /* 字符串最大长度 */
void InputArray(int *p, int m, int n);
int FindMax(int *p, int m, int n, int *pRow, int *pCol);//函数返回最大值,pRow和pCol分别返回最大值所在的行列下标
int main()
{
int number[M][N];
int m, n, max;
int row, column;
printf("Input m,n:\n");
scanf("%d,%d", &m,&n);
printf("Input %d*%d array:\n", m, n);
InputArray(*number, m, n);
max=FindMax(*number, m, n, &row, &column);
printf("max=%d,row=%d,col=%d\n", max, row, column);
return 0;
}
void InputArray(int *p, int m, int n)
{
int i, j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d", p+i*n+j);
}
}
int FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
int max, i, j;
max=*p;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(max<*(p+i*n+j))
{
max=*(p+i*n+j);
*pRow=i;
*pCol=j;
}
}
}
return max;
}
题目内容:
采用冒泡法进行升序排序法的基本原理是:对数组中的n个数执行n-1遍检查操作,在每一遍执行时,对数组中剩余的尚未排好序的元素进行如下操作:对相邻的两个元素进行比较,若排在后面的数小于排在前面的数,则交换其位置,这样每一遍操作中都将参与比较的数中的最大的数沉到数组的底部,经过n-1遍操作后就将全部n个数按从小到大的顺序排好序了。程序的某次运行结果如下:
Input n:10↙
Input 10 numbers:2 9 3 4 0 6 8 7 5 1↙
Sorting results: 0 1 2 3 4 5 6 7 8 9
#include
#include
#define M 20
int main()
{
int n, number[M], i, j, temp;
printf("Input n:");
scanf("%d", &n);
printf("Input %d numbers:", n);
for(i=0;i<n;i++)
scanf("%d", &number[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(number[j]>number[j+1])
{
temp = number[j];
number[j]=number[j+1];
number[j+1]=temp;
}
}
}
printf("Sorting results:");
for(i=0;i<n;i++)
printf("%4d", number[i]);
return 0;
}
题目内容:
在字符串中删除与某字符相同的字符,要求用字符数组作函数参数。
程序运行结果示例:
Input a string:
hello, my friend!↙
Input a character:
!↙
Results:hello, my friend
#include
#include
#define M 50
void CancelRepeat(char str[], char ch, char result[]);
int main()
{
char str[M], ch, result[M];
printf("Input a string:\n");
gets(str);
printf("Input a character:\n");
ch=getchar();
CancelRepeat(str, ch, result);
printf("Results:%s\n", result);
return 0;
}
void CancelRepeat(char str[], char ch, char result[])
{
long n;
int i, j=0;
n = strlen(str);
for(i=0;i<n;i++)
{
if(str[i]!=ch)
{
result[j]=str[i];
j++;
}
}
result[j]='\0';
}
题目内容:
从键盘输入10个正整数,求出最大数,最小数,以及他们的最大公约数。要求用数组实现。
程序运行结果示例1:
Input 10 numbers:
15 23 56 87 94 105 78 19 22 43↙
maxNum=105
minNum=15
15
程序运行结果示例2:
Input 10 numbers:
33 1 2 9 8 7 5 4 0 10↙
maxNum=33
minNum=0
#include
#include
#define M 10
int GetGCD(int a, int b);
int main()
{
int number[M], i, max, min, GCD;
printf("Input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d", &number[i]);
max = number[0];
min = number[0];
for(i=1;i<10;i++)
{
if(max<number[i])
max=number[i];
if(min>number[i])
min = number[i];
}
printf("maxNum=%d\n", max);
printf("minNum=%d\n", min);
if(max>0&&min>0)
{
GCD=GetGCD(max, min);
printf("%d", GCD);
}
return 0;
}
int GetGCD(int a, int b)
{
int r;
do{
r = a%b;
a = b;
b = r;
}while(r!=0);
return a;
}
题目内容:
已知两个不同长度的降序排列的数列(假设序列的长度都不超过5),请编程将其合并为一个数列,使合并后的数列仍保持降序排列。
【提示】假设两个降序排列的数列分别保存在数组a和数组b中,用一个循环,从前往后依次比较保存在数组a和数组b中的两个剩余序列里的第一个数,将其中的较大者存到数组c中,当一个较短的序列存完后,再将较长的序列剩余的部分依次保存到数组c的末尾。假设两个序列的长度分别是m和n,在比较剩余序列的循环中,用i和j分别记录两个序列待比较的数组元素位置,循环结束后,若i小于m,则说明数组a中的数有剩余,将数组a中剩余的数存到数组c的末尾即可;若j小于n,则说明数组b中的数有剩余,将数组b中剩余的数存到数组c的末尾即可。在第一个循环中,用k记录往数组c中存了多少个数,在第二个循环中,就从k这个位置开始继续存储较长序列中剩余的数。
函数原型:void Merge(int a[], int b[], int c[], int m, int n);
函数功能:将两个长度分别为m和n、降序排列的子序列a和b合并后放到数组c中
程序运行结果示例1:
Input m,n:3,2↙
Input array a:5 3 1↙
Input array b:4 2↙
5 4 3 2 1
程序运行结果示例2:
Input m,n:3,3↙
Input array a:31 27 -5↙
Input array b:98 30 -7↙
98 31 30 27 -5 -7
#include
#include
#define M 5
#define N 10
void Merge(int a[], int b[], int c[], int m, int n);
int main()
{
int m, n, i;
int a[M], b[M], c[N];
printf("Input m,n:");
scanf("%d,%d", &m, &n);
printf("Input array a:");
for(i=0;i<m;i++)
scanf("%d", &a[i]);
printf("Input array b:");
for(i=0;i<n;i++)
scanf("%d", &b[i]);
Merge(a, b, c, m, n);
for(i=0;i<m+n;i++)
printf("%4d", c[i]);
return 0;
}
void Merge(int a[], int b[], int c[], int m, int n)
{
int i=0, j=0, k=0;
do{
if(a[i]>b[j])
{
c[k]=a[i];
k++;
i++;
}
else{
c[k]=b[j];
k++;
j++;
}
}while(i<m&&j<n);
if(i==m)
{
for(;j<n;j++)
{
c[k]=b[j];
k++;
}
}
else
for(;i<m;i++)
{
c[k]=a[i];
k++;
}
}
题目内容:
为了能在下一次跑步比赛中有好的发挥,小白在一条山路上开始了她的跑步训练。她希望能在每次训练中跑得尽可能远,不过她也知道农场中的一条规定:女孩子独自进山的时间不得超过M秒(1 <= M <= 10,000,000)。假设整条山路划分成T个长度相同的路段(1 <= T <= 100,000),并且小白用si表示第i个路段的路况,用u、f、d这3个字母分别表示第i个路段是上坡、平地、下坡。小白跑完一段上坡路的耗时是U秒(1 <= U <= 100),跑完一段平地的耗时是F秒(1 <= F <= 100),跑完一段下坡路的耗时是D秒(1 <= D <= 100)。注意,沿山路原路返回时,原本是上坡的路段变成了下坡路段,原本是下坡的路段变成了上坡路段。小白想知道,在能按时返回农场的前提下,她最多能在这条山路上跑多少个路段。请你编程帮助她计算。
函数原型:long Fun(long M, long T, long U, long F, long D, char str[]);
函数功能:计算在限时M秒内T个路段的情况下,最多往返可跑的路段数。
参数:M,T,U,F,D分别代表限时、路段数,以及上坡、平地、下坡的耗时
数组str保存整条山路的路段状况
返回值:最多可跑的路段数
程序运行结果示例1:
Input M,T,U,F,D:13 5 3 2 1↙
Input conditions of road:ufudf↙
num=3
程序运行结果示例2:
Input M,T,U,F,D:150 8 18 10 5↙
Input conditions of road:fuffdfud↙
num=7
#include
#include
#define N 30
long Fun(long M, long T, long U, long F, long D, char str[]);
int main()
{
long M, T, U, F, D, num;
char str[N];
printf("Input M,T,U,F,D:");
scanf("%ld%ld%ld%ld%ld", &M, &T, &U, &F, &D);
printf("Input conditions of road:");
scanf("%s", str);
num = Fun(M,T,U,F,D,str);
printf( "num=%ld\n", num);
return 0;
}
long Fun(long M, long T, long U, long F, long D, char str[])
{
long num=0, time=0;
int i=0;
do{
switch(str[i])
{
case 'u': time+=U+D; break;
case 'f': time+=F+F; break;
case 'd': time+=D+U; break;
}
num++;
i++;
}while(time<M && i<T);
if(time<=M)
return num;
else
return num-1;
}
题目内容:
输入n个整数(n从键盘输入,假设n的值不超过100),按奇偶数分成两组并输出。输出两行,第一行为所有奇数,第二行为所有偶数,保持数据的相对顺序与输入顺序相同。
函数原型如下所示:
void Seperate(int a[], int n); //数组a[]存放用户输入的n个整数
解题思路:用两个循环分别输出奇数和偶数,在输出第一个数时用"%d"格式字符,在输出其余数时用",%d"格式字符,用标志变量记录和判断是否是第一个奇数或偶数。
程序运行结果示例1:
Input n:7↙
Input numbers:5 9 47 82 0 6 7↙
5,9,47,7
82,0,6
程序运行结果示例2:
Input n:8↙
Input numbers:-2 3 5 0 23 62 79 83↙
3,5,23,79,83
-2,0,62
#include
#include
#define N 30
void Seperate(int a[], int n);
int main()
{
int i, a[N], n;
printf("Input n:");
scanf("%d", &n);
printf("Input numbers:");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
Seperate(a, n);
return 0;
}
void Seperate(int a[], int n)
{
int i, flag=0;
for(i=0;i<n;i++)
{
if(a[i]%2==1)
{
if(flag==0)
{
printf("%d", a[i]);
flag++;
}
else
printf(",%d", a[i]);
}
}
printf("\n");
flag=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
if(flag==0)
{
printf("%d", a[i]);
flag++;
}
else
printf(",%d", a[i]);
}
}
}
题目内容:从键盘输入两个长度小于80的字符串A和B,且A的长度大于B的长度,编程判断B是不是A的子串,如果是,则输出”Yes”,否则输出”No”。这里所谓的该串的子串是指字符串中任意多个连续的字符组成的子序列。
函数原型:int IsSubString(char a[], char b[]);
函数功能:判断b是否是a的子串,是则返回1,否则返回0
程序运行结果示例1:
Input the first string: Abcdefghijk123↙
Input the second string: 123↙
Yes
程序运行结果示例2:
Input the first string: abefsfl↙
Input the second string: befs↙
Yes
程序运行结果示例3:
Input the first string: aAbde↙
Input the second string: abc↙
No
这题我做得有点乱…其实也不是很确定
#include
#include
#define N 80
int IsSubString(char a[], char b[]);
int main()
{
char a[N], b[N];
int x;
printf( "Input the first string:");
gets(a);
printf("Input the second string:");
gets(b);
x = IsSubString(a, b);
if(x==1)
printf("Yes\n");
else
printf( "No\n");
return 0;
}
int IsSubString(char a[], char b[])
{
int i, j, flag=0;
long x, y;
x = strlen(a);
y = strlen(b);
for(i=0;i<x;i++)
{
if(a[i]==b[0])
{
for(j=0;j<y;j++)
{
if(a[i+j]==b[j])
flag = 1;
else{
flag=0;
break;
}
}
if(flag==1)
return 1;
}
}
return 0;
}
题目内容:
任意输入英文的星期几,通过查找如图所示的星期表,输出其对应的数字,若查到表尾,仍未找到,则输出错误提示信息。
提示:用一个二维字符数组weekDay来存放如图所示的星期表的内容(字符串)。输入待查找的字符串,然后在星期表中顺序查找与输入字符串相匹配的字符串。找到的字符串在星期表数组中的第一维下标(行号)即为题目所求。
程序运行结果示例1:
Please enter a string:
Friday↙
Friday is 5
程序运行结果示例2:
Please enter a string:
Fruday↙
Not found!
#include
#include
#define N 20
int main()
{
char weekDay[][N]={"Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday"};
char day[N];
int i;
printf("Please enter a string:\n");
gets(day);
for(i=0;i<7;i++)
{
if(strcmp(weekDay[i], day)==0)
{
printf("%s is %d\n", weekDay[i],i);
exit(0);
}
}
printf("Not found!\n");
return 0;
}