6-8 简单阶乘计算 (10 分)
本题要求实现一个计算非负整数阶乘的简单函数。
函数接口定义:
int Factorial( const int N );
其中N是用户传入的参数,其值不超过12。如果N是非负整数,则该函数必须返回N的阶乘,否则返回0。
裁判测试程序样例:
#include <stdio.h>
int Factorial( const int N );
int main()
{
int N, NF;
scanf("%d", &N);
NF = Factorial(N);
if (NF) printf("%d! = %d\n", N, NF);
else printf("Invalid input\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
5
输出样例:
5! = 120
答案
int Factorial( const int N ){
if(N<0){
return 0;
}
int i=1,ji=1;
for(;i<=N;i++){
ji*=i;
}
return ji;
}
6-9 统计个位数字 (15 分)
这个题负数和零的情况一定要注意
本题要求实现一个函数,可统计任一整数中某个位数出现的次数。例如-21252中,2出现了3次,则该函数应该返回3。
函数接口定义:
int Count_Digit ( const int N, const int D );
其中N和D都是用户传入的参数。N的值不超过int的范围;D是[0, 9]区间内的个位数。函数须返回N中D出现的次数。
裁判测试程序样例:
#include <stdio.h>
int Count_Digit ( const int N, const int D );
int main()
{
int N, D;
scanf("%d %d", &N, &D);
printf("%d\n", Count_Digit(N, D));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
-21252 2
输出样例:
3
答案
int Count_Digit ( const int N, const int D ){
int m=N,t;
int count=0;
if(N==0){
if(D==0){
return 1;
}
else{
return 0;
}
}
if(m<0){
m=-m;
}
while(m>0){
t=m%10;
if(t==D){
count++;
}
m=m/10;
}
return count;
}
6-10 阶乘计算升级版 (20 分)
此题犯错处有2个
1. 当N过大时,long int 也无法满足解的要求
2. 用数组存放结果时,要注意1, 进位 在进行最高位计算完毕后,进位可能是多位数;2, 结果在数组中存放的是0~k-1位且高位在后,低位在前
本题要求实现一个打印非负整数阶乘的函数。
函数接口定义:
void Print_Factorial ( const int N );
其中N是用户传入的参数,其值不超过1000。如果N是非负整数,则该函数必须在一行中打印出N!的值,否则打印“Invalid input”。
裁判测试程序样例:
#include <stdio.h>
void Print_Factorial ( const int N );
int main()
{
int N;
scanf("%d", &N);
Print_Factorial(N);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
15
输出样例:
1307674368000
答案
void Print_Factorial ( const int N ){
if(N<0){
printf("Invalid input");
}else{
long int sum=1;
if(N<100){
int i=1;
for(;i<=N;i++){
sum*=i;
}
printf("%ld",sum);
}
else{
int sum[3000]={0};
int temp,i,j;
int k=1,n=0;
sum[0]=1;
for(i=2;i<=N;i++){
for(j=0;j<k;j++){
temp=sum[j]*i+n;
sum[j]=temp%10;
n=temp/10;
if(n!=0 &&j==k-1){
k++; //处理最后进位为两位
}
}
}
for(i=k-1;i>=0;i--){
printf("%d",sum[i]);
}
}
}
}
6-11 求自定类型元素序列的中位数 (25 分)
快速排序时间上还是不行,使用堆排序
本题要求实现一个函数,求N个集合元素A[]的中位数,即序列中第⌊N/2+1⌋大的元素。其中集合元素的类型为自定义的ElementType。
函数接口定义:
ElementType Median( ElementType A[], int N );
其中给定集合元素存放在数组A[]中,正整数N是数组元素个数。该函数须返回N个A[]元素的中位数,其值也必须是ElementType类型。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
typedef float ElementType;
ElementType Median( ElementType A[], int N );
int main ()
{
ElementType A[MAXN];
int N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &A[i]);
printf("%.2f\n", Median(A, N));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
12.3 34 -5
输出样例:
12.30
void HeapAdjust(ElementType array[], int i, int nlength)
{
int nChild;
ElementType nTemp;
for (; 2 * i + 1 < nlength; i = nChild)
{
nChild = 2 * i + 1;
if (nChild < nlength - 1 && array[nChild + 1] > array[nChild])
nChild++;
if (array[i] < array[nChild])
{
nTemp = array[i];
array[i] = array[nChild];
array[nChild] = nTemp;
}
else
break;
}
}
void Heapsort(ElementType array[], int length)
{
int i;
ElementType temp;
for (i = length / 2 - 1; i >= 0; i--)
HeapAdjust(array, i, length);
for (i = length - 1; i >= 0; i--)
{
temp = array[0];
array[0] = array[i];
array[i] = temp;
HeapAdjust(array, 0, i);
}
}
ElementType Median(ElementType A[], int N)
{
Heapsort(A, N);
return A[N / 2];
}
6-12 判断奇偶性 (10 分)
本题要求实现判断给定整数奇偶性的函数。
函数接口定义:
int even( int n );
其中n是用户传入的整型参数。当n为偶数时,函数返回1;n为奇数时返回0。注意:0是偶数。
裁判测试程序样例:
#include <stdio.h>
int even( int n );
int main()
{
int n;
scanf("%d", &n);
if (even(n))
printf("%d is even.\n", n);
else
printf("%d is odd.\n", n);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例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-13 折半查找 (15 分)
给一个严格递增数列,函数int Search_Bin(SSTable T, KeyType k)用来二分地查找k在数列中的位置。
函数接口定义:
int Search_Bin(SSTable T, KeyType k)
其中T是有序表,k是查找的值。
裁判测试程序样例:
#include <iostream>
using namespace std;
#define MAXSIZE 50
typedef int KeyType;
typedef struct
{ KeyType key;
} ElemType;
typedef struct
{ ElemType *R;
int length;
} SSTable;
void Create(SSTable &T)
{ int i;
T.R=new ElemType[MAXSIZE+1];
cin>>T.length;
for(i=1;i<=T.length;i++)
cin>>T.R[i].key;
}
int Search_Bin(SSTable T, KeyType k);
int main ()
{ SSTable T; KeyType k;
Create(T);
cin>>k;
int pos=Search_Bin(T,k);
if(pos==0) cout<<"NOT FOUND"<<endl;
else cout<<pos<<endl;
return 0;
}
/* 请在这里填写答案 */
输入格式:
第一行输入一个整数n,表示有序表的元素个数,接下来一行n个数字,依次为表内元素值。 然后输入一个要查找的值。
输出格式:
输出这个值在表内的位置,如果没有找到,输出"NOT FOUND"。
输入样例:
5
1 3 5 7 9
7
输出样例:
4
输入样例:
5
1 3 5 7 9
10
输出样例:
NOT FOUND
答案
int Search_Bin(SSTable T, KeyType k){
int L=T.length,H=0;
int m;
while (L>=H){
m=(L+H)/2;
if (T.R[m].key==k)
return m;
else if(T.R[m].key<k)
H=m+1;
else
L=m-1;
}
return 0;
}