CCF部分真題代碼Java_第1頁
CCF部分真題代碼Java_第2頁
CCF部分真題代碼Java_第3頁
CCF部分真題代碼Java_第4頁
CCF部分真題代碼Java_第5頁
免費預覽已結束,剩余28頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、標準 文案 2013-12-1 / 出現次數最多的數 package demo; import java.util.*; public class Test2 public static void main(String args ) new Test2().run(); public void run() System. out .println( 請輸入: ); SuppressWarnings (resource ) Scanner fin = new Scanner(System. int N = fin .nextInt(); int count = new int 10001; f

2、or (int i=0; iN;+ i)in); 標準 文案 + count fin .nextInt(); int maxCount =-1; int result = 0; for (int i=1; i maxCount ) maxCount = count i; result = i; System. out .println( result );標準 文案 2014-03-01 / 相反數 package demo; import java.util.*; public class Test3 public static void main(String args ) new Tes

3、t3().run(); public void run() SuppressWarnings (resource ) / 輸入一個正整數 1500 Scanner input = new Scanner(System. in); System. out .println( 請輸入一個 1500 的整數 :); int N = input .nextInt(); int value = new int N; int count = 0; 標準 1000 , 文案 System. out .println( 請輸入 + N + 個正整數,且每個數絕對值不超過 不同的數 ); / 加入數組 for

