判断回文字符串与子集和问题
2012-06-27 14:45
537 查看
题目一:
题目二:
第一题:
第二题
题目二:
第一题:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Palindrome { int i,j; String s1,s2; public Palindrome(String es1,String es2){ //构造器 s1 = es1; s2 = es2; } public boolean judge(){ String es1=" "; String es2=" "; for(i=0;i<s1.length();i++){ char c = s1.charAt(i); es1+=c; } for(int j=(s2.length()-1);j>=0;j--){ char d = s2.charAt(j); es2+=d; } if (es1.equals(es2)){ System.out.println("It is a palindrome!"); return true; } else{ System.out.println("It is not a palindrome!"); return false; } } public static void main(String[] args){ // Hw_p1 h1=new Hw_p1("gabag", "gabag"); System.out.println("%java Palindrome\nEnter the word to test:"); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); try { String s=in.readLine(); Palindrome h1=new Palindrome(s, s); h1.judge(); } catch (IOException e) { e.printStackTrace(); } } }
第二题
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Set; import java.util.TreeSet; public class Groups { static ArrayList<String> list = new ArrayList<String>(); static Set<String> index = new TreeSet<String>(); static StringBuilder str; static StringBuilder indexStr; static int sum; static int startFrom; public static void groupNoAdj(int start,int[] nums,int target){ for(int i=0; i<nums.length; i++){ sum = nums[i]; str = new StringBuilder(); str.append(nums[i]); indexStr = new StringBuilder(); indexStr.append(i); for(int j=start; j<nums.length; j++){ if(i != j){ sum += nums[j]; if(sum == target){ str.append("+"+ nums[j]); indexStr.append("+"+ j); int size = index.size(); index.add(indexStr.toString()); if(index.size() > size) list.add(str.toString()); } if(sum < 10){ str.append("+"+ nums[j]); indexStr.append("+"+ j); } if(sum > 10){ sum -= nums[j]; } } } } } public static void main(String args[]){ System.out.println("%java Groups\nEnter the target:"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); String targets = null; try { targets = br1.readLine(); } catch (IOException e) { e.printStackTrace(); } int target=Integer.parseInt(targets); System.out.println("Enter the num of elements in the array:"); BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in)); String numofelements = null; try { numofelements = br2.readLine(); } catch (IOException e) { e.printStackTrace(); } int numOfElems=Integer.parseInt(numofelements); System.out.println("Enter the elements:"); BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in)); int []temp=new int[numOfElems]; for(int i=0;i<numOfElems;i++){ try { temp[i]=Integer.parseInt(br3.readLine()); } catch (Exception e) { e.printStackTrace(); } } for (int i=0; i<temp.length; i++){ groupNoAdj(i,temp,target); } System.out.println("We can make a group that sums to:"+target); } }
相关文章推荐
- 问题 D: 回文判断(函数)//回文字符串的快速算法//回文数的快速判断
- 数据结构常见问题(二)利用栈和队列判断字符串是否是回文
- 问题 C: 判断字符串是否为回文
- 编程判断字符串是否为回文 判断一个字符串是否是回文,例如单词‘level’
- 判断回文字符串.
- 在JSP页面中使用标签判断字符串长度的问题
- 6-2 判断回文字符串(20 分)
- 判断输入的字符串是否是回文
- 33判断字符串是否为回文
- 判断一个字符串是否为回文
- 127.判断字符串是否为回文
- 编程判断字符串是否为回文
- C 判断字符串是否是子集
- C语言 判断回文字符串
- 判断字符串是否是回文
- 简单判断字符串是否是回文
- 第十五周OJ(7)判断字符串是否为回文
- 回文字符串问题
- 回文字符串问题
- 判断字符串是否可以通过交换相邻字符得到回文字符串,如果可以输出最少交换次数