zoukankan      html  css  js  c++  java
  • 和为0的最长子序列

    和为0的最长子序列问题

    注意:

    1、输入格式 1 2 3 4 5 6  

    以回车结束,应该怎么读取? 考虑两点:回车结束判断 和 数组长度未定义?

    while(temp = cin.get()!=' ')

    {

      cin.unget();

      cin >> temp;

      vector.pushback(temp);

    }

    2、输入

    // ConsoleApplication11.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <iostream>
    #include <stdio.h>
    #include <vector>

    using namespace std;
    vector<int> vec;


    int _tmain(int argc, _TCHAR* argv[])
    {


    int temp = 0;
    int count = 0;
    vec.clear();
    while ((temp = cin.get()) != ' ')
    {
    cin.unget();
    cin >> temp;
    vec.push_back(temp);
    }
    int thisSum = 0;
    int l = 0;
    int r = 0;
    int i = 0, j = 0;

    for (int i = 0; i < vec.size(); i++)
    {

    thisSum = vec[i];
    for (int j = i+1; j < vec.size(); j++)
    {
    thisSum += vec[j];
    if (thisSum == 0 && (r - l) < (j - i))
    {
    l = i;
    r = j;
    }
    }

    }

    cout << l << " " << r << endl;

    for (int k = l; k <= r; k++)
    {
    cout << vec[k];
    }

    }

    // ConsoleApplication11.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <stdio.h>
    #include <vector>
    
    using namespace std;
    vector<int> vec;
    
     int column = 0;
     int Char2Int(char *a, int n);
     char LastChar(char *a, int n);
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        
        char temp[10];
        int a;
        int count = 0;
        while ((a = cin.get()) != '
    ')
        {
            cin.unget();
            cin >> temp;
    
            vec.push_back(Char2Int(temp,8));
            if (LastChar(temp, 10) == ';' && count == 0)
            {
                count = 1;
                column = vec.size();
            }
    
            
        }
        
        cout << column << "	" << vec.size() / column;
        for (int i = 0; i < vec.size(); i++)
        {
            if (i%column == 0)
            {
                cout << endl;
            }
            cout << vec[i] << "	";
    
    
        }
    
    
        
    
    }
    
    int Char2Int(char *a, int n)
    {
        if (a == NULL)
        {
            return 0;
        }
        int i = 0;
        int result = 0;
        while (a[i] != NULL)
        {
            if (a[i] != ';')
            {
                result *= 10;
                result += a[i]-'0';
                
            }
            i++;
        
        
        }
        return result;
    
    }
    
    char LastChar(char *a, int n)
    {
        int i = 0;
        int j = 0;
        while (a[i] != NULL)
        {
            j = i;
            i++;
        }
    
        return a[j];
    }
  • 相关阅读:
    MySQL · 引擎特性 · InnoDB崩溃恢复
    MySQL · 引擎特性 · InnoDB Buffer Pool
    MySQL · 引擎特性 · InnoDB IO子系统
    MySQL · 引擎特性 · InnoDB 同步机制
    docker基本操作命令
    IIS日志导出mysql
    Win10 MySQL Community Server 8.0.17安装
    win10 TortoiseGit 图标不显示
    window环境配置nginx
    windows openssh安装
  • 原文地址:https://www.cnblogs.com/wll-zju/p/4839509.html
Copyright © 2011-2022 走看看