我的C ++代码有问题。 [英] I have a problem with the C++ code.

查看:96
本文介绍了我的C ++代码有问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <bits/stdc++.h>
#include<iostream>
#include <vector>
using namespace std;
long long int n,q;
vector<int>arr;
vector<int>ans;
int main(){
    int i,j;
    cin>>n;
    cin>>q;
    for (int i = 0; i < n; ++i){
    cin>>arr[i];
    }
    int pos_val,l,r;
    for (int j = 0;j < q; ++j){
    cin>>pos_val>>l>>r;
    if (pos_val == 1){
        arr[l-1]=r;
    }
    else if (pos_val == 2){
        vector<int>arr2 = arr;
        int N = sizeof(arr2) / sizeof(int);
        ans.push_back(isPossibleTriangle(arr2, N));
        }
    }
    int p;
    for (p=0;p<=q;p++){
        cout<<ans[p];
    }
    return 0;
}
int isPossibleTriangle(vector<int>arr, int N){
    if (N < 3)
            return false;
    int cnt = 0;
    sort(arr.begin(),arr.end());;
    for (int i=0; i<N-2; i++){
        if (arr[i] + arr[i+1] > arr[i+2]){
            if (cnt<(arr[i] + arr[i+1] + arr[i+2])){
                cnt += (arr[i] + arr[i+1] + arr[i+2]);
            }
        }
    }
    return cnt;
}







示例:

输入格式:(半冒号意味着换行符)






Example:
Input Format:(Semi colon means newline)

5 4   \n;
3 1 8 9 7   \n;
2 1 5   \n;
1 2 12   \n;
2 1 3   \n;
2 2 5   \n;

Output:
24   \n;
0   \n;
29   \n;





第一个查询要求我们使用一些元素找到三角形的最大周长{3,1,8,9,7}。我们可以采用元素7,8和9,并使用这三个元素作为边长构建三角形。该三角形的周长为7 + 8 + 9 = 24,这是可能的最大周长。因此答案是24.



第二个查询要求我们将第二个元素更改为12.



第三个查询要求我们使用一些元素{3,12,8}找到三角形的最大周长。这三个元素不形成三角形,因此答案是0.



第四个查询要求我们使用一些元素找到三角形的最大周长{ 12,8,9,7}。我们可以使用元素8,9和12,并使用这三个元素作为边长来形成三角形。该三角形的周长为8 + 9 + 12 = 29,这是最大可能的。因此答案是29。



我尝试过:



我是一个python编码器。我用c ++解决了一个问题,我遇到了问题。请帮助我。



The first query asks us to find the maximum perimeter of a triangle using some of the elements {3, 1, 8, 9, 7}. We can take the elements 7, 8, and 9 and construct a triangle using these three elements as side lengths. This triangle has perimeter 7 + 8 + 9 = 24, which is the maximum possible perimeter. Hence the answer is 24.

The second query asks us to change the second element to 12.

The third query asks us to find the maximum perimeter of a triangle using some of the elements {3, 12, 8}. These three elements do not form a triangle, hence the answer is 0.

The fourth query asks us to find the maximum perimeter of a triangle using some of the elements {12, 8, 9, 7}. We can take the elements 8, 9 and 12 and make a triangle using these three elements as side lengths. This triangle has perimeter 8 + 9 + 12 = 29, which is maximum possible. Hence the answer is 29.

What I have tried:

I am a python coder.I was solving a question in c++ and i am having problems.Please help me.

推荐答案

这看起来像家庭作业,但我会提供评论。



i和j的重新定义

要么像在C中那样在顶部声明它,要么在for中声明它(int i = 0 ; ...),无需同时执行这两项操作。



您需要在尝试调用函数之前提供函数原型,否则编译器将无法识别它在通话时间。

int isPossibleTriangle(vectorarr,int N);



向量是动态数组,你不能简单地将它们索引到它们中首先向他们添加项目。



我对下面的代码做了一些修改以修复错误和其他一些事情。现在你至少有一些可以编译的东西,可能不会抛出任何异常。



玩得开心:



This looks like home work, but I will provide a review.

Redefinition of i and j
Either declare it at the top like in C or in the for(int i = 0;...), no need to do both.

You need to provide a prototype for a function before you try to call it, or the complier will not recognize it at the call sight.
int isPossibleTriangle(vectorarr, int N);

Vectors are dynamic arrays you cannot simply index into them without first adding items to them.

I made some changes to the code below to fix the errors and a few other things. Now you at least have something that will compile and probably not throw any exception.

Have fun :)

#include<iostream>
#include <vector>
#include <algorithm>

using namespace std;

int isPossibleTriangle(vector<int>& arr);

long int n,q;
vector<int>arr;
vector<int>ans;

int main()
{
    cin >> n;
    cin >> q;
    for (int i = 0; i < n; ++i)
	{
		int value;
		cin >> value;
		arr.push_back(value);
    }

    int pos_val,l,r;
    for (int j = 0;j < q; ++j)
	{
		cin >> pos_val >> l >> r;
		if (pos_val == 1)
		{
			arr[l-1] = r;
		}
		else if (pos_val == 2)
		{
			ans.push_back(isPossibleTriangle(arr));
        }
    }
    for (int p = 0; p < ans.size(); ++p)
	{
        cout << ans[p] << endl;
    }
    return 0;
}

int isPossibleTriangle(vector<int>& arr)
{
    int cnt = 0;
    if (arr.size() > 2)
	{
		sort(arr.begin(),arr.end());
		for (int i=0; i<arr.size()-2; ++i)
		{
			if (arr[i] + arr[i+1] > arr[i+2])
			{
				if (cnt<(arr[i] + arr[i+1] + arr[i+2]))
				{
					cnt += (arr[i] + arr[i+1] + arr[i+2]);
				}
			}
		}
	}
    return cnt;
}


这篇关于我的C ++代码有问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