Java PAT练习2.0
2017-01-05 01:07
274 查看
2.1
Given number n. Print number from 1 to n. But:
when number is divided by 3, print “fizz”.
when number is divided by 5, print “buzz”.
when number is divided by both 3 and 5, print “fizz buzz”.
2.2 Second Max of Array
Find the second max number in a given array.
2.3 Simple Calculator
Given two integers a and b, an operator, choices:
![](http://img.blog.csdn.net/20170104143817350?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc1ViXzU2OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2.5 找出最大的对象
![](http://img.blog.csdn.net/20170104152002552?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc1ViXzU2OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2.6 对字符串进行排序输出
![](http://img.blog.csdn.net/20170105010510690?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc1ViXzU2OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Given number n. Print number from 1 to n. But:
when number is divided by 3, print “fizz”.
when number is divided by 5, print “buzz”.
when number is divided by both 3 and 5, print “fizz buzz”.
/** * Created by ukiyoe on 17-1-4. */ import java.util.*; public class Fizz_Buzz { public static ArrayList<String> fizzBuzz(int n){ ArrayList<String> res= new ArrayList<String>(); for(int i=1;i<=n;i++){ if( i %3 ==0 && i%5 ==0){ res.add("fizz buzz"); } else if(i%3 ==0){ res.add("fizz"); } else if(i %5 ==0){ res.add("buzz"); }else{ // res.add(Integer.toString(i)); res.add(String.valueOf(i)); } } return res; } public static void main(String []args){ Scanner cin = new Scanner(System.in); int n = cin.nextInt(); ArrayList<String> res = new ArrayList<String>(); res = fizzBuzz(n); /* for(int i=0;i<n;i++){ System.out.println(res[i]); } */ for(String s:res){ System.out.println(s); } } }
2.2 Second Max of Array
Find the second max number in a given array.
/** * Created by ukiyoe on 17-1-4. */ public class Second_Max { public static int secondMax(int [] nums){ int max = Integer.MIN_VALUE; int secMax = Integer.MIN_VALUE; for( int i=0;i<nums.length;i++) { if (nums[i] > secMax) { if (nums[i] > max) { secMax = max; max = nums[i]; } else { secMax = nums[i]; } } } return secMax; } public static void main(String []args){ int a[] = {5,2,4,3,1,6}; int ret = secondMax(a); System.out.println(ret); } }
2.3 Simple Calculator
Given two integers a and b, an operator, choices:
/** * Created by ukiyoe on 17-1-4. */ public class Calculator { public static int calculate(int a,char operator,int b){ int ret=0; if(operator =='+'){ ret=a+b; } if(operator =='-'){ ret=a-b; } if(operator =='*'){ ret=a*b; } if(operator =='/'){ ret=a/b; } return ret; } public static void main(String []args){ int a=6,b=2; System.out.println(calculate(a,'+',b)); System.out.println(calculate(a,'-',b)); System.out.println(calculate(a,'*',b)); System.out.println(calculate(a,'/',b)); } } 2.4 查找电话号码
/** * Created by ukiyoe on 17-1-4. */ import java.util.*; public class Map_Test2 { public static void main(String []args){ Scanner cin = new Scanner (System.in); Map m = new HashMap() ; while(true){ String x= cin.next(); if(x.equals("noname")){ break; } String y= cin.next(); m.put(x,y); } Set set = m.keySet(); Iterator it = set.iterator(); String a=cin.next(); int flag=1; while(it.hasNext()){ String t =(String) it.next(); if(t.equals(a)){ System.out.println( m.get(t) ); flag=0; } } if(flag==1){ System.out.println("Not found."); } } }
2.5 找出最大的对象
/** * Created by ukiyoe on 17-1-4. */ import java.util.*; public class Main{ public static Object max(Comparable []a){ Arrays.sort(a); return a[a.length-1]; } public static void main(String []args){ Scanner cin = new Scanner(System.in); String [] str = new String [5]; Integer [] num = new Integer[5]; for(int i=0;i<5;i++){ str[i]=cin.next(); } for(int i=0;i<5;i++){ num[i]=cin.nextInt(); } System.out.println("Max string is "+max(str)); System.out.println("Max integer is "+max(num)); } }
2.6 对字符串进行排序输出
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; /** */ public class Main { public static void main(String []args){ Scanner cin = new Scanner(System.in); while(cin.hasNext()){ String str = cin.next(); int len = str.length(); char [] a= new char [len]; for(int i=0;i<len;i++){ a[i] =str.charAt(i); } Arrays.sort(a); String t=""; for(int i=0;i<len;i++){ t += a[i]; } System.out.println(t); } } }
相关文章推荐
- Pat天梯练习L2-009. 抢红包 java实现
- Java PAT练习1.0
- PAT练习集天梯赛L1-002. 打印沙漏--JAVA
- WSDP 2.0产生的WebService类在Java Application Server 9下的问题
- JTR (Java Test Runner) 2.0 发布
- Java Pet Store 2.0介绍
- java1.5和.net 2.0的比较
- JAVA I/O 里面的东西,自己翻译下,顺便练习英语
- 使用JavaServer Pages2.0开发Web应用
- 不可以使用 Firefox 2.0 调试 Java HttpSession 多线程访问
- Java数据对象JDO 2.0查询语言的特点
- Java Q&A: 抽象类和接口 - 实战练习
- Java5.0与C#2.0的区别之一:Struct类型
- 结合SPRING 2.0使用JAVA PERSISTENCE API
- The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build pat
- Java Pet Store 2.0体验
- 在较量中携手前行——写给Java 1.5和.NET 2.0
- 在较量中携手前行——写给Java 1.5和.NET 2.0
- 我的JAVA练习代码
- java Spring 2.0 学习