学习笔记-动态规划-poj1050-修改中

//
//  main.c
//  poj1050
//
//  Created by 韩雪滢 on 10/13/16.
//  Copyright © 2016 韩雪滢. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 10000

/* poj1050 失败的小demo
 *其中包括
 *读取一行字符串直到换行符
 *将字符串按照空格拆分,将拆分的char[] 专为 int,正负整数
 *用了一丢丢动态规划去求最大的值
 ***********************
 *未实现的是  题目要求These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc.
 也就是说,是连续的
 */

int arraySize = 0;
int max = 0;

int getLine(char s[],int lim)
{
    int c,i;
    for(i = 0;i < lim-1 && (c=getchar()) != '\n' ;i++){
        s[i] = c;
    }
    
    //如果for结束是因为换行符
    if(c == '\n'){
        s[i] = c;
        ++i;
    }
    
    s[i] = '\0';//字符串结束符
    return i;//返回字符串的真实长度
}

void cutToInt(char s[],int rlen){
    int i;
    char sub[rlen];
    int sublen = 0;
    for(i=0;i<rlen+1;i++){
        
        if(s[i] != ' ' && s[i] != '\0'){
            sub[sublen] = s[i];
            sublen++;
        }else{
            sub[sublen] = '\0';
            
            int num;
            
            if(sub[0] == '-'){
                char newSub[sublen-1];
                int k;
                for(k=0;k<sublen-2;k++)
                {
                    newSub[0] = sub[k+1];
                }
                newSub[sublen-2] = '\0';
                
                num = (-1)*atoi(newSub);
            }else{
                num = atoi(sub);
            }
            
            
            max = (max > (max+num))?max:(max+num);
            
            arraySize++;
            
            //清空sub
            int j;
            for(j=0;j<sublen;j++){
                sub[j] = '\t';
            }
            sublen = 0;
        }
    }
}

int main() {
    
    int n;
    char c;
    scanf("%d%c",&n,&c);
    
    while(arraySize < n*n){
        
        char line[MAXLEN];
        int realLen = getLine(line, MAXLEN);
        
        if(realLen > 0){
            cutToInt(line, realLen);
        }

    }
    
    printf("%d",max);
    
    return 0;
}

 

//
//  answer.cpp
//  poj1050
//
//  Created by 韩雪滢 on 10/13/16.
//  Copyright © 2016 韩雪滢. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define maxn 105
#define inf 0x3f3f3f3f

int n, array[maxn][maxn];
int f[maxn][maxn][maxn];

//之前的代码证明C没学好
void input()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            scanf("%d", &array[i][j]);
}


//我未实现的部分:确保是某个子矩阵的和为最大,先 按列i 加 ,再 按行j 加,存储在(列的个数)个矩阵中,行数还是i,列数是k
//有重复计算,三维矩阵可优化
int work()
{
    memset(f, 0, sizeof(f));
    int ret = -inf;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            int sum = 0;
            for (int k = j; k <= n; k++)
            {
                sum += array[i][k];
                f[i][j][k] = max(f[i - 1][j][k] + sum, sum);
                ret = max(ret, f[i][j][k]);
            }
        }
    return ret;
}

int main()
{
    input();
    printf("%d\n", work());
    return 0;
}

 

posted @ 2016-10-13 21:01  ShellHan  阅读(232)  评论(0编辑  收藏  举报