HDOJ 1003 Max Sum 线性DP 神奇的输出格式 java

🍑 算法题解专栏


🍑 HDOJ 1003 Max Sum

在这里插入图片描述
输入

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

输出

Case 1:
14 1 4

Case 2:
7 1 6

🙈 输出换行优先使用 println
⭐ \n:回车的转义符
⭐ %d:格式化字符( 用于printf )

🤠 线性DP + 神奇的输出
🍑 f[ i ] 表示以第 i 个数结尾的最大和
🥚 关于 %n 格式

🍑 代码实现

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
	static int N = 100010;
	static int[] a = new int[N];
	static int[] f = new int[N];

	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int i = 1; i <= T; i++)
		{
			int n = sc.nextInt();
			for (int j = 1; j <= n; j++)
				a[j] = sc.nextInt();
			int r = 1;
			Arrays.fill(f, 0);
			for (int j = 1; j <= n; j++)
			{
				if (f[j - 1] > 0)
					f[j] = a[j] + f[j - 1];
				else
				{
					f[j] = a[j];
				}
				if (f[j] > f[r])
				{
					r = j;
				}

			}
//			System.out.println(res + " " + idx);
// 			System.out.print("Case " + i + ":\n" + f[r] + " ");
 
			int l = r-1;
            while(l >= 1 && f[l] >= 0)
            {
                l--;
            }
            
            /*
            //输出格式测试
            
            System.out.println("版本1");
            System.out.printf("Case %d:\n%d %d %d\n",i,f[r],l+1,r);   //PE 
            // System.out.printf("Case %d:%n%d %d %d%n",i,f[r],l+1,r);   //PE
            
            System.out.println("版本2");
            System.out.print("Case " + i + ":\n" + f[r] + " " + (l+1) + " " + r + "\n"); //PE
            
            System.out.println("版本3");
            */
            //AC1
            System.out.println("Case " + i + ":");
			System.out.println(f[r] + " " + (l+1) + " " + r) ;

			//AC2
			// System.out.printf("Case %d:%n",i); 
            // System.out.printf("%d %d %d%n",f[r],l+1,r);
			if(i != T)
				System.out.println();
		}
	}
}

🍑 输出格式测试

版本1
Case 1:
14 1 4
版本2
Case 1:
14 1 4
版本3
Case 1:
14 1 4

版本1
Case 2:
7 1 6
版本2
Case 2:
7 1 6
版本3
Case 2:
7 1 6

🍑 参考大佬的版本(原文找不到了)

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
	static int N = 100010;
	static int[] f = new int[N];
	static int[] a = new int[N];

	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int i = 1; i <= T; i++)
		{
			int n = sc.nextInt();
			for (int j = 1; j <= n; j++)
				a[j] = sc.nextInt();

			Arrays.fill(f, 0);
			int l = 1;
			int r = 1;
			int start = 1;
			int max = a[1];
			f[1] = a[1];
			for (int j = 2; j <= n; j++)
			{
				if (f[j - 1] >= 0)
					f[j] = f[j - 1] + a[j];
				else
				{
					f[j] = a[j];
//					顺便更新起点
					start = j;// 抛弃 j 前边的所有数
				}
				if (f[j] > max)
				{
					max = f[j];
					l = start;
					r = j;
				}
			}

			System.out.println("Case " + i + ":");
			System.out.println(max + " " + l + " " + r) ;

			if (i != T)
				System.out.println();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值