給定一個日期,輸出這個日期是該年的第幾天。
Input
數據格式為YYYY/MM/DD組成,具體參見sample input ,另外,可以向你確保所有的輸入數據是合法的。
Output
輸出一行,表示該日期是該年的第幾天。
Sample Input 1
1985/1/20
Sample Output 1
20
import java.util.Scanner; import static java.lang.Math.abs; //input:1985/1/20 output:20 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String date = sc.nextLine(); String[] array = date.split("/"); int year = Integer.parseInt(array[0]); int month = Integer.parseInt(array[1]); int day = Integer.parseInt(array[2]); boolean flag = isleap(year); if(!flag){ System.out.println(whatday(month,day)); }else{ if(month<=2){ System.out.println(whatday(month,day)); }else { System.out.println(whatday(month,day)+1); } } } public static int whatday(int month,int day) { if (month == 1) return day; if (month == 2) return day + 31; if (month == 3) return day + 31 + 28; if (month == 4) return day + 31 + 28 + 31; if (month == 5) return day + 31 + 28 + 31 + 30; if (month == 6) return day + 31 + 28 + 31 + 30 + 31; if (month == 7) return day + 31 + 28 + 31 + 30 + 31 + 30; if (month == 8) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31; if (month == 9) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31; if (month == 10) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30; if (month == 11) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; if (month == 12) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; return 0; } public static boolean isleap(int year){ boolean flag =false; if(year%4==0){ flag=true; if(year%100==0){ flag=false; if(year%400==0){ flag=true; } } } return flag; } }
2.
Description
計算 1 + 2 + 3 + ... + n
Input
輸入將包含一系列整數n,每行一個整數。
Output
對於每種情況,在一行中輸出答案, 結果將在32位整數的范圍內。
Sample Input 1
1 100
Sample Output 1
1 5050
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { long n = sc.nextLong(); long sum = 0; for (long i = 1; i <= n; i++) { sum += i; } System.out.println(sum); } } }
Description
對於表達式n^2+n+41,當n在(x,y)范圍內取整數值時(包括x,y)(-39<=x<y<=50),判定該表達式的值是否都為素數。
Input
輸入數據有多組,每組占一行,由兩個整數x,y組成,當x=0,y=0時,表示輸入結束,該行不做處理。
Output
對於每個給定范圍內的取值,如果表達式的值都為素數,則輸出"OK",否則請輸出“Sorry”,每組輸出占一行。
Sample Input 1
0 1 0 0
Sample Output 1
OK
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int x = sc.nextInt(); int y = sc.nextInt(); if(x==0&&y==0) return; boolean flag = false; for(int i=x;i<=y;i++){ int res = function(i); flag = isprime(res); if(!flag){ System.out.println("Sorry"); return; } } System.out.println("OK"); } } public static boolean isprime(int num){ if(num<2) return false; if(num==3||num==2) return true; int temp = (int) Math.sqrt(num); for(int i= 2;i <=temp; i++){ if(num%i==0) return false ; } return true ; } public static int function(int x){ return x*x+x+41; } }
4.絕對值排序
Description
輸入n(n<=100)個整數,按照絕對值從大到小排序后輸出。題目保證對於每一個測試實例,所有的數的絕對值都不相等。
Input
輸入數據有多組,每組占一行,每行的第一個數字為n,接着是n個整數,n=0表示輸入數據的結束,不做處理。
Output
對於每個測試實例,輸出排序后的結果,兩個數之間用一個空格隔開。每個測試實例占一行。
Sample Input 1
3 3 -4 2 4 0 1 2 -3 0
Sample Output 1
-4 3 2 -3 2 1 0
import java.util.Scanner; import static java.lang.Math.abs; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); while(sc.hasNext()){ String[] strr = sc.nextLine().split(" "); String[] str = new String[strr.length-1]; if (str.length >= 0) System.arraycopy(strr, 1, str, 0, str.length); int[] num = new int[str.length]; for(int i=0;i<str.length;i++){ num[i] = Integer.parseInt(str[i]); } for(int i=0;i<num.length-1;i++){ for(int j=0;j<num.length-1-i;j++){ if(abs(num[j])<abs(num[j+1])){ int temp = num[j]; num[j] = num[j+1]; num[j+1] = temp; } } } for(int n:num){ System.out.print(n+" "); } } } }
5.二進制數
Description
若將一個正整數化為二進制數,在此二進制數中,我們將數字1的個數多於數字0的個數的這類二進制數稱為A類數,否則就稱其為B類數。
例如:
(13)10=(1101)2
其中1的個數為3,0的個數為1,則稱此數為A類數;
(10)10=(1010)2
其中1的個數為2,0的個數也為2,稱此數為B類數;
(24)10=(11000)2
其中1的個數為2,0的個數為3,則稱此數為B類數;
程序要求:
求出1~1000之中(包括1與1000),全部A、B兩類數的個數。
Input
無
Output
在一行中輸出兩個整數A和B,A表示A類數的個數,B表示B類數的個數,AB之間由一個空格分隔,除此之外不要再輸出其他多余的東西。
public class Main { public static void main(String[] args) { int countA=0,countB=0; for(int i=1;i<=1000;i++){ int countone = 0,countzero = 0; String[] str = String.valueOf(Binary(i)).split(""); for(String s:str){ if("1".equals(s)){ countone++; }else{ countzero++; } } if(countone>countzero){ countA++; }else{ countB++; } } System.out.println(countA+" "+countB); } public static int Binary(int n){ String result = Integer.toBinaryString(n); return Integer.parseInt(result); } }
6.交換最小值和最大值
Description
輸入一個正整數n,再輸入n個整數,將最小值與第一個數交換,最大值與最后一個數交換,然后輸出交換后的n個數。
Input
有多個測試用例。 每個案例包含一個正整數n,接着n個正整數。 n = 0時測試用例輸入結束。 該測試用例不做處理。
Output
將每個測試用例輸入的n個整數,將最小值與第一個數交換,最大值與最后一個數交換,然后輸出交換后的n個數。
Sample Input 1
5 4 3 5 1 2 4 1 5 6 7 5 5 4 3 2 1 0
Sample Output 1
1 3 2 4 5 1 5 6 7 1 4 3 2 5
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); if(n == 0) return; int[] num = new int[n]; for(int i=0;i<n;i++){ num[i] = sc.nextInt(); } //找最小值並交換 int indexmin = 0; int min = num[0]; int k; for(k=1;k<n;k++){ if(num[k]<min) { min=num[k]; indexmin = k; } } int temp = num[indexmin]; num[indexmin] = num[0]; num[0] = temp; //找最大值並交換 int indexmax = 0; int max = num[0]; int j; for(j=1;j<n;j++){ if(num[j]>max) { max=num[j]; indexmax = j; } } temp = num[indexmax]; num[indexmax] = num[n-1]; num[n-1] = temp; for(int c:num){ System.out.print(c+" "); } System.out.println(); } } }
7.DNA排序
Description
逆序數可以用來描述一個序列混亂程度的量,例如,"DAABEC"的逆序數為5,其中D大於它右邊的4個數,E大於它右邊的1個數,4+1=5;又如,"ZWQM"的逆序數為3+2+1+0=6。
現在有許多長度一樣的字符串,每個字符串里面只會出現四種字母(A,T,G,C)。請編寫程序,將這些字符串按照它們的逆序數進行排序。
Input
輸入由多組測試數據組成。每組測試數據的第一行為兩個數n和m,0<n<=50,0<m<=100。n表示每個序列的長度(同一組測試數據中各序列的長度都為n),m表示此組測試數據中的序列個數。接下來有m行,每行為一個長度為n的DNA字母序列。
Output
對於每組測試數據,輸出排序后的序列列表。在排序時,逆序數小的序列排在前面。如果兩個序列的逆序數相等,那么它們在列表中出現的順序和它們在輸入中的順序相同。
在每組測試數據后輸出一行“********************”(二十個星號)。
Sample Input 1
3 3 ACG CGT AGT 6 2 TAAAAA ATAAAA
Sample Output 1
ACG CGT AGT ******************** ATAAAA TAAAAA ********************
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int length = sc.nextInt(); int count = sc.nextInt(); sc.nextLine(); String[] array = new String[count]; for (int i = 0; i < count; i++) { String str = sc.nextLine(); array[i] = str; } for (int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array.length - 1 - i; j++) { if (countReverse(array[j]) > countReverse(array[j + 1])) { String temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } for (String s : array) { System.out.println(s); } System.out.println("********************"); } } public static int countReverse(String str){ String[] array = str.split(""); int length = array.length; int count=0; for(int i=0;i<length;i++){ if("A".equals(array[i])) continue; if("C".equals(array[i])){ for(int k=i+1;k<length;k++){ if("A".equals(array[k])) count++; } } if("G".equals(array[i])){ for(int k=i+1;k<length;k++){ if("A".equals(array[k])||"C".equals(array[k])) count++; } } if("T".equals(array[i])){ for(int k=i+1;k<length;k++){ if("A".equals(array[k])||"C".equals(array[k])||"G".equals(array[k])) count++; } } } return count; } }
8.求整數的位數以及各位數之和
Description
輸入一個正整數repeat(0<repeat<10),做repeat次下列運算:
輸入一個整數,輸出它的位數以及各位數之和。
Input
正整數repeat及repeat個整數
Output
整數的位數以及各位數之和
Sample Input 1
4 123456 -100 -1 99
Sample Output 1
number=6,sum=21 number=3,sum=1 number=1,sum=1 number=2,sum=18
import java.util.Scanner; import static java.lang.Math.abs; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=0;i<n;i++){ int num = abs(sc.nextInt()); String[] array = String.valueOf(num).split(""); int count = array.length; int sum = 0; for(String s:array){ sum+=Integer.parseInt(s); } System.out.println("number="+count+",sum="+sum); } } }
9.輸出Fibonacci序列
Description
輸入一個正整數repeat(0<repeat<10),做repeat次下列運算:
輸入2個正整數m和n(1<=m,n<=300000),輸出m和n之間所有的Fibonacci數。
Fibonacci序列除第一個和第二個數外,任意一個數都可由前兩個數相加得到,第一個數和第二個數的值均為1。
Fibonacci序列(第1項起):1 1 2 3 5 8 13 21 ......
Input
輸入一個正整數repeat(0<repeat<10),代表做repeat次運算
輸入repeat個正整數m和n
Output
輸出
repeat次
m和n之間所有的Fibonacci數
每兩個Fibonacci數之間用一個空格隔開,m和n之間的最后一個Fibonacci數后面也有一個空格
Sample Input 1
3 1 10 20 100 1000 6000
Sample Output 1
1 1 2 3 5 8 21 34 55 89 1597 2584 4181
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=0;i<n;i++){ int start = sc.nextInt(); int end = sc.nextInt(); int[] res = Fibonacci(start,end); for(int num:res){ System.out.print(num+" "); } System.out.println(); } } public static int[] Fibonacci(int start,int end){ int[] array = new int[30]; array[0] = 1; array[1] = 1; for(int i=2;i<30;i++) { array[i]=array[i-1]+array[i-2]; if(array[i] > end) break; } List<Integer>list = new ArrayList<>(); for(int n:array) { if(n>=start && n<=end) { list.add(n); } } int[] fibonacci = new int[list.size()]; for(int i = 0;i<list.size();i++){ fibonacci[i] = list.get(i); } return fibonacci; } }
10.請完成匯率和金額排序程序
Description
在國際機場,我們會發現有多個貨幣兌換的窗口,這是由於各國貨幣的單位價值是不一樣的。
下面列出了某日國際貨幣的匯率表(相對於100人民幣的各國貨幣值)。
貨幣 | CNY | HKD | USD | EUR
匯率 | 100 | 118 | 15 | 13
例如,100元人民幣可以兌換118香港幣。請利用繼承機制與Comparable接口實現不同數目貨幣對象價值的排序。
Input
輸入3個數字
Output
金額價值從小到大
Sample Input 1
100 100 100
Sample Output 1
HKD100 USD100 EUR100
import java.util.Arrays; import java.util.Scanner; class Currency { private String name; //貨幣名稱 private int originalValue; //原始值 private int value; //轉換為人民幣后的值 public static String[] CURRENCY_NAME = { "CNY", "HKD", "USD", "EUR" }; public static int[] CURRENCY_RATIO = { 100, 118, 15, 13 }; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getOriginalValue() { return originalValue; } public void setOriginalValue(int originalValue) { this.originalValue = originalValue; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } } class HKD extends Currency implements Comparable { // 實現你的構造函數與Comparable中的接口 public HKD(int hkd){ this.setName("HKD"); this.setValue(hkd); this.setOriginalValue((int)(100.0/118.0*hkd)); } @Override public int compareTo(Object o) { Currency oo = (Currency)o; if(this.getOriginalValue()<oo.getOriginalValue()){ return -1; }else{ return 0; } } } class USD extends Currency implements Comparable { public USD(int usd){ this.setName("USD"); this.setValue(usd); this.setOriginalValue((int)(100.0/15.0*usd)); } @Override public int compareTo(Object o) { Currency oo = (Currency)o; if(this.getOriginalValue()<oo.getOriginalValue()){ return -1; }else{ return 0; } } } class EUR extends Currency implements Comparable { public EUR(int eur){ this.setName("EUR"); this.setValue(eur); this.setOriginalValue((int)(100.0/13.0*eur)); } @Override public int compareTo(Object o) { Currency oo = (Currency)o; if(this.getOriginalValue()<oo.getOriginalValue()){ return -1; }else{ return 0; } } } public class Main { public static void main(String[] args) { Currency[] cs = new Currency[3]; //初始化 Scanner sc = new Scanner(System.in); //利用hasNextXXX()判斷是否還有下一輸入項 int a = 0; int b = 0; int c = 0; if (sc.hasNext()) { a = sc.nextInt(); cs[0] = new HKD(a); } if (sc.hasNext()) { b = sc.nextInt(); cs[1] = new USD(b); } if (sc.hasNext()) { c = sc.nextInt(); cs[2] = new EUR(c); } Arrays.sort(cs); for(int i=0;i<3;i++){ System.out.println(cs[i].getName()+""+cs[i].getValue()); } } }