c试题及答案(Cquestionsandanswers)

上传人:缘*** 文档编号:333288668 上传时间:2022-09-01 格式:PDF 页数:40 大小:1.97MB
返回 下载 相关 举报
c试题及答案(Cquestionsandanswers)_第1页
第1页 / 共40页
c试题及答案(Cquestionsandanswers)_第2页
第2页 / 共40页
c试题及答案(Cquestionsandanswers)_第3页
第3页 / 共40页
c试题及答案(Cquestionsandanswers)_第4页
第4页 / 共40页
c试题及答案(Cquestionsandanswers)_第5页
第5页 / 共40页
点击查看更多>>
资源描述

《c试题及答案(Cquestionsandanswers)》由会员分享,可在线阅读,更多相关《c试题及答案(Cquestionsandanswers)(40页珍藏版)》请在金锄头文库上搜索。

1、c+试题及答案(C+questions and answers)1.Title:polynomial 1!+2!+3!+.+15!S um of valuesS tored in variable outftinclude Void,main()(Int,out,I,J,factorial;Out=0;For(I=1;I 15;i+)Factorial=1;For(J=1;J I;j+)factorial=factorial*j;Out=out+factorial;Cout out endl;2.Title:seek al 1 prime numbers between 1 and 200,a

2、nd put theprime number into the variable sumt t i n c lu d e In t p r i m e (i n t i)(In t j;Fo r (J=2;J I;j+)If (.(1%J)r e t u r n 0;)If (J=I)r e t u r n j;Els e r e t u r n 0;)V o i d,m a i n ()(In t,I,s u m;S u m =0;Fo r (1=1;I 200;i+)S u m =s u m +p r i m e (I);Co u t s u m e n d l,;)3.T i t le:

3、u s i n g w h i le lo o p p r o g r a m m i n g,n a t u r a l n u m b e r s b e t w e e n1 t o 100 o d d s q u a r e sAn d s u m.t t i n c lu d e V o i d,m a i n ()(In t,I,s u m;1=1;S u m =0;W h i le (I =100)S u m =(1*1);1=1+2;Co u t s u m e n d l;4.T i t le:t o d e t e r m i n e w h e t h e r a n u

4、 m b e r 23437 i s a p r i m e n u m b e rT h e p a r a m e t e r f la g,f la g i s 1,s t a n d s f o r p r i m e n u m b e r,0r e p r e s e n t s n o)t t i n c lu d e In t p r i m e (i n t i)(In t j;Fo r (J=2;J I;j+)(If (.(1%J)r e t u r n 0;)If (J=I)r e t u r n j;Els e r e t u r n 0;Void,main()Int,

5、I,flag;I=23437;Flag=prime(I)i;Cout flag endl,z;5.T itle:known a number m (=252),ask you sum of numbersttinclude ftinclude Using namespace std;Void,main()(String s;Int,len,sum,i;S =252”;L e n =s.s i z e ();S u m =0;Fo r (I=0;I le n;i+)S u m =s u m +s i -O ;/t a ke a d i g i t a l c h a r a c t e r,i

6、t i s t h ed i g i t a l va lu e m i n u s t h e c h a r a c t e r 0)Co u t,“t h e s u m o f y o u r n u m b e r s i s:s u m e n d l;6.T i t le:t h e o d d n u m b e r b e t w e e n 1-100 c u m u la t i ve o r d e r i n t ot h e N,u n t i l i t s a n d s o o nAt o r g r e a t e r t h a n 200f t i n

7、c lu d e V o i d,m a i n ()(In t,I,s u m;I=1;S um=0:While(I =200 break);Cout sum endl;7.Title:two numbers to calculate x with the division method”,the largest convention ynumber#includeVoid,main()(Int,a,B,numl,num2,temp;Cout“enter two integers.:;CIN numl;CIN num2;If(numl num2)Temp=numl;Numl=num2;Num

8、2=temp;A=numl;B=num2;While(b=0)Temp=a%b;A=b;B=temp;Cout is the common denom inator:z/a e n d l;8.T itle:three known a,B,C,in ascending order in a,b,C and output.#includeVoid,main()(Int,a,B,C,temp;Cout“enter three integers.CIN a;CIN b;CIN c;If(a b)(Temp=a;A =b;B =temp;If(a C)Temp=a;A=c;C=temp;)If(b C

9、)(Temp=b;B =c;C=temp;)上 n iCout a,B ,C,e n d 1;9.Title:known n,calculate the sum of all primes within n sum.t t i n c lu d e In t p r i m e (i n t i)(In t j;Fo r (J=2;J I;j+)If (.(1%J)r e t u r n 0;)If (J=I)r e t u r n j;Els e r e t u r n 0;)In t,m a i n ()(In t,I,N,s u m;S u m =0;Co u t “e n t e r

10、n:,e n d l;CIN n;Fo r (1=1;I n;i +)s u m =s u m +b o n u s (i).n/以内素数和为:,e n d l.)10.题目打印水仙花数及个数(水仙花数是三位数,每.位数字的立方和等于这个数本身示例见图1).i n i o s t r e a m.h#vo i d m a i n ()(i n t a,b,c,d;f o r a =100;a =999;+)b =a /100.c =(a *b);d =a%10.i f (a =b*b*b*c*c+d*d*d)i e n d l.)11.题 目 找 出 1-200之间的完数及个数(完数是一个数的

11、因.子之和是这个数本身.例如6=1+2 +3).i n i o s t r e a m,h#i n e n d f e w (i n t x)(i n s u m,is u m =0.d =1.y =x;w h i le (x (j)if(y%j=0)sum+=j;j+.if(y=sum)return 1;else return 0.void main()(int i,n;n=0.1 -200 之间的完数 cost endl.for(i=1,i=200;i+)if(endfew(i)=1)i i endl.n+.i 共 n 个e n d l.)12.题目:编写一个c +函数m a x,输入参数

12、为3个实数,输出为其中的最大数.i n i o s t r e a m,h#i n t m a x (i n t x,i n t y,i n t y)(i n t t e m p.t e m p =x;i f (y t e m p t e m p =y;f (z)=z t e m p t e m p.r e t u r n t i m e.vo i d m a i n ()i n t a,b,c,n;“c o s t 输入 3 个整数:.-b;-c;n =m a x (a,b,c).“c o s t”最大的数为:n e n d l.13.题目:编写一个 函 数(i n t n,i n t i

13、s s u s h u),判 断 n是否是素数.i n i o s t r e a m,h#i n i s s u s h u (i n t n)(i n t i;f o r (i =2,j n;i +)i f (!(n)(j)r e t u r n 0;i f (i =n)r e t u r n 1;e ls e r e t u r n 0.vo i d m a i n ()(i n t x;“c o s t”输入 1 个整数:x.i f (i s s u s h u (x)=1)x c o s t 是素数“e n d l.x w i ll c o s t 不是素数 e n d l.)14.

14、题目:从键盘中分别给两个整形变量x和y赋值为20和10,通过编程使它们的值交换,然后输出交换以后的x和y的值.i n i o s t r e a m,h#vo i d m a i n ()i n t x,y,a n d t i m e.“c o s t 输入 x:x.c o s t 输入 y:.t e m p =x;x =y;y =t e m p;i z,x =,x e n d l.e n d l.)15.题目:利 用 s w i t c h 语句将输入的百分制成绩转换成相应的等级.成绩等级优 秀 90%8 9 8 0良好中 等 70.及 格 60.不及格5 9 0i n i o s t r

15、e a m,h#vo i d m a i n ()(i n t x;“c o s t 输入成绩:x.(x 1/10)b o x 10:“c o s t 优 秀 e n d l.b r e a k;e n d l.b o x 9:c o s t 优 秀 e n d l.b r e a k;b o x 8:“c o s t 良好 e n d l.b r e a k;b o x 7:“c o s t 中 等 e n d l.b r e a k;b o x 6:“c o s t 及格 z,e n d l.b r e a k;d e f a u lt:“c o s t 不及格 e n d l.16.题目

16、:输入一个自然数n u m,将该数的每一位数字按反序输出d i g i t a l.例如:输入123456,输出654321(要求输入123456).i n i o s t r e a m,h#vo i d m a i n ()(t h e lo n g s,mc h a r t e m p.i n t le n,i.c o s t 输入一串数字:.m =0.w h i le (s)m =m *10+%10.s =s /10;im 反序:e n d l.17.Title:use the loop statement to output the 99 multiplicationtable.ftinclude iostream,h”Void,main()(Int,I,j;For(I=1;I 10;i+)For(J=1;J I;j+)Cout J *I =I*j;If(J=I)continue;Cout endl;Cout endl;18.Title:use the for cycle and break statements:from thekeyboard input ifDo a p

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号