当前位置:   article > 正文

C程序设计综合练习--基础编程2:_任意输入10个正整数,找出其中的素数,并将这些素数按由小到大排序。要求:判断一个

任意输入10个正整数,找出其中的素数,并将这些素数按由小到大排序。要求:判断一个

 1.题目(Description):

输入一个正整数,编写函数prime,判断其是否为素数。

int prime(int n)

{                         }

输入(Input):

一个整数

输出(Output):

如果是素数,则输出 "yes"

如果非素数,则输出 "no"

提示(Hint):

程序的后缀代码已经给出,请在提交作业时注释或者去掉后缀代码。

示例1(Sample):

输入(Input):

2

输出(Output):

yes

示例2(Sample):

输入(Input):

4

输出(Output):

no

后缀代码:
//StudybarCommentBegin
int main()
{
	int i,num,bool;
	scanf("%d",&num);
	bool=prime(num);
	if(bool==1)  printf("%s","yes");
	else  printf("%s","no");
	return 0;
}
//StudybarCommentEnd

2.

题目(Description):

任意输入10个正整数,找出其中的素数,并将这些素数按由小到大排序。要求:判断一个数是否为素数用函数实现;排序用函数实现。在主函数输入10个正整数到数组,输出排序结果。

输入(Input):

输入10个正整数

输出(Output):

排好序的素数

示例1(Sample):

输入(Input):

10 9 21 7 5 4 11 35 2 100

输出(Output):

2 5 7 11

示例2(Sample):

输入(Input):

10 9 21 100 200 4 6 8 22 12 30

输出(Output):

No prime


3.

题目(Description):

统计给定整数M和N区间内素数个数并对它们求和。

输入(Input):

在一行中给出2个正整数M和N (1<=M<=N<=500)

输出(Output):

在一行中顺序输出M和N区间内素数的个数以及它们的和,数字间以空格分隔

示例(Sample):

输入(Input):

10 31

输出(Output):

7 143


4.

题目(Description):

输入一个不多于5位的正整数,要求:编写程序逆序输出各位数字

输入(Input):

一个正整数

输出(Output):

逆序的各位数字(第一位为非0数字)

提示(Hint):

程序的前缀代码已经给出,请在提交作业时注释或者去掉前缀代码。

示例(Sample):

输入(Input):

12345

输出(Output):

54321

前缀代码:
//StudybarCommentBegin
#include<stdio.h>
int main( ) 
{
	int palindrome(int x);
	int x,pnum; 
	scanf("%d",&x); 
	pnum=palindrome(x);
	printf("%d",pnum);
	return 0;
}

//StudybarCommentEnd
5.

杨辉三角,又称贾宪三角形,帕斯卡三角形,是二项式系数在三角形中的一种几何排列。简单的说,就是两个未知数和的幂次方运算后的系数问题,比如:(x+y)2=x2+2xy+y2,这样系数就是1、2、1,是杨辉三角的第三行。

杨辉三角最本质的特征是,它的两条斜边都是由数字1组成的,而其余的数则是等于它肩上的两个数之和。

杨辉三角的排列性质如下:

要求:根据输入的n(n不大于10)值,编写程序输出n行杨辉三角形。

输入(Input):

杨辉三角形的行数n

输出(Output):

n行杨辉三角形,且每个数占5列且右对齐

提示(Hint):

程序的前缀代码已经给出,请在提交作业时注释或者去掉前缀代码。

示例(Sample):

输入(Input):

10

输出(Output):

前缀代码:
//StudybarCommentBegin
#include<stdio.h>
int main() 
{
	void pascals_triangle(int n);
	int n; 
	scanf("%d",&n);
	pascals_triangle(n);
	return 0;
}

//StudybarCommentEnd

6.

题目(Description):

编写函数numbers,通过指针操作,统计一个字符串(不多于20个字符)中数字字符的个数。

int numbers(char *p)

{                                   }

输入(Input):

一个字符串(不多于20个字符)

输出(Output):

一个字符串中数字字符的个数

提示(Hint):

程序的前缀代码已经给出,请在提交作业时注释或者去掉前缀代码。

示例(Sample):

输入(Input):

0p9o8i7u66y

输出(Output):

There are 6 numbers in "0p9o8i7u66y"

前缀代码:
//StudybarCommentBegin
#include<stdio.h>
#include<string.h>
int numbers(char *p);
#define SIZE 20
int main()
{
        char str[SIZE];
        int num;
        gets(str);
        num=numbers(str);
        printf("There are %d numbers in \"%s\"",num,str);
        return 0;
}
//StudybarCommentEnd
7.

题目(Description):

利用字符串指针,不使用系统函数strcmp,实现其功能。通过主函数main输入2个字符串(在main函数的后缀中已经给出),在被调用函数stringcompare(char  *str1,char *str2)中比较2个字符串大小,并进行输出。

输入(Input):

字符串1

字符串2

输出(Output):

(1)当输入的第一个字符串大于第二个字符串输出bigger

(2)第一个字符串小于第二个字符串输出smaller

(3)第一个字符串与第二个字符串相等,输出equal

提示(Hint):

程序的后缀代码已经给出,请在提交作业时注释或者去掉后缀代码。

示例1(Sample):

输入(Input):

baby

BABY

输出(Output):

bigger

示例2(Sample):

输入(Input):

boy

boya

输出(Output):

smaller

示例3(Sample):

输入(Input):

hello

hello

输出(Output):

equal

后缀代码:
//StudybarCommentBegin
int main(int argc, char* argv[])
 {
        char str1[100],str2[100];
        gets(str1);gets(str2);
        void stringcompare(char * str1,char * str2);
        stringcompare(str1,str2);
        return 0;
 }
//StudybarCommentEnd
8.

