小编tri*_*der的帖子

数组值未按预期显示

a[2][2]数组值的位置应-5根据以下输入

1
3
3
-2 -3 3
-5 -10 1
10 30 -5
Run Code Online (Sandbox Code Playgroud)

但是当我运行这段代码时,它显示的值为a[2][2]0,不知道为什么.我确信我没有更新数组中的值.

#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<math.h>
#define min(a,b) a<b?a:b

int m,n;
int func(int i,int j,int a[][101],int dp[][101])
{
 if(i>=m||j>=n)
{
    printf("i=%d j=%d intmax\n",i,j);
    return INT_MAX;
}
if(i==m-1&&j==n-1)
{

    if(a[i][j]<0)
    {
        printf("i=%d j=%d return abs a[i][j]%d\n",i,j,abs(a[i][j]));
        return abs(a[i][j]);
    }
    printf("i=%d j=%d a[i][j]=%d return 0\n",i,j,a[i][j]);
    return 0;
}
if(dp[i][j]!=-1)
{
    printf("returning dp=%d\n",dp[i][j]);
    return dp[i][j];
}
int t1=func(i+1,j,a,dp);
int t2=func(i,j+1,a,dp);
t1=min(t1,t2);
if(a[i][j]<0)
{
    dp[i][j]=t1+abs(a[i][j]);
}
else
{ …
Run Code Online (Sandbox Code Playgroud)

c arrays

1
推荐指数
1
解决办法
46
查看次数

如何访问嵌套在 stl 向量中的 pair 内的 pair 元素

我有一个这样的向量:

vector < pair < int, pair < int,int > > > v
Run Code Online (Sandbox Code Playgroud)

我想访问所有三个元素。我怎样才能通过迭代器做到这一点?我在下面将迭代器声明为 it1 和 it2:

#include <bits/stdc++.h>
using namespace std;
int main() 
{

int t;
scanf("%d",&t);
while(t--)
{
    vector<pair<int,pair<int,int> > > v;
    int n,a,b,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        v.push_back(make_pair(b,make_pair(a,i+1)));
    }
    sort(v.begin(),v.end());
    vector<pair<int,pair<int,int> > > :: iterator it1=v.begin();
    vector<pair<int,pair<int,int> > > :: iterator it2=v.begin()+1;
    printf("%d ",(it1->first)->second);

        while(it2!=v.end())
        {
            if(it2->first.first>it1.first)
            {
                printf("%d ",it2.first.second);
                it1=it2;

            }
            it2++;
        }

    }

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stl

-4
推荐指数
1
解决办法
4962
查看次数

标签 统计

arrays ×1

c ×1

c++ ×1

stl ×1