练习7-2 求最大值及其下标(20 分)
本题要求编写程序,找出给定的n个数中的最大值及其对应的最小下标(下标从0开始)。
## 输入格式:
输入在第一行中给出一个正整数$n(1\lt n\le10)$。第二行输入n个整数,用空格分开。
## 输出格式:
在一行中输出最大值及最大值的最小下标,中间用一个空格分开。
## 输入样例:
6
2 8 10 1 9 10
## 输出样例:
10 2
本题要求实现一个求整数的逆序数的简单函数。
| 1 | int reverse( int number ); | 
其中函数reverse须返回用户传入的整型number的逆序数。
| 1 | #include <stdio.h> | 
| 1 | -12340 | 
| 1 | -4321 | 
本题要求实现一个判断素数的简单函数,并利用该函数验证哥德巴赫猜想:任何一个不小于6的偶数均可表示为两个奇素数之和。素数就是只能被1和自身整除的正整数。注意:1不是素数,2是素数。
| 1 | int prime( int p ); | 
其中函数prime当用户传入参数p为素数时返回1,否则返回0;函数Goldbach按照格式“n=p+q”输出n的素数分解,其中p≤q均为素数。又因为这样的分解不唯一(例如24可以分解为5+19,还可以分解为7+17),要求必须输出所有解中p最小的解。
| 1 | #include <stdio.h> | 
| 1 | 89 100 | 
| 1 | 89 is a prime number | 
本题要求实现一个计算Fibonacci数的简单函数,并利用其实现另一个函数,输出两正整数$m$和$n(0\lt m \leq n \leq 10000)$之间的所有Fibonacci数。所谓Fibonacci数列就是满足任一项数字是前两项的和(最开始两项均定义为1)的数列。
| 1 | int fib( int n ); | 
其中函数fib须返回第n项Fibonacci数;函数PrintFN要在一行中输出给定范围[m, n]内的所有Fibonacci数,相邻数字间有一个空格,行末不得有多余空格。如果给定区间内没有Fibonacci数,则输出一行“No Fibonacci number”。
| 1 | #include <stdio.h> | 
| 1 | 20 100 7 | 
| 1 | fib(7) = 13 | 
| 1 | 2000 2500 8 | 
| 1 | fib(8) = 21 | 
本题要求实现一个计算整数因子和的简单函数,并利用其实现另一个函数,输出两正整数$m$$和n(0<m≤n≤10000)$之间的所有完数。所谓完数就是该数恰好等于除自身外的因子之和。例如:$6=1+2+3$,其中1、2、3为6的因子。
| 1 | int factorsum( int number ); | 
其中函数factorsum须返回int number的因子和;函数PrintPN要逐行输出给定范围[m, n]内每个完数的因子累加形式的分解式,每个完数占一行,格式为“完数 = 因子1 + 因子2 + … + 因子k”,其中完数和因子均按递增顺序给出。如果给定区间内没有完数,则输出一行“No perfect number”。
| 1 | #include <stdio.h> | 
| 1 | 1 30 | 
| 1 | 1 is a perfect number | 
| 1 | 7 25 | 
| 1 | No perfect number | 
给定两个均不超过9的正整数a和n,要求编写函数求$a+aa+aaa++⋯+aa⋯a$(n个a)之和。
| 1 | int fn( int a, int n ); | 
其中函数fn须返回的是n个a组成的数字;SumA返回要求的和。
| 1 | #include <stdio.h> | 
| 1 | 2 3 | 
| 1 | fn(2, 3) = 222 | 
本题要求实现一个函数,统计给定字符串中英文字母、空格或回车、数字字符和其他字符的个数。
| 1 | void StringCount( char s[] ); | 
其中 char s[] 是用户传入的字符串。函数StringCount须在一行内按照
| 1 | letter = 英文字母个数, blank = 空格或回车个数, digit = 数字字符个数, other = 其他字符个数 | 
的格式输出。
| 1 | #include <stdio.h> | 
| 1 | aZ & | 
| 1 | letter = 4, blank = 3, digit = 2, other = 1 | 
本题要求实现一个函数,用下列公式求cos(x)的近似值,精确到最后一项的绝对值小于e:
$cos(x)=\frac{x^0}{0!}−\frac{x^2}{2!}+\frac{x^4}{4!}−\frac{x^6}{6!}+\cdots$
| 1 | double funcos( double e, double x ); | 
其中用户传入的参数为误差上限e和自变量x;函数funcos应返回用给定公式计算出来、并且满足误差要求的cos(x)的近似值。输入输出均在双精度范围内。
| 1 | #include <stdio.h> | 
| 1 | 0.01 -3.14 | 
| 1 | cos(-3.14) = -0.999899 | 
水仙花数是指一个N位正整数$(N \geq 3)$,它的每个位上的数字的N次幂之和等于它本身。例如:$153=13+53+33$。 本题要求编写两个函数,一个判断给定整数是否水仙花数,另一个按从小到大的顺序打印出给定区间(m,n)内所有的水仙花数。
| 1 | int narcissistic( int number ); | 
函数narcissistic判断number是否为水仙花数,是则返回1,否则返回0。
函数PrintN则打印开区间(m, n)内所有的水仙花数,每个数字占一行。题目保证100≤m≤n≤10000。
| 1 | #include <stdio.h> | 
| 1 | 153 400 | 
| 1 | 153 is a narcissistic number | 
本题要求实现一个统计整数中指定数字的个数的简单函数。
| 1 | int CountDigit( int number, int digit ); | 
其中number是不超过长整型的整数,digit为[0, 9]区间内的整数。函数CountDigit应返回number中digit出现的次数。
| 1 | #include <stdio.h> | 
| 1 | -21252 2 | 
| 1 | Number of digit 2 in -21252: 3 | 
Update your browser to view this website correctly. Update my browser now