您的位置:首页 > 编程语言 > Java开发

判断回文字符串与子集和问题

2012-06-27 14:45 501 查看
题目一:



题目二:



第一题:

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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息