编程题考核题库50题及答案Word文档下载推荐.docx

上传人:b****6 文档编号:21754521 上传时间:2023-02-01 格式:DOCX 页数:44 大小:32.69KB
下载 相关 举报
编程题考核题库50题及答案Word文档下载推荐.docx_第1页
第1页 / 共44页
编程题考核题库50题及答案Word文档下载推荐.docx_第2页
第2页 / 共44页
编程题考核题库50题及答案Word文档下载推荐.docx_第3页
第3页 / 共44页
编程题考核题库50题及答案Word文档下载推荐.docx_第4页
第4页 / 共44页
编程题考核题库50题及答案Word文档下载推荐.docx_第5页
第5页 / 共44页
点击查看更多>>
下载资源
资源描述

编程题考核题库50题及答案Word文档下载推荐.docx

《编程题考核题库50题及答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《编程题考核题库50题及答案Word文档下载推荐.docx(44页珍藏版)》请在冰豆网上搜索。

编程题考核题库50题及答案Word文档下载推荐.docx

i++)

 {printf("

%12ld%12ld"

f1,f2);

   if(i%2==0)printf("

\n"

/*控制输出,每行四个*/

   f1=f1+f2;

/*前两个月加起来赋值给第三个月*/

   f2=f1+f2;

 }

【程序2】 

判断101-200之间有多少个素数,并输出所有素数。

程序分析:

判断素数的方法:

用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

publicclasslianxi02{

intcount=0;

for(inti=101;

200;

i+=2){

booleanb=false;

for(intj=2;

j<

=Math.sqrt(i);

j++)

{

if(i%j==0){b=false;

break;

}

else 

{b=true;

if(b==true){count++;

System.out.println(i);

System.out.println("

素数个数是:

+count);

#include<

stdio.h>

intn,i;

for(n=101;

n<

=200;

n++){

for(i=2;

i*i<

=n;

if(n%i==0)break;

if(i*i>

n)printf("

%d"

n);

#include"

math.h"

 intm,i,k,h=0,leap=1;

 printf("

 for(m=101;

m<

=200;

m++)

  {k=sqrt(m+1);

   for(i=2;

=k;

     if(m%i==0)

      {leap=0;

break;

   if(leap){printf("

%-4d"

m);

h++;

        if(h%10==0)

        printf("

       }

   leap=1;

  }

\nThetotalis%d"

h);

【程序3】 

打印出所有的"

水仙花数"

,所谓"

是指一个三位数,其各位数字立方和等于该数本身。

例如:

153是一个"

,因为153=1的三次方+5的三次方+3的三次方。

publicclasslianxi03{

intb1,b2,b3;

for(intm=101;

m<

1000;

m++){

b3=m/100;

b2=m%100/10;

b1=m% 

10;

if((b3*b3*b3+b2*b2*b2+b1*b1*b1)==m){

System.out.println(m+"

是一个水仙花数"

inta,b,c;

for(inti=100;

=999;

a=i/100;

b=i%100/10;

c=i%10;

if(a*a*a+b*b*b+c*c*c==i)

cout<

<

endl;

return0;

inti,j,k,n;

'

waterflower'

numberis:

"

 for(n=100;

n<

n++)

 {

  i=n/100;

/*分解出百位*/

  j=n/10%10;

/*分解出十位*/

  k=n%10;

/*分解出个位*/

  if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)

   {

   printf("

%-5d"

   }

【程序4】 

将一个正整数分解质因数。

输入90,打印出90=2*3*3*5。

对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:

(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。

(2)如果n<

k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。

(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

importjava.util.*;

public 

class 

lianxi04{

publicstaticvoidmain(String[]args){

Scanners=newScanner(System.in);

System.out.print("

请键入一个正整数:

int 

=s.nextInt();

intk=2;

System.out.print(n+"

="

);

while(k<

=n){

if(k==n){System.out.println(n);

elseif(n%k==0){System.out.print(k+"

*"

n=n/k;

k++;

main(){

intn;

intk=0;

for(i=2;

if(k==0&

&

n%i==0)

cout<

n="

i;

n/=i;

//找到第一个质因数,按格式输出

while(n%i==0)

*'

k=1;

}//分解到n不再是i的倍数是为止

system("

pause"

\npleaseinputanumber:

scanf("

%d"

&

n);

%d="

for(i=2;

  while(n!

=i)

  {

   if(n%i==0)

   {printf("

%d*"

i);

    n=n/i;

   else

    break;

【程序5】 

利用条件运算符的嵌套来完成此题:

学习成绩>

=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

publicclasslianxi05{

intx;

chargrade;

请输入一个成绩:

x=s.nextInt();

grade=x>

=90?

'

A'

:

x>

=60?

B'

C'

;

等级为:

+grade);

}

【程序6】 

输入两个正整数m和n,求其最大公约数和最小公倍数。

/**在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。

*/

lianxi06 

{

int 

a,b,m;

Scanners=newScanner(System.in);

System.out.print("

键入一个整数:

a=s.nextInt();

再键入一个整数:

b=s.nextInt();

deffcd=newdeff();

m=cd.deff(a,b);

intn=a*b/m;

最大公约数:

+m);

最小公倍数:

+n);

classdeff{

publicintdeff(intx,inty){

intt;

if(x<

y){

t=x;

x=y;

y=t;

while(y!

=0){

if(x==y)returnx;

else{

intk=x%y;

y=k;

returnx;

intp,r,n,m,temp;

任意输入两个正整数m,n:

m>

in(m>

n)

temp=n;

n=m;

m=temp;

p=n*m;

while(m!

=0)

r=n%m;

m=r;

最大公约数="

最小公倍数="

p/n<

 inta,b,num1,num2,temp;

pleaseinputtwonumbers:

 scanf("

%d,%d"

num1,&

num2);

 if(num1 {temp=num1;

  num1=num2;

 

  num2=temp;

a=num1;

b=num2;

while(b!

=0)/*利用辗除法,直到b为0为止*/

  temp=a%b;

  a=b;

  b=temp;

gongyueshu:

%d\n"

a);

gongbeishu:

num1*num2/a);

【程序7】 

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

publicclasslianxi07{

intdigital=0;

intcharacter=0;

intother=0;

intblank=0;

char[]ch=null;

Scannersc=newScanner(System.in);

Strings=sc.nextLine();

ch=s.toCharArray();

for(inti=0;

ch.length;

if(ch>

='

0'

&

ch<

9'

){

digital++;

}elseif((ch>

a'

z'

)||ch>

Z'

character++;

}elseif(ch=='

blank++;

}else{

other++;

数字个数:

+digital);

英文字母个数:

+character);

空格个数:

+blank);

其他字符个数:

+other);

string>

voidmain()

unsignedintl=0,m=0,n=0,k=0;

charc;

string:

while((c=getchar())!

=EOF)

if((c>

='

)&

(c<

)||(c>

))

l++;

else

m++;

if(c=='

n++;

k++;

thesumofnumber 

thesumofblankspace 

thesumofletler 

l<

thesumofothers 

k<

stdio.h"

{charc;

 intletters=0,space=0,digit=0,others=0;

pleaseinputsomecharacters\n"

 while((c=getchar())!

\n'

 if(c>

c<

||c>

  letters++;

 elseif(c=='

  space++;

   elseif(c>

       digit++;

     else

       others++;

allinall:

char=%dspace=%ddigit=%dothers=%d\n"

letters,

space,digit,others);

【程序8】 

求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。

例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

publicclasslianxi08{

longa,b=0,sum=0;

System.out.print("

输入数字a的值:

a=s.nextInt();

输入相加的项数:

intn=s.nextInt();

inti=0;

while(i<

n){

b=b+a;

sum=sum+b;

a=a*10;

++i;

System.out.println(sum);

 inta,n,count=1;

 longintsn=0,tn=0;

 printf("

pleaseinputaandn\n"

 scanf("

a,&

a=%d,n=%d\n"

a,n);

 while(count<

=n)

 {

  tn=tn+a;

  sn=sn+tn;

  a=a*10;

  ++count;

 }

a+aa+...=%ld\n"

sn);

 inta,n,count=1;

 longintsn=0,tn=0;

 while(count<

  tn=tn+a;

  sn=sn+tn;

  a=a*10;

  ++count;

【程序9】 

一个数如果恰好等于它的因子之和,这个数就称为"

完数"

例如6=1+2+3.编程 

找出1000以内的所有完数。

publicclasslianxi09{

1到1000的完数有:

for(inti=1;

intt=0;

for(intj=1;

=i/2;

j++){

if(i%j==0){

t=t+j;

if(t==i){

System.out.print(i+"

iostream.h>

classtest{

private:

intj;

public:

test(){

i=0;

j=0;

voidInput(intnum1,intnum2)[

i=num1;

j=num2;

voidouput(){

for(inta=i;

a<

j;

a++){

inttem=0;

for(intb=i;

b<

a;

b++){

if(a%b==0){tem+=b;

if(a==tem){cout<

tem<

voidmain(){

inta,b;

testt;

please2number!

endl

a>

b;

t.Input(a,b);

t.Output();

staticintk[10];

inti,j,n,s;

for(j=2;

j<

j++)

 n=-1;

 s=j;

  for(i=1;

i

   if((j%i)==0)

   { n++;

    s=s-i;

    k[n]=i;

 if(s==0)

%disawanshu"

j);

}}

【程序10】 

一球从100米高度自由落下,每次落地后反跳回原高度的一半;

再落下,求它在 

第10次落地时,共经过多少米?

第10次反弹多高?

publicclasslianxi10{

doubleh=100,s=100;

10;

s=s+h;

h=h/2;

System.out.p

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1