题目(Description):

输入两个整数,编写函数swap,交换着两个整数。

void swap(int *p1, int *p2)

{                                            }

输入(Input):

两个整数

输出(Output):

交换后的两个整数

提示(Hint):

程序的后缀代码已经给出,请在提交作业时注释或者去掉后缀代码。

示例(Sample):

输入(Input):

9 5

输出(Output):

5 9

后缀代码:
//StudybarCommentBegin
int main( )
{
        int a,b;
        scanf("%d%d",&a,&b);
        swap(&a,&b);
        printf("%d %d",a,b);
        return 0;
}
//StudybarCommentEnd
9.

题目(Description):

有一个字符串,包含n个字符,写一个函数,将此字符串中从第m个字符开始的全部字符复制为另一个字符串。要求:主函数在前缀中已给出,只写copystr函数,注意函数参数的类型。

void copystr(char *,char *,int)

{                                                   }

输入(Input):

一个字符串

整数

输出(Output):

第m个字符开始的字符序列

示例1(Sample):

输入(Input):

12345abcde

6

输出(Output):

abcde

示例2(Sample):

输入(Input):

12345abcde

12

输出(Output):

input error!

前缀代码:
//StudybarCommentBegin
#include <stdio.h>
#include <string.h>
int main()
{
	void copystr(char *,char *,int);
        int m;
        char str1[20],str2[20];
        gets(str1);
        scanf("%d",&m);
        if(strlen(str1)<m)  printf("input error!");
        else
        {
  	        copystr(str1,str2,m);
                printf("%s",str2);
        }
        return 0;
}

//StudybarCommentEnd
10.

题目(Description):

某个公司采用公用电话传递数据,数据是四位的正整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。要求: 编写函数fun,加密输入的四位数。

void fun(int b[],int a)

{                                   }

输入(Input):

一个四位正整数

输出(Output):

加密后的四位数

提示(Hint):

程序的前缀代码已经给出,请在提交作业时注释或者去掉前缀代码。

示例(Sample):

输入(Input):

1234

输出(Output):

9876

前缀代码:
//StudybarCommentBegin
#include<stdio.h>
int main( ) 
{
	void fun(int *p,int a);
	int num,i,a[4]; 
	scanf("%d",&num); 
	fun(a,num);
	for(i=3;i>=0;i--)  printf("%d",a[i]); 
	return 0;
}

//StudybarCommentEnd
11、

题目(Description):

假设每班人数最多不超过40人,具体人数由键盘输入,用一维数组和指针变量作函数参数,编程打印某班一门课成绩的最高分和学号。函数原型如下:

函数功能:    计算最高分及最高分学生的学号

函数参数:    整型数组score,存放学生的成绩
                         长整型数组num,存放学生的学号
                         整型变量n,存放学生人数
                         长整型指针变量pMaxNum,存放求出来的最高分学生的学号

函数返回值:整型,最高分

int FindMax(int score[], long num[], int n, long *pMaxNum);

{                                                                                                          }

输入(Input):

学生人数n

学号1 成绩1

学号2 成绩2

…………

学号n 成绩n

输出(Output):

最高分和学号

提示(Hint):

程序的前缀代码已经给出,请在提交作业时注释或者去掉前缀代码。

不考虑两人同时具有最高分的情况。

示例(Sample):

输入(Input):

5

1001 88

1002 90

1003 56

1004 100

1005 88

输出(Output):

maxScore = 100, maxNum =1004

前缀代码:
//StudybarCommentBegin
#include <stdio.h>
#define ARR_SIZE 40
int FindMax(int score[], long num[], int n, long *pMaxNum);
int main()
{
	int score[ARR_SIZE], maxScore, n, i;
        long num[ARR_SIZE], maxNum;
        scanf("%d",&n);								// 从键盘输入学生人数n
        for(i=0; i<n; i++)								// 分别以长整型和整型格式输入学生学号和成绩
                scanf("%ld%d",&num[i],&score[i]);
        maxScore=FindMax(score, num, n, &maxNum);  	// 计算最高分及学生学号
        printf("maxScore = %d, maxNum =%ld",maxScore,maxNum);
        return 0;
}

//StudybarCommentEnd

12.

题目(Description):

编写函数 void revstr(char *),通过指针传递,将键盘上输入的字符串反向输出。

void revstr(char *)

{                            }

输入(Input):

一个字符串

输出(Output):

反向字符串

提示(Hint):

程序的前缀代码已经给出,请在提交作业时注释或者去掉前缀代码。

示例(Sample):

输入(Input):

12345 abcdefBC

输出(Output):

CBfedcba 54321

前缀代码:
//StudybarCommentBegin
#include <stdio.h>
#include <string.h>
void revstr(char *);
int main()
{
	char a[100];
  	gets(a);
  	revstr(a);
  	puts(a);
	return 0;
}

//StudybarCommentEnd
13

题目(Description):

从键盘上输入两个字符串(以回车键作为字符串结束标志),找出其中没有同时出现在两个字符串中的字符。如果所有的字符都同时出现,则输出ok。

输入(Input):

两个字符串

输出(Output):

(1)输出没有同时出现在两个字符串中的字符。输出的顺序是:没有在第二个字符串中出现的第一个字符串中的字符在前。

(2)若第一个字符串中所有的字符都在第二个字符串中出现,并且第二个字符串中所有的字符都在第一个字符串中出现则输出ok。

示例1(Sample):

输入(Input):

abca

bcd

输出(Output):

ad

示例2(Sample):

输入(Input):

abc

def

输出(Output):

abcdef

示例3(Sample):

输入(Input):

abca

abc

输出(Outtput ):

ok


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/328776?site=
推荐阅读
相关标签
  

闽ICP备14008679号