问题描述
importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjava.util.Scanner;publicclassReverseComplement{publicstaticvoidmain(String[]args)throwsIOException{System.out.println("请输入组数:");Scannerscan=newScanner(System.in);inti=Integer.parseInt(scan.nextLine());List<String>list=newArrayList<String>();for(intt_=0;t_<i;t_++){System.out.println("请输入第"+(t_+1)+"组:");StringBuffersb=newStringBuffer();chart=(char)(System.in.read());while(t!='n'&&t!='r'){sb.append((char)t);t=(char)(System.in.read());}//window系统t=(char)(System.in.read());list.add(sb.toString());}System.out.println("输出样例:");for(i=0;i<list.size();i++){reverseComple(list.get(i));}}/***反转串方法**@paraminputStr*/privatestaticvoidreverseComple(StringinputStr){charb[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};StringreverseStr="";//反转串for(inti=0;i<inputStr.length();i++){for(intj=0;j<b.length;j++){//查询出对应的反转字符if(inputStr.charAt(i)==b[j]){reverseStr+=b[(j+13)%26];break;}}}//System.out.println("原串============"+inputStr);//System.out.println("反转串============"+reverseStr);//查询互补子串intretLength=0;for(inti=0;i<reverseStr.length();i++){//反转串intrePos=i;//反转串遍历开发位置inttempLength=0;//此次查找反转串长度intmodCnt=2;//反转串允许剔除字符数量intePos=reverseStr.length();for(intj=ePos-1;j>=rePos;j--){//原串if(reverseStr.charAt(rePos)==inputStr.charAt(j)){//反转串向后走,原串向前走tempLength++;rePos++;//System.out.println("反转串向后走,原串向前走="+a.charAt(j));}elseif(tempLength>0&&modCnt>0){//原串向前走modCnt--;//System.out.println("//原串向前走="+a.charAt(j));}elseif(modCnt<=0){//原串向前查找,寻找下一个匹配位置重新计数if(retLength<tempLength){retLength=tempLength;}rePos=i;tempLength=0;modCnt=2;j=--ePos;//System.out.println("原串向前查找,寻找下一个匹配位置重新计数="+a.charAt(j)+"=====rePos"+rePos+"=====ePos="+ePos);}if(retLength<tempLength){retLength=tempLength;}}}System.out.println(retLength);}}
解决方案
解决方案二:
packageJUnit;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjava.util.Scanner;publicclassReverseComplement{publicstaticvoidmain(String[]args)throwsIOException{System.out.println("请输入组数:");Scannerscan=newScanner(System.in);inti=Integer.parseInt(scan.nextLine());List<String>list=newArrayList<String>();for(intt_=0;t_<i;t_++){System.out.println("请输入第"+(t_+1)+"组:");StringBuffersb=newStringBuffer();chart=(char)(System.in.read());while(t!='n'&&t!='r'){sb.append((char)t);t=(char)(System.in.read());}//window系统t=(char)(System.in.read());list.add(sb.toString());}System.out.println("输出样例:");for(i=0;i<list.size();i++){reverseComple(list.get(i));}}/***反转串方法**@paraminputStr*/privatestaticvoidreverseComple(StringinputStr){charb[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};StringreverseStr="";//反转串for(inti=0;i<inputStr.length();i++){for(intj=0;j<b.length;j++){//查询出对应的反转字符if(inputStr.toUpperCase().charAt(i)==b[j]){reverseStr+=b[(j+13)%26];break;}}}//System.out.println("原串============"+inputStr);//System.out.println("反转串============"+reverseStr);//查询互补子串intretLength=0;for(inti=0;i<reverseStr.length();i++){//反转串intrePos=i;//反转串遍历开发位置inttempLength=0;//此次查找反转串长度intmodCnt=2;//反转串允许剔除字符数量intePos=reverseStr.length();for(intj=ePos-1;j>=rePos;j--){//原串if(reverseStr.charAt(rePos)==inputStr.charAt(j)){//反转串向后走,原串向前走tempLength++;rePos++;//System.out.println("反转串向后走,原串向前走="+a.charAt(j));}elseif(tempLength>0&&modCnt>0){//原串向前走modCnt--;//System.out.println("//原串向前走="+a.charAt(j));}elseif(modCnt<=0){//原串向前查找,寻找下一个匹配位置重新计数if(retLength<tempLength){retLength=tempLength;}rePos=i;tempLength=0;modCnt=2;j=--ePos;//System.out.println("原串向前查找,寻找下一个匹配位置重新计数="+a.charAt(j)+"=====rePos"+rePos+"=====ePos="+ePos);}if(retLength<tempLength){retLength=tempLength;}}}System.out.println(reverseStr);}}
两个主要问题:1是反转字符你只定义了大写的,这样的话需要对原输入进行大小写转换,2是你最后output打印的string变量名搞错了细心点,提交前多调试即可