java小练习

上传人:第*** 文档编号:34212501 上传时间:2018-02-22 格式:DOC 页数:15 大小:150.50KB
返回 下载 相关 举报
java小练习_第1页
第1页 / 共15页
java小练习_第2页
第2页 / 共15页
java小练习_第3页
第3页 / 共15页
java小练习_第4页
第4页 / 共15页
java小练习_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《java小练习》由会员分享,可在线阅读,更多相关《java小练习(15页珍藏版)》请在金锄头文库上搜索。

1、Problem A 计算数字出现次数Problem Description编写程序,读取在 1 到 100 之间的整数,然后计算每个数出现的次数。假定输入是以0 结束的。注意:如果一个数的次数大于一次,就在输出时使用复数“times” 。测试示例Test Case 1:Input2 5 6 5 4 3 23 43 2 0Output2 occurs 2 times3 occurs 1 time4 occurs 1 time5 occurs 2 times6 occurs 1 time23 occurs 1 time43 occurs 1 timeTest Case 2:Input2 5 7 5

2、 4 3 7 2 0Output2 occurs 2 times3 occurs 1 time4 occurs 1 time5 occurs 2 times7 occurs 2 timeTest Case 3:Input1 2 5 6 2 0Output1 occurs 1 time2 occurs 2 times5 occurs 1 time6 occurs 1 timeTest Case 4:Input1 5 6 5 4 4 23 1 0Output1 occurs 2 times4 occurs 2 time5 occurs 2 times6 occurs 1 time23 occurs

3、 1 timeTest Case 5:Input2 4 3 2 0Output2 occurs 2 times3 occurs 1 time4 occurs 1 time程序代码01020304050607080910111213141516171819202122import java.util.Scanner;public class NumberCount public static void main(String args) / TODO code application logic hereint NumberArray = new int101;Scanner input = n

4、ew Scanner(System.in);while (input.hasNext() int temp = input.nextInt();if (temp = 0) break;NumberArraytemp+;for (int i = 1; i 1)System.out.println(i + occurs + NumberArrayi + times);Problem B 相同的数组Problem Description如果两个数组 list1 和 list2 的内容相同,那么就说他们是相同的。使用下面的方法头编写一个方法,如果 list1 和 list2 是相同的,该方法就返回 t

5、rue:public static boolean equal(int list1, int list2);编写一个测试程序,提示用户输入两个整数数列,然后显示他们两个是否相同。测试示例Test Case 1:Input5 2 5 6 6 15 5 2 6 1 6OutputTwo lists are identicalTest Case 2:Input5 2 5 6 6 15 5 2 6 1 3OutputTwo lists are not identicalTest Case 3:Input5 5 5 6 6 15 2 5 6 1 6OutputTwo lists are not iden

6、ticalTest Case 4:Input5 5 5 6 7 15 5 5 6 1 7OutputTwo lists are identicalTest Case 5:Input5 5 5 6 6 15 5 5 6 1 7OutputTwo lists are not identical程序代码010203040506070809101112131415161718192021222324252627282930313233import java.util.*;public class ListCompare public static void main(String args) / TO

7、DO code application logic hereScanner input = new Scanner(System.in);int size1 = input.nextInt();int list1 = new intsize1;for (int i = 0; i p, y2 q, z2 r, xy u, yz v, zx w and the function f(x,y,z) = ax2 + by2 + cy2 + dxy + eyz + fzx + gx + hy + iz + j can be written as g(p,q,r,u,v,w,x,y,z) = ap + b

8、q + cr + du + ev + fw + gx + hy + iz + j, which is a linear function with 9 variables.Now your task is to write a program to change f into g.测试示例Test Case 1:Input0 46 3 4 -5 -22 -8 -32 24 27Output46q+3r+4u-5v-22w-8x-32y+24z+27Test Case 2:Input2 31 -5 0 0 12 0 0 -49 12Output2p+31q-5r+12w-49z+12Test C

9、ase 3:Input0 0 0 0 0 0 0 0 0 0Output0Test Case 4:Input0 0 0 0 0 0 0 0 0 -9Output-9Test Case 5:Input0 0 1 0 0 0 0 0 0 -9Outputr-9程序代码0102030405060708091011121314151617181920212223242526272829303132333435import java.util.Scanner;public class Main public static void main(String args) / TODO code applic

10、ation logic hereScanner input = new Scanner(System.in);int times = input.nextInt();for (int i = 0; i 1)System.out.printf(+%d%c, constantNumberi, variablesi);else if(constantNumberi 0)System.out.println(+ + constantNumber9);elseSystem.out.println(constantNumber9);return;Problem F 全排列Problem Descripti

11、on写一个函数,例如:给你 abc,则输出其全排列 abc、acb、bac 、bca、cab 、cba。测试示例Test Case 1:InputabcOutput6abc, acb, bac, bca, cab, cbaTest Case 2:InputabOutput2ab, baTest Case 3:InputaOutput1aTest Case 4:InputabdOutput6abd, adb, bad, bda, dab, dbaTest Case 5:InputabcdOutput24abcd, abdc, acbd, acdb, adbc, adcb, bacd, badc,

12、 bcad, bcda, bdac, adb, cbad, cbda, cdab, cdba, dabc, dacb, dbac, dbca, dcab, dcba程序代码0102030405060708091011121314151617181920212223242526import java.util.*;public class stringSequence public static void main(String args) / TODO code application logic hereScanner input = new Scanner(System.in);Strin

13、g str = input.next();List result = list(str, );System.out.println(result.size();System.out.println(result);public static List list(String base, String buff) List result = new ArrayList();if (base.length() temp = list(new StringBuilder(base).deleteCharAt(i).toString(), buff + base.charAt(i);result.addAll(temp);return result;

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

当前位置:首页 > 办公文档 > 解决方案

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