浙江大学 PAT 编程初级2
2016-09-03 16:13
267 查看
以下是《基础题目集合》的下半部分,4-7到4-12的题目和源代码,如果大家有好的思路和想法,欢迎赐教。
/*
4-7 统计某类完全平方数 (20分)
本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144、676等。
*/
#include <stdio.h>
#include <math.h>
int IsTheNumber ( const int N );
int main()
{
int n1, n2, i,cnt;
scanf("%d %d", &n1, &n2);
cnt = 0;
for ( i=n1; i<=n2; i++ ) {
if ( IsTheNumber(i) )
// printf("%d\n",i);
cnt++;
}
printf("cnt = %d\n", cnt);
return 0;
}
/* 你的代码将被嵌在这里 */
//看清楚题目:至少两位数字相同
int IsTheNumber(const int N){
int num[10]={0},i,j;
int n=(int)sqrt(N);
int NNum=n*n;
int flag=0;
if(NNum==N){
while(NNum>0){
for(i=0;i<=9;i++){
if(NNum%10==i)
num[i]++;
if(num[i]==2)
flag=1;
}
NNum=NNum/10;
}
}
//不要在if的外面判断num[i]==2,不同的编译器,程序执行的次序的问题,不同的编译器,编译的结果可能不同
return flag;
}
/*
4-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;
}
/* 你的代码将被嵌在这里 */
int Factorial(const int N){
if(N<=12 && N>0){
int i;
int sum=1;
for(i=1;i<=N;i++)
sum*=i;
return sum;
}
else if(N==0)
return 1;
else
return 0;
}
/*
4-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;
}
/* 你的代码将被嵌在这里 */
int Count_Digit(const int N,const int D){
int a[10]={0};
int s;
if(N<0)
s=-N;
else if(N==0){
if(D==0)
return 1;
else
return 0;
}
else
s=N;
while(s!=0){
a[s%10]++;
s=s/10;
}
int i; //这个判断很重要,因为,题目。。。。。 额
for(i=0;i<10;i++)
{
if(D==i)
return a[i];
}
return 0;
}
/*
4-10 阶乘计算升级版 (20分)
本题要求实现一个打印非负整数阶乘的函数。
函数接口定义:
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;
}
/* 你的代码将被嵌在这里 */
void Print_Factorial(const int N){
if(N>0 && N<=12){
int i;
long int M=1;
for(i=2;i<=N;i++){
M=M*i;
}
printf("%ld\n",M);
}
else if(N>12 && N<=1000){
int Num[3001]={0};
Num[0]=1;
int i,j,k=1,temp,n=0;
for(i=2;i<=N;i++){
for(j=0;j<k;j++){
temp=Num[j]*i+n;
Num[j]=temp%10;
n=temp/10;
}
while(n!=0){
Num[k]=n%10;
k++;
n=n/10;
}
}
for(i=k-1;i>=0;i--){
printf("%d",Num[i]);
}
}
else if(N==0){
printf("1\n");
}
else{
printf("Invalid input\n");
}
}
/*
4-11 求自定类型元素序列的中位数 (25分)
本题要求实现一个函数,求N个集合元素A[]的中位数,即序列中第\lfloor N/2 +1\rfloor⌊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));
for(i=0;i<N;i++)
printf("%.2f " ,A[i]);
return 0;
}
/* 你的代码将被嵌在这里 */
void quickSort(ElementType A[],int l ,int r){
if(l<r){
int i=l,j=r;
ElementType x=A[l];
while(i<j){
while(i<j && A[j]>=x)
j--;
if(i<j)
A[i++]=A[j];
while(i<j && A[i]<x)
i++;
if(i<j)
A[j--]=A[i];
}
A[i]=x;
quickSort(A,l,i-1);
quickSort(A,i+1,r);
}
}
ElementType Median(ElementType A[],int N){
quickSort(A,0,N-1);
return A[N/2];
}
/*
4-12 判断奇偶性 (10分)
本题要求实现判断给定整数奇偶性的函数。
函数接口定义:
int even( int n );
其中n是用户传入的整型参数。当n为偶数时,函数返回1;n为奇数时返回0。注意:0是偶数。
*/
8ea5
#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;
}
/* 你的代码将被嵌在这里 */
int even(int n){
if(n%2==0)
return 1;
else
return 0;
}
/*
4-7 统计某类完全平方数 (20分)
本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144、676等。
*/
#include <stdio.h>
#include <math.h>
int IsTheNumber ( const int N );
int main()
{
int n1, n2, i,cnt;
scanf("%d %d", &n1, &n2);
cnt = 0;
for ( i=n1; i<=n2; i++ ) {
if ( IsTheNumber(i) )
// printf("%d\n",i);
cnt++;
}
printf("cnt = %d\n", cnt);
return 0;
}
/* 你的代码将被嵌在这里 */
//看清楚题目:至少两位数字相同
int IsTheNumber(const int N){
int num[10]={0},i,j;
int n=(int)sqrt(N);
int NNum=n*n;
int flag=0;
if(NNum==N){
while(NNum>0){
for(i=0;i<=9;i++){
if(NNum%10==i)
num[i]++;
if(num[i]==2)
flag=1;
}
NNum=NNum/10;
}
}
//不要在if的外面判断num[i]==2,不同的编译器,程序执行的次序的问题,不同的编译器,编译的结果可能不同
return flag;
}
/*
4-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;
}
/* 你的代码将被嵌在这里 */
int Factorial(const int N){
if(N<=12 && N>0){
int i;
int sum=1;
for(i=1;i<=N;i++)
sum*=i;
return sum;
}
else if(N==0)
return 1;
else
return 0;
}
/*
4-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;
}
/* 你的代码将被嵌在这里 */
int Count_Digit(const int N,const int D){
int a[10]={0};
int s;
if(N<0)
s=-N;
else if(N==0){
if(D==0)
return 1;
else
return 0;
}
else
s=N;
while(s!=0){
a[s%10]++;
s=s/10;
}
int i; //这个判断很重要,因为,题目。。。。。 额
for(i=0;i<10;i++)
{
if(D==i)
return a[i];
}
return 0;
}
/*
4-10 阶乘计算升级版 (20分)
本题要求实现一个打印非负整数阶乘的函数。
函数接口定义:
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;
}
/* 你的代码将被嵌在这里 */
void Print_Factorial(const int N){
if(N>0 && N<=12){
int i;
long int M=1;
for(i=2;i<=N;i++){
M=M*i;
}
printf("%ld\n",M);
}
else if(N>12 && N<=1000){
int Num[3001]={0};
Num[0]=1;
int i,j,k=1,temp,n=0;
for(i=2;i<=N;i++){
for(j=0;j<k;j++){
temp=Num[j]*i+n;
Num[j]=temp%10;
n=temp/10;
}
while(n!=0){
Num[k]=n%10;
k++;
n=n/10;
}
}
for(i=k-1;i>=0;i--){
printf("%d",Num[i]);
}
}
else if(N==0){
printf("1\n");
}
else{
printf("Invalid input\n");
}
}
/*
4-11 求自定类型元素序列的中位数 (25分)
本题要求实现一个函数,求N个集合元素A[]的中位数,即序列中第\lfloor N/2 +1\rfloor⌊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));
for(i=0;i<N;i++)
printf("%.2f " ,A[i]);
return 0;
}
/* 你的代码将被嵌在这里 */
void quickSort(ElementType A[],int l ,int r){
if(l<r){
int i=l,j=r;
ElementType x=A[l];
while(i<j){
while(i<j && A[j]>=x)
j--;
if(i<j)
A[i++]=A[j];
while(i<j && A[i]<x)
i++;
if(i<j)
A[j--]=A[i];
}
A[i]=x;
quickSort(A,l,i-1);
quickSort(A,i+1,r);
}
}
ElementType Median(ElementType A[],int N){
quickSort(A,0,N-1);
return A[N/2];
}
/*
4-12 判断奇偶性 (10分)
本题要求实现判断给定整数奇偶性的函数。
函数接口定义:
int even( int n );
其中n是用户传入的整型参数。当n为偶数时,函数返回1;n为奇数时返回0。注意:0是偶数。
*/
8ea5
#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;
}
/* 你的代码将被嵌在这里 */
int even(int n){
if(n%2==0)
return 1;
else
return 0;
}
相关文章推荐
- 网络路由技术及运用2
- 获取免费的Camtasia Studio 2使用授权
- 选择模式 - XSL教程 - 2
- JS Common 2 之比较常用到的函数第1/3页
- 支持Ajax跨域访问ASP.NET Web Api 2(Cors)的示例教程
- [Struts 2系列] Struts 2发展历程
- 让Sublime Text 2支持GB2312和GBK编码
- 支持Ajax跨域访问ASP.NET Web Api 2(Cors)的示例教程
- 【python】编程语言入门经典100例--1
- 野人过河问题
- Video 5 Part 2 计算子网
- 诺基亚N8
- Sublime Text 2 快捷键用法大全
- 四个人过桥分别用时1、2、5、10,每次最多两个人过桥,只有一把手电,求时间最短
- 2
- CentOS /usr/libexec/gconf-sanity-check-2 exited with status 256问题解决
- 关于Linux实时性能的研究及测试 2
- init 0-6 (启动级别:init 0,1,2,3,4,5,6)
- programming language
- (2)开始构建:一个基本的应用--ZF2官方教程译文