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”.
/**
* 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);
}
}
}