4、(int i = 0; i N; + i) value i = input .nextInt(); / 雙層循環查找 for (int i = 0; i N; i+) for (int j = i + 1; j N; j+) if ( value i = (- value j) count +; System. out .println( count ); 201309-3 / 字符串匹配 package demo; import java.util.Scanner; 標準 文案 public class Test4 public static void main(String args )

5、SuppressWarnings (resource ) Scanner reader = new Scanner(System. in); String str = reader .next(); int p = reader .nextInt(); int n = reader .nextInt(); int d = 0; String re = new String n; for (int t = 0; t n; t+) String s = reader .next(); if (p = 0) s = s.toLowerCase();標準 文案 str = str .toLowerCa

6、se(); if ( s.contains( str) red = s; d = d + 1; for (int i = 0; i d; i+) System. out .println( re i ); 標準 文案 2014-09-1 / 相鄰數對 package demo; import java.util.*; public class Test5 public static void main(String args ) new Test5().run(); public void run() / 輸入 System. out .println( 請輸入 ); SuppressWarn

7、ings (resource ) Scanner input = new Scanner(System. in); int N = input .nextInt(); int count = 0; int value = new int N; / 加入數組標準 文案 fo (int i = 0; i N; i+) value i = input .nextInt(); / 雙循環查找 for (int i = 0; i N; i+) for (int j = i + 1; j N; j+) int s = value i - value j if (Math. abs(s) = 1) coun

8、t +; System. out .println( count ); 標準 文案 201503-1 /圖像反轉 package demo; import java.util.*; public class Test7 public static void main(String args ) new Test7().run(); public void run() SuppressWarnings (resource) Scanner sc= new Scanner(System. in ); int M = sc. next In t(); int N = sc. next In t();

9、 /in t arr=new in tMN; int arr=new int NM; for (int i=0; i M ;i+) for (int j =0; j N ;j+) arrN-j-1 i= sc.nextInt(); / 需要找的規律,歸納法 for (int i=0; i N ;i+) for (int j=0; jM ;j+) 標準 文案 System. out .print( arr ij+ ); System. out .println(); 標準 文案 201312-03 / 最大的矩形 package demo; import java.util.*; public

10、class Test9 public static void main(String args ) new Test9().run(); public void run() SuppressWarnings (resource ) Scanner fin = new Scanner(System. int N = fin .nextInt(); int height = new int N; for (int i = 0; i N; + i) height i = fin .nextInt(); int result = 0; for (int i = 0; i = 0; - j) if (h

11、eight j height i) break ; + width ; /向第 i 個矩形的右邊尋找 for (int j = i + 1; j N; + j) if (height j height i) break ; + width ; int area = width * height i; 包含當前第 i 個矩形框的最大面積 result = Math. max ( result , area );/ max(a1,a2) 取兩個數的最大值 System. out .println( result ); 201409-2/ 畫圖 package demo; import java.u

12、til.Scanner; import java.util.HashSet; public class Test10 標準 文案 public static void main(String args) SuppressWarnings(resource) Scanner sc = new Scanner(System.in); int n = sc.nextInt(); / 要畫矩形的個數 HashSet set = new HashSet(); int axis = new int4; for(int i = 1; i = n; i+) for(int j = 0; j 4; j+) ax

13、isj = sc.nextInt(); set.addAll(transferToUniteGrid(axis0, axis1, axis2, axis3); System.out.println(set.size(); private static HashSet transferToUniteGrid(int x1, int y1, int x2, int y2) HashSet set = new HashSet(); / 對兩個點進行排序,以保證 x1x2,y1 x2) temp = x1; x1 = x2; x2 = temp; 標準 文案 if(y1 y2) temp = y1;

14、y1 = y2; y2 = temp; for(int i = x1; i x2; i+)/ 不包括上界 for(int j = y1; j y2; j+) set.add(new UnitGrid(i,j); 標準 文案 return set; class UnitGrid int x, y; / 用左下角的坐標來代表一個 UnitGrid public UnitGrid(int x, int y) this.x = x; this.y = y; / 重寫 equals 方法,若左下角坐標一致,則相等 Override public boolean equals(Object o) if(o

15、 = null) return false; if(!(o instanceof UnitGrid) return false; UnitGrid ug = (UnitGrid)o; if(this.x = ug.x) & (this.y = ug.y) return true; return false; / 重寫 hashCode 方法。 標準 文案 Override public int hashCode() / 如果兩個 UnitGrid 的 x,y 相等,則為同一元素 int result = 17; return (37*result + this.x)*37+this.y

16、; 201403-4 / 窗口 package demo; import java.util.Scanner; public class Test11 public static void main(String args ) System. out .println(輸入 N 和 M:); 標準 文案 SuppressWarnings (resource ) Scanner input = new Scanner(System. in); int n = input .nextInt(); int m = input .nextInt(); / 給出 N 個窗口位置 / 每個給出四個數,并且

17、 x1x2,y1y2 / 創建二維數組 int values = new int n 4; for (int i = 0; in;i+) System. out .println( n+個窗口 :”); for (int j = 0; j4; j+) values ij = input .nextInt(); 標準 文案 / 分別是最下層到頂層的次序 / 用一個數組存儲他們的次序 / 初始化順序 int num = new int n; for (int i = 1; i = n ;i +) num i-1 = i; / 接下來進行 M 次點擊事件 / 每行包含兩個非負整數 x,y / 輸出結

18、果存儲 String results = new String m; /0 for IGNORED / 進行點擊 / 先判斷點擊的位置落在哪些窗口,然后判斷順序 擊相應窗口 /m 次點擊 / 點擊后要改變順序 for (int i = 0; im;i+) / 每次操作 ,順序在最上面的就是我們的點 標準 文案 System. out .println( 進行點擊 :); int x = input .nextInt(); int y = input .nextInt(); for (int j = 0; j= values j0 & x= values j1 & y= valu

19、es j 3) if (results i = null | num Integer. parseInt (results i) num j) results i = String. valueOf (j); System. out .println( 第+( j +1)+ 個窗口顯示 + results i); / 被點擊的置于頂層 交換順序 /n 個窗口判斷哪個是最上面的 int top = 0; for (int k = 0; k n;k+) if (num k = n) top = k; if (results i = null ) 標準 文案 results i = IGNORED

20、else int temp = num Integer. parseInt (results i ); num Integer. parseInt (results i) = n; num top = temp ; for (int i = 0; in;i+) if (results i != null ) results i = String. valueOf (Integer. parseInt (results i)+1); System. out .println( 結果: ); for (int i = 0; im;i+) System. out .println( results

21、i); 標準 文案 201412-2 /Z 字形掃描 package demo; /*1. 根據上一步的方向以及是否到達邊界位置來決定下一步方向; 2.根據步驟 1 得到的方向前進一步,讀取該位置的數字。 循環直至到達終點 (n-1, n-1) */ import java.util.Scanner; public class Test12 / 下面四個變量代表四個方向 public static final int RIGHT = 1 ; / 向右走 public static final int DOWN = 2; / 向下走 public static final int LEFTDOW

22、N = 3; / 向做下走 public static final int RIGHTUP = 4; / 向右上走 public static int data ; / 矩 public static void main(String args ) new Test12().run(); public void run() / 接收輸入 SuppressWarnings ( resource ) Scanner scanner = new Scanner(System. in); int n = scanner .nextInt(); data = new int nn for (int i

23、= 0; i n; i+) for (int j = 0; j = 0 & y+1 = 0 & y+1 n) / 左下可走 direction = LEFTDOWN ; else / 只能走右上了 direction = RIGHTUP ; else if (direction = LEFTDOWN ) / 上一次向左下,如果可以, 下一步應該繼續向左下,否則 向右或者向下走 if (y +1 = 0) / 先判斷能否繼續向左下 direction = LEFTDOWN ; else if(y+1 n) / 然后判斷能否向下走 direction = DOWN ; else /

24、 最后只能向右走了 direction = RIGHT ;標準 文案 else if(direction = RIGHTUP ) / 上一次向右上,如果可以,下 步應該繼續向右上,否則向右或者下走 if (x+1 =0) /先判斷能否繼續向右上 direction = RIGHTUP ; else if(x+1 n) /然后判斷能否向右走 direction = RIGHT ; else /最后只能向下走了 direction = DOWN ; /根據上面確定的方向來走出下一步 switch (direction ) case RIGHT : x = x+1; break ; case DOW

25、N : y = y+1; break ; case LEFTDOWN : x = x-1; y = y+1; break ; case RIGHTUP : x = x+1; y = y-1; break ; /讀取當前走到位置的數字注意 x和 y 的位置 result += data yx + ; 標準 文案 /輸出結果(這里需不需要去掉最后的空格?沒怎么玩過 ACM,不記得了 格式控制會不會這么嚴) System. out .println( result ); 10. import java.io.BufferedReader; import java.io.InputStreamRead

26、er; /ISBN public class Test13 public static void main(String args ) BufferedReader bin = new BufferedReader( InputStreamReader(System. in); try int sum =0; char cc= 0 ; String isbn_0 = bin .readLine(); String isbn = isbn_0 .replace( - , ); acc new 標準 文案 for (int i=0; i9; i+)標準 文案 int ii=( int )isbn

27、.charAt( i )-48; sum += ii*( i+1); sum = sum % 11; if (sum = 10) cc = X; else cc = ( char )(sum +48); if (cc = isbn .charAt(9) System. out .println( Right ); else isbn_0 = isbn_0 .substring(0,12) + System. out .println( isbn_0 ); catch (Exception e) e.printStackTrace(); 201412-03 / 集合競價 package demo

28、; import java.text.DecimalFormat; import java.util.Arrays; import java.util.Comparator; cc; 標準 文案 import java.util.Scanner; class StockArray String SBC; float price; int number; public class Test14 public static void main(String args) System.out.println(please input data); Scanner scanner=new Scanne

29、r(System.in); StockArray ss=new StockArray5002; for (int i = 0; i 5002; i+) ssi=new StockArray(); 標準 文案 int num=1; while(scanner.hasNext() ssnum.SBC=scanner.next(); if (ssnum.SBC.equals(buy)|ssnum.SBC.equals(sell) ssnum.price=scanner.nextFloat(); ssnum.number=scanner.nextInt(); else if (ssnum.SBC.eq

30、uals(cancel) ssscanner.nextInt().SBC=CANCEL; / 如果取消,則將相應行標記為 num+; System.out.println(aaa); StockArray n1=new StockArraynum; / 買方 for (int i = 0; i num; i+) n1i=new StockArray(); StockArray n2=new StockArraynum; / 賣方 for (int i = 0; i num; i+) n2i=new StockArray(); int num1=0; int num2=0; CANCEL 標準 文案 int ans_num=0; float ans_price=0; for(int i=1;inum;i+) if (ssi.SBC.equals(buy) / 放入 Buy 的出價和交易量 n1num1.price=ssi.price; n1num1.number=ssi.number; num1+; if (ssi.SBC.equals(sell) / 放入 sell 的出價和交易量 n2num2.price=ssi.price; n2num2.number=ssi.number; num2+; Arrays.sort(n1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論