2023年高级语言程序设计C++平时作业答案.docx
一、分析程序,写输出结果# i ncl u d e< i o stream. h>#include<math. h >v o id mainOint m, k, i ;f o r ( m=l; m<=10; m+=2 ) k = m/3;for( i = 2 ; i<=k; i+ )i f ( m%i ) cou t « m «)运营结果:7 9#incl u de<i o stream. h>void fun ():void ma i n()int i;fo r (i= 1 ; i< 5 ; i+) f u n();cou t<<endl;)v oid fun() st a t i c int a;int b=2;a += 2 ;cout« a+b «> t* ;1 package work;2 public class HomeWork3 public static void printOmorphism(int start,int end)5 if(start >= end)6 System, o口七.printin (“开始位置不能大结束位置! *,);7 return;8 9 int squareNum = 0;boolean flag = false;11for(int i = start + 1; i < end;i+)squareNum = i;flag = false;while(squareNum < end)14 if(getSquare(squareNum) < end)flag = true;15 if(flag)16 System.out.print(squareNum + " n);)17 squareNum=getSguare(squareNum);)System.out.printin("“);24 )26+public static int getSquare(int a)return a * a;)28 、publicstatic void main(String args) Homeork3.printOmorphism(1, 99);31QO I运营结果:2 4 163 9 814 165 256 367 498 649 814.编写一个程序,实现如下功能:(1)从键盘输入aop bo其中a, b为数值;op为字符,限制为+、-、*、/。(2)调用函数811标(。46),计算表达式 op b的值。由主函数输出结果。代码:XAUpM U JO VC1A ACL Xpu OUL Xp LX Vli /101 i public olass HomeWork4 publio static int count(String opf int a, int b) ScriptEngineManager manager = new ScriptEngineManager();14ScriptEngineengine=manager .getEngineByName jsM);lbintresult=0;16try(result=(int)engine.eval(a+op+b); catch (ScriptException e)(19/ TODO Auto-generated catch blocke. prin|tStackTrace ();21)return result;23Fpublic static boolean isOperator(String op)(String Operator 26List<String> list = Arrays.asList(Operator);if(list.contains(op) return true;29)return false;31)32 public static void main(String args) 34boolean flag = true;Scanner in = new Scanner(System.in);36System.ou亡.printin(“沛输入第一个够敢!”);int a » in.nextlnt();System.out.printin!");39 String op =while(flag)40 op = in.next ();if(!HomeWork4.Isoperator(op)41 System.out.printin("输入的不足掾作符,诂旗新粕入!42 )else(flag = false;43 )System.out.println(”请输入第:个能敢!");49int b = in.nextlnt();System.out.printin(HomeWork4.count(op, a, b);运营结果:请输入第二不整数!请输入操作符! 请输入第二个整数!20I402.编写一个程序,实现如下功能:(1)输入k (<100)个整数到数组x 100 中;(2)计算k个数的平均值及大于平均值的元素个数。代码:package work;3 public class HomeWork5 public static int generatorRadomNum(int num) int 1 = new intnum;6 int rNum = 0;for(int i=0;i<num;i+)( rNum = (int)Math.floor(Math.random() * 100); 1i = rNum;10 return 1;11 12 public static double avgNum(int 1)doublesum = 0;13 doubleavgNum =0.0;for(int i:l)14 sum += i;)avgNum = sum / 1.length;return avgNum;2223public static int getThanAvgCount(double avg,int 1) int count = 0;26for(int i=0;i<l.length;i+)if(li) > avg) count+;29)30return count;32)|3334public static void main(String() args) int j = HomeWork5.generatorRadomNum(100);36double avgNum = HomeWork5.avgWu/n( j );System. out. print In ("" + avgNum);int count = getThanAvgCount(avgNum,j);System. out.printin ("大于平均数的个数足:“+ count);40 40运营结果:vri5”D“5Rj pava Mppnvaiiviij v.ri vyid平均数是:4597大广平均数的个数是:466.定义函数。v oid r e version ( i nt ar y , i n t size);逆置数组ar y的元素。例如实参数组本来为1, 5, 3, 2,6, 8, 9, 4 ,调用函数rc v e r s i o n 后变成为4, 9,8, 6, 2, 3, 5, 1 0代码:1 package work;23 public class HomeWork6 public static void reversion(int arr,int start, int end) if (start = end | | start > end) 6 return;7 if (start < end)9 swap(arr,start, end);reversion(arr, +start# -end);124 public static void swap(int A, int a, int b)(int temp;15 temp = Aa;Aa = Ab;18 Ab = temp;public static void printArr(intarr) for(int i:arr)System, out .print (i + *, *');23 public static void main(String args) 26int arr = 11,5,3,2,6,8,9,4);reversion(arr,0,arr.length - 1);printArr(arr);29 30 运营结果:、, QJU Ji 、z 、,n、z I”,'4,9,8,6,2,3,5,1,47.数组a包含50个整数,把a中所有的后项除以前项之商取整后存入数组b(即bi=ai/a i-1,并且b0=a0/a 49),最后按每行5个元素的格式输出数组b。代码:1 package work;3 public class Homework7( 4public static int generatorRadomNum(int num)6int 1(= new intnum;int rNum = 0;for(int i=0;i<num;i+) rNum = (int)Math.ceil(Math.random() * 100);1i = rNum;10 retuxm 1;13)14 public static int( getAdditionNum(intJ a)(16int b = new int a.length;int denominator = 0;18 for(int i=0;i<a.length;i+)try denominator a i-1;)catch (Exception e) denominator = aa.length - 1;2324bi = ai / denominator;25 return b;26 public static void main(String args)( int a = generatorRadomNum(50);31int b( = getAdditionNum(a);for (int 1=0; i<b. length;if(i % 5 = 0)33 System.out.printin(HW);)|36 System.out.print(bi + "t");37 )|38 运营结果:1011020020061100310080110503000200310021110120101401制.编程输出所有不超过100的其平方具有对称性质的正整数(也称回文数)。输出格式如下:numb e r s q uarc1 12 43 91 1 12122 48426 6 76代码:1 package work;23 public class HomeWork8 - public static boolean isPalindrome(int a) boolean flag = true;6if(a < 10)(return flag;8String b = new Integer(a).toString();10for(int i=0;i<b.length();i+)if(b.charAt(i) != b.charAt(b.length()- i - 1) flag = false;13 break;)14 )returnflag;15 )-publicstaticintgetSquare(int a)16 returna * a;)17 ,publicstaticvoidmain(String(args)(int square = 0; for(int i=l;i<=100;i+)(24square = getSquare(i);if(isPalindrome(squa re)System.out.printin(i + "t" + square);26 27 )28 运营结果:terminatedHomeWork8 Java Applicatk11243911121|22484266769.编写程序,打印如下杨辉三角。111 2 113 3 11 4 6 4 1代码:1 package work;23 public class Homework9 4 public static void main(String args)5 6 final int NMAX = 10;int odds = new int NM/kX + 1;for (int n = 0; n <= NMAX; n+)9 oddsn = now intn + 1;for (int n = 0; n < odds.length; n+)for (int k = 0; k < oddsn.length; k+)13 (1 4int lotteryOdds = 1;for (int i = 1; i <= k; i+)16 lotteryOdds = lotteryOdds * (n - i + 1) / i;17 odds|n (k = lotteryOdds;I18 for (int row : odds)22(for (int odd : row)24System, out.printfodd);System.out.printIn();26 )27 运营结果:111iii T"CM-* 1 1I5.P .5Jr uy,qi a ii1121i331i464115101051i615201561i72135352171i8285670562881i93684126126843691110451202102522101204510110.定义一个函数,计算长度为k的整型数组元素的平均值及大于平均值的元素个数。调用函数的语句为:c o un t (a, k, ave, num);其中a是数组名,k是数组元素个数,a v。返回的平均值,num返回大于平均值的元素个数。运营结果:468 103. Sincl u de< i o st r eam. h>int fu n ( i nt n) if (n=0) return 1 ;ret u rn 2 * fu n (n-1);)v oid mai n () int a=5;cou t <<f u n (a) <<en d 1 ;运营结果:324 . Sine 1 u d e <iost ream. h>void main () c h ar *cp = z,wo rd”;fo r (int i=0 ; i < 4 ; i + )c o u t «cp+ i << ' t ';)运营结果:w o r d cor d d二、.根据程序功能填空。1 .程序把10个数存储到一维数组a中,并求该数组中最大值。# i n c lude<i o str e a m. h>void mainO int max;i n t a10 = 76, 55, 95,8 7,8 5,83,65, 9 0, 7 7, 8 5 );in t *p=; ° max=*p;for ( ; p< ;p+)0i f( *p>ma x ) max=; ac o ut<<,max= " «max«end 1 ;).下面程序的功能是输出1至10 0之间每位数字的乘积大于每位数的和的数。例如,4 5两位 数字的乘积为4 X 5=20,和为4 + 5 =9。#in c lude<ios t r earn. h>void ma i n() i n t n, k =1, s=0, m; fo r (n =1; n<=100; n +) k=l; s=0:®wh i 1 e ()s+=m% 1 0 ;)i f (k> s ) cout<< n «> t* ;)3 .程序对输入的n求s = 1 +1 / 23 + 1/33 + 1/n3。# inclu d e<iostream. h>vo i d main ()doub i e s; i nt i, n;co u t <V n= c i n » n ;s = 0;fo r (i= 1 ; i+)s= ;cout« " s= " «s<<endl;).函数c r ea t e从键盘输入整数序列,以输入0为结束。按输入顺序建立单向链表并返I可表头。struct node int da t a; no d e * next;;n ode * c r eateO(。node *he a d , *p, * q ;。p = new node;h e ad = p;» c i n»p->data;whi le ()(° q = p;时 ;q->n ext = p;cin>> p ->delta;)。i f (head = p) head = NULL;oo else ; ,return hea d ;).以下程序求方程的所有整数解:3x + 2y 7z = 5。( 0 W x, y, z W 100 )# i nc 1 ude<iost r e am. h>void mainO in t x, y, z ;for( x=0; x<=100; x+ )for( y=0; y <=100; y+ ) if( ( z = 3*x+2*y-5 ) % 7 );。/求出z的值i f ()/检查z的范围<< z << endl ;<< z << endl ;cou t « "x=" « x « "y="<< y << " z=三、程序设计1.编写函数输出以下形状的图形,其中构成图形的数字和输出的行数通过参数传送。代码:1 package work;23 public class HomeWorkl 5a6789LOLIL2L3L4L5L6L7L8L92021222324925262728 5a6789LOLIL2L3L4L5L6L7L8L92021222324925262728 public static void printsharp(int list, int row) if(list.length < row)System.out:.printing这样做会导致数组下标异常哦! ”); return;)int column = row *2-1;for(int i = 0;i<row;i+)for(int j=0;j<column;j+)if(2 * i + 1 > j)System.out.print(listi + "");)else System.out.print(n n);)System.out.printin(nn);)public static void main(String args) int a = 1,2,3,4);HomeWorkl.printsharp(a, 4);29运营结果:12 2 23 3 3 3 34 4 4 4 4 4 45555555552.请编程序,输入两个正整数啊a和b(ab ),输出a、b之间所有整数的因数(除1和自身)。每行输出数据不超过10个。例如,若输入a为6, b为8,则输出格式规定如下:t h e f a ctor s o f 6 :2 3the facto r s of 7 :n o f act o rthe fa c tors o f 8 :2 4代码:1 package work;2 public class HomeWork2 3®45678910111213141516171819202122232425262728。293031 3®45678910111213141516171819202122232425262728。293031 public static void printFactor(int a, int b) if(a >= b)System. out. print In (" 个参数只能小第二个参数哦!"); return;boolean flag = true;for(int i=a;i<=b;i+)flag = true;System. out.printin ("the factors of " + i + " : ) if(i<=3)System.out.printin(nno factor"); continue;for(int j=2;j<i;j+) if(i%j = 0) flag = false; System. out.print (j + *' '*) if(flag)System, out.print (*no factor*); System. out.printin (*"*);)public static void main(String args) HomeWork2.printFactor(2, 13);运营结果thefactorsof2:nof actor|thenofactors factorof3:the2factorsof4 :thenofactors factorof5:the2 3factorsof6:the nofactors factorof7:the2 4factorsof8:the3factorsof9:the2 5factorsof10:thenofactors factorof11:the2 3factors4 6of12:the nofactors factorof13:据.请编程序,找出1至99之间的所有同构数。同构数是这样一组数:它出现在平方数的右边。例如:5是25右边的数,25是6 2 5右边的数,所以5和25都是同构数。代码: