进制转换_统计回文_连续最大和

进制转换

进制转换
image-20220430102416951

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String result = "";//结果表
        String table = "0123456789ABCDEF";//进制表
        while(sc.hasNext()){
            int M = sc.nextInt();
            int N = sc.nextInt();
            if(M==0){
                result += "0";
            }
            while(M!=0){
                if(M<0){
                    M=-M;//负数!
                   System.out.print("-");
                }
                //字符串拼接, 后面的结果拼接在前!
                result = table.charAt(M%N) + result;
                M/=N;
            }
            System.out.println(result);
        }
    }
}
//代码优化!
//str1+str2+...strn 会产生n-1个临时对象,那么避免大量无效对象的创建,所以提倡用stringbuffer.append()避免产生无效临时变量
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        StringBuilder result = new StringBuilder();//结果表
        String table = "0123456789ABCDEF";//进制表
        while(sc.hasNext()){
            int M = sc.nextInt();
            int N = sc.nextInt();
            if(M==0){
               result.append(0);
            }
            while(M!=0){
                if(M<0){
                    M=-M;//负数!
                   System.out.print("-");
                }
                result.append(table.charAt(M%N));
                M/=N;
            }
            result.reverse();//逆置!
            System.out.println(result);
        }
    }
}

统计回文

统计回文

image-20220430115123006

import java.util.*;
public class Main{
    public static boolean isPalindrome1(StringBuilder str){
          StringBuilder tmp = new StringBuilder(str);
          tmp.reverse();
        //注意这里要先转成String 因为 StringBuilder中没有 equals 方法!
        return str.toString().equals(tmp.toString());
    }
    public static boolean isPalindrome(StringBuilder str){
        int l = 0,end = str.length()-1;
       while (l<end){
           if(str.charAt(l)!=str.charAt(end)){
               return false;
           }
           l++;
           end--;
       }
       return true;
    } 
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            StringBuilder str1 = new StringBuilder(sc.nextLine());
            String str2 = sc.nextLine();
            int count = 0;
            for(int i = 0;i<=str1.length();i++){
                StringBuilder tmp = new StringBuilder(str1);
                //在第i位置插入!
                if(isPalindrome(tmp.insert(i,str2))){
                    count++;//次数++
                }
            }
            System.out.println(count);
        }
    }
}

连续最大和

连续最大和

image-20220430130618786

// 经典dp问题
// 假设dp[n]表示以n为最后一个元素的子数组和的最大值,
// 因此, dp[n] = max(dp[n-1],0)+num[n];
// 当然实现的时候,没有必要设置一个数组保存所有的情况,因为只是用到了前一个位置的计算结果。
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = 0;
        while(sc.hasNext()){
            n = sc.nextInt();
            int[] num = new int[n];
            for(int i=0;i<n;i++){
                num[i] = sc.nextInt();
            }
            int max = num[0];
            for(int i=1;i<n;i++){//保存连续值! 
               num[i] = Math.max(num[i-1],0)+num[i];
                if(max<num[i]){
                    max=num[i];
                }
            }
            System.out.println(max);
        }
    }
}
全部评论
楼主有这些题的链接没
点赞 回复 分享
发布于 2022-08-21 15:41 陕西
little bit?
点赞 回复 分享
发布于 2022-11-07 23:32 四川

相关推荐

点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务