首页 > 其他 > 详细

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

时间:2016-10-13 21:40:07      阅读:256      评论:0      收藏:0      [点我收藏+]
//
//  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;
}

 

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

原文:http://www.cnblogs.com/HackHer/p/5958131.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!