zoukankan      html  css  js  c++  java
  • 回文

    [实验任务二]:递归方法
    (1)    使用递归方式判断某个字串是否是回文( palindrome );
    “回文”是指正着读、反着读都一样的句子。比如“我是谁是我”
    使用递归算法检测回文的算法描述如下:
    A single or zero-character string is a palindrome.
    Any other string is a palindrome if the first and last characters are the same, and the string that remains, excepting those characters, is a palindrome.

    package palindrome;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    /*
     * 使用递归方式判断某个字串是否是回文( palindrome )
     * 从文件中读入
     */
    public class palindrome {
        
        public static void main(String[] args) throws IOException {
            String url = "test.txt";
            File file = new File(url);// 指定操作文件
            if (!file.exists() || file.isDirectory()) {
                System.out.println("文件不存在!");
                return;
            } else
                System.out.println("文件导出成功");
            StringBuffer sb0 = new StringBuffer();
            BufferedReader br;
            try {
                br = new BufferedReader(new FileReader(file));
                String temp = null;
                //sb = new StringBuffer();
                temp = br.readLine();
                while (temp != null) {
                    sb0.append(temp + " ");
                    temp = br.readLine();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            String sb = sb0.toString();
            boolean flag = find(sb,0,sb.length());
           
            System.out.println(flag);
        }
     
        private static boolean find(String sb, int start, int length) {
           
            if(length<=1)
                return true;
            else if(sb.toCharArray()[start]==sb.toCharArray()[length-1]){
                return find(sb,start+1,length-1);
            }
            return false;
        }
    }
    自我抑郁又自我救赎
  • 相关阅读:
    URLProtocol服务协议
    ODBC、OLEDB和ADO之间的关系 ,以及性能比较
    如何在VS2015查看C#界面窗体里的控件层次
    SpeechVoiceSpeakFlags枚举类型的详细解释
    SQL中遇到多条相同内容只取一条的最简单实现方法
    flink elasticsearch sink table 忽略部分字段开发
    flink elasticsearch source table 集成elasticsearch-hadoop connector开发
    记一次python 协程给合多线程死锁问题
    kubernetes gitlab runner java maven ci/cd 整体方案示例
    某云elasticsearch节点失效,手动重置primary,迁移分区
  • 原文地址:https://www.cnblogs.com/zjm15511858030/p/9788767.html
Copyright © 2011-2022 走看看