CCF部分真题代码(Java)_第1页
CCF部分真题代码(Java)_第2页
CCF部分真题代码(Java)_第3页
CCF部分真题代码(Java)_第4页
CCF部分真题代码(Java)_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、2013-12-1/出现次数最多的数package demo;import java.util.*;public class Test2public static void main(String args)new Test2().run();public void run()System.out.println("请输入:");SuppressWarnings("resource")Scanner fin = new Scanner(System.in);int N =fin.nextInt();intcount = new int10001;for(

2、int i=0;i<N;+i)+countfin.nextInt();int maxCount =-1;int result = 0;for (int i=1;i<=10000;+i)if(counti>maxCount)maxCount=counti;result = i;System.out.println(result);2014-03-01/相反数package demo;import java.util.*;public class Test3 public static void main(String args) new Test3().run();public

3、 void run() SuppressWarnings("resource")/ 输入一个正整数 1500Scanner input = new Scanner(System.in);System.out.println("请输入一个1500的整数:");int N = input.nextInt();int value = new intN;int count = 0;System.out.println("请输入" + N + "个正整数,且每个数绝对值不超过1000,不同的数");/ 加入数组for (in

4、t i = 0; i < N; +i) valuei = input.nextInt();/ 双层循环查找for (int i = 0; i < N; i+) for (int j = i + 1; j < N; j+) if (valuei = (-valuej) count+;System.out.println(count);201309-3/字符串匹配package demo;import java.util.Scanner;public class Test4 public static void main(String args) SuppressWarnings

5、("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 Stringn;for (int t = 0; t < n; t+) String s = reader.next();if (p = 0) s = s.toLowerCase();str = str.toLowerCase();if (s.contains(st

6、r) red = s;d = d + 1;for (int i = 0; i < d; i+)System.out.println(rei);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("请输入");SuppressWarnings("resource")Scanner i

7、nput = new Scanner(System.in);int N = input.nextInt();int count = 0;int value = new intN;/ 加入数组for (int i = 0; i < N; i+) valuei = input.nextInt();/ 双循环查找for (int i = 0; i < N; i+) for (int j = i + 1; j < N; j+) int s = valuei - valuej;if (Math.abs(s) = 1)count+;System.out.println(count);20

8、1503-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.nextInt(); int N=sc.nextInt(); /int arr=new intMN; int arr=new intNM; for(int i

9、=0;i<M;i+) for(int j=0;j<N;j+) arrN-j-1i=sc.nextInt(); /需要找的规律,归纳法 for(int i=0;i<N;i+) for(int j=0;j<M;j+) System.out.print(arrij+" "); System.out.println(); 201312-03/最大的矩形package demo;import java.util.*;public class Test9 public static void main(String args) new Test9().run()

10、;public void run() SuppressWarnings("resource")Scanner fin = new Scanner(System.in);int N = fin.nextInt();int height = new intN;for (int i = 0; i < N; +i)heighti = fin.nextInt();int result = 0;for (int i = 0; i < N; +i) int width = 1;/ 每次循环i自增时,都要将width设为1/ 向第i个矩形的左方寻找for (int j = i

11、- 1; j >= 0; -j) if (heightj < heighti)break;+width;/ 向第i个矩形的右边寻找for (int j = i + 1; j < N; +j) if (heightj < heighti)break;+width;int area = width * heighti;/ 包含当前第i个矩形框的最大面积result = Math.max(result, area);/ max(a1,a2)取两个数的最大值System.out.println(result);201409-2/画图package demo;import jav

12、a.util.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<UnitGrid> set = new HashSet<UnitGrid>(); int axis = new int4; for(int i = 1;

13、i <= n; i+) for(int j = 0; j < 4; j+) axisj = sc.nextInt(); set.addAll(transferToUniteGrid(axis0, axis1, axis2, axis3); System.out.println(set.size(); private static HashSet<UnitGrid> transferToUniteGrid(int x1, int y1, int x2, int y2) HashSet<UnitGrid> set = new HashSet<UnitGri

14、d>(); /对两个点进行排序,以保证x1<x2,y1<y2。 int temp; if(x1 > x2) temp = x1; x1 = x2; x2 = temp; if(y1 > y2) temp = y1; 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 pub

15、lic UnitGrid(int x, int y) this.x = x; this.y = y; /重写equals方法,若左下角坐标一致,则相等 Override public boolean equals(Object o) if(o = 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方法。

16、Override public int hashCode() /如果两个UnitGrid的x,y相等,则为同一元素 int result = 17; return (37*result + this.x)*37+this.y; 201403-4 /窗口package demo;import java.util.Scanner;public class Test11 public static void main(String args) System.out.println("输入N和M:"); SuppressWarnings("resource")S

17、canner input = new Scanner(System.in); int n = input.nextInt(); int m = input.nextInt(); /给出N个窗口位置 /每个给出四个数,并且x1<x2,y1<y2 /创建二维数组 int values = new intn4; for(int i = 0;i<n;i+) System.out.println(n+"个窗口:"); for(int j = 0;j<4;j+) valuesij = input.nextInt(); /分别是最下层到顶层的次序 /用一个数组存储

18、他们的次序 /初始化顺序 int num = new intn; for(int i = 1;i <= n;i+) numi-1 = i; /接下来进行M次点击事件 /每行包含两个非负整数x,y /输出结果存储 String results = new Stringm; /0 for IGNORED /进行点击 /先判断点击的位置落在哪些窗口,然后判断顺序,顺序在最上面的就是我们的点击相应窗口 /m次点击 /点击后要改变顺序 for(int i = 0;i<m;i+) /每次操作 System.out.println("进行点击:"); int x = inpu

19、t.nextInt(); int y = input.nextInt(); for(int j = 0;j<n;j+) if(x >= valuesj0 && x<=valuesj2 && y >=valuesj1 &&y<=valuesj3) if(resultsi = null | numInteger.parseInt(resultsi) < numj) resultsi = String.valueOf(j); System.out.println("第"+(j+1)+"个

20、窗口显示" + resultsi); /被点击的置于顶层 交换顺序 /n个窗口判断哪个是最上面的 int top = 0; for(int k = 0;k<n;k+) if(numk = n) top = k; if(resultsi = null) resultsi = "IGNORED" else int temp = numInteger.parseInt(resultsi); numInteger.parseInt(resultsi) = n; numtop = temp; for(int i = 0;i<n;i+) if(resultsi !

21、= null) resultsi = String.valueOf(Integer.parseInt(resultsi)+1); System.out.println("结果:"); for(int i = 0;i<m;i+) System.out.println(resultsi); 201412-2/Z字形扫描package demo;/*1.根据上一步的方向以及是否到达边界位置来决定下一步方向; 2.根据步骤1得到的方向前进一步,读取该位置的数字。 循环直至到达终点(n-1, n-1) */import java.util.Scanner;public clas

22、s Test12 /下面四个变量代表四个方向 public static final int RIGHT = 1; /向右走 public static final int DOWN = 2; /向下走 public static final int LEFTDOWN = 3; /向做下走 public static final int RIGHTUP = 4; /向右上走 public static int data; /矩 public static void main(String args) new Test12().run(); public void run() /接收输入 Sup

23、pressWarnings("resource")Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); data = new intnn for(int i = 0; i < n; i+) for(int j = 0; j < n; j+) dataij = scanner.nextInt(); /从第一个位置开始,x为横坐标,y为纵坐标,注意x,y在二维数组中的位置 int x = 0; int y = 0; /输出要求有空格隔开 String result = datayx

24、+ " " /方向变量的初始值 int direction = 0; /下面就开始出发走咯 while( !(x = n-1 && y = n-1) )/循环直至到达终点(最右下角的位置) /先判断下一步往哪个方向走 if(direction = 0) /为0说明还没走出第一步,所以接着应该往右边走一步 direction = RIGHT; else if(direction = RIGHT) /上一次方向向右,下一步应该向左下或者右上 if(x-1 >= 0 && y+1 < n) /左下可走 direction = LEFTD

25、OWN; else /只能走右上了 direction = RIGHTUP; else if(direction = DOWN) /上一次方向向下,下一步应该向左下或者右上 if(x-1 >= 0 && y+1 < n) /左下可走 direction = LEFTDOWN; else /只能走右上了 direction = RIGHTUP; else if(direction = LEFTDOWN) /上一次向左下,如果可以,下一步应该继续向左下,否则 向右或者向下走 if(y+1 < n && x-1 >= 0) /先判断能否继续向左

26、下 direction = LEFTDOWN; else if(y+1 < n) /然后判断能否向下走 direction = DOWN; else /最后只能向右走了 direction = RIGHT; else if(direction = RIGHTUP) /上一次向右上,如果可以,下一步应该继续向右上,否则向右或者下走 if(x+1 < n && y-1 >=0) /先判断能否继续向右上 direction = RIGHTUP; else if(x+1 < n) /然后判断能否向右走 direction = RIGHT; else /最后只能向

27、下走了 direction = DOWN; /根据上面确定的方向来走出下一步 switch(direction) case RIGHT: x = x+1; break; case DOWN: 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 += datayx + " " /输出结果(这里需不需要去掉最后的空格?没怎么玩过ACM,不记得了acc格式控制会不会这么严) System

28、.out.println(result); 10.import java.io.BufferedReader;import java.io.InputStreamReader;/ISBN号码public class Test13 public static void main(String args) BufferedReader bin = new BufferedReader(new InputStreamReader(System.in); try int sum=0; char cc='0' String isbn_0= bin.readLine(); String i

29、sbn=isbn_0.replace("-", ""); for (int i=0; i<9;i+)int ii=(int)isbn.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) + cc; System.out

30、.println(isbn_0); catch(Exception e) e.printStackTrace();201412-03 /集合竞价package demo;import java.text.DecimalFormat;import java.util.Arrays;import java.util.Comparator;import java.util.Scanner;class StockArray String SBC; float price; int number;public class Test14 public static void main(String arg

31、s) System.out.println("please input data"); Scanner scanner=new Scanner(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(&

32、quot;sell") ssnum.price=scanner.nextFloat(); ssnum.number=scanner.nextInt(); else if (ssnum.SBC.equals("cancel") ssscanner.nextInt().SBC="CANCEL" /如果取消,则将相应行标记为CANCEL num+; System.out.println("aaa"); StockArray n1=new StockArraynum; /买方 for (int i = 0; i < num;

33、i+) n1i=new StockArray(); StockArray n2=new StockArraynum; /卖方 for (int i = 0; i < num; i+) n2i=new StockArray(); int num1=0; int num2=0; int ans_num=0; float ans_price=0; for(int i=1;i<num;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,0,num1,new MyComprator1(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论