F - Triangles

本文介绍了一个使用C++实现的算法,该算法能够判断一个整数数组是否可以被等分为三个部分,且每个部分的总和相等。通过运用map来记录前缀和出现的次数,此算法能在O(n)的时间复杂度内完成任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
map<int,int>vis;
int main()
{
int n,m,a[100001]={0},j,temp=0,i,k,pre[100001]={0};
cin>>n;
int sum=0;
for(i=1;i<=n;i++)
{
    cin>>a[i];
    pre[i]=a[i]+pre[i-1];
    vis[pre[i]]++;
    sum+=a[i];
}
if(sum%3!=0)
    cout<<0<<endl;
else
{
    int ans=sum/3;
    int count=0,temp=0,cnt=0;
 for(i=1;i<=n;i++)
 {
         if(vis[ans+pre[i]]&&vis[ans*2+pre[i]]) cnt++;
}
 cout<<cnt<<endl;
}

return 0;
}

 

转载于:https://www.cnblogs.com/shengge-777/p/9470368.html

https://blog.youkuaiyun.com/Wang_Dou_Dou_/article/details/121065437 根据网页中的代码,使用qt中相关函数实现相同功能,在窗口中画出对应3D图像,一定要理解网页中对应的代码!!! class Point_Light { public: Point_Light() { this->update(); } void Draw(Shader &shader) { glBindVertexArray(light_VAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); } ~Point_Light() { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); glDeleteVertexArrays(1, &light_VAO); glDeleteBuffers(1, &light_VBO); } private: GLuint light_VAO, light_VBO; void update() {GLfloat vertices_2[] = { // 坐标 // x、y、z 坐标 // color -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, -0.5f, -0.5f, -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f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, }; glGenVertexArrays(1, &light_VAO); glGenBuffers(1, &light_VBO); glBindVertexArray(light_VAO); glBindBuffer(GL_ARRAY_BUFFER, light_VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_2), vertices_2, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); } }; 着色器:class Shader { private: GLuint vertex, fragment; public: GLuint Program; Shader(const GLchar *vertexPath, const GLchar *fragmentPath) { string vertexCode; string fragmentCode; ifstream vShaderFile; ifstream fShaderFile; vShaderFile.exceptions(ifstream::badbit); fShaderFile.exceptions(ifstream::badbit); try { vShaderFile.open(vertexPath); fShaderFile.open(fragmentPath); stringstream vShaderStream, fShaderStream; vShaderStream << vShaderFile.rdbuf(); fShaderStream << fShaderFile.rdbuf(); vShaderFile.close(); fShaderFile.close(); vertexCode = vShaderStream.str(); fragmentCode = fShaderStream.str(); } catch (ifstream::failure e) { cout << “ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ” << endl; } const GLchar *vShaderCode = vertexCode.c_str(); const GLchar *fShaderCode = fragmentCode.c_str(); vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vShaderCode, NULL); glCompileShader(vertex); GLint flag; GLchar infoLog[512]; glGetShaderiv(vertex, GL_COMPILE_STATUS, &flag); if (!flag) { glGetShaderInfoLog(vertex, 512, NULL, infoLog); cout << “ERROR::SHADER::VERTEX::COMPILATION_FAILED\n” << infoLog << endl; } fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fShaderCode, NULL); glCompileShader(fragment); glGetShaderiv(fragment, GL_COMPILE_STATUS, &flag); if (!flag) { glGetShaderInfoLog(fragment, 512, NULL, infoLog); cout << “ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n” << infoLog << endl; } this->Program = glCreateProgram(); glAttachShader(this->Program, vertex); glAttachShader(this->Program, fragment); glLinkProgram(this->Program); if (!flag) { glGetProgramInfoLog(this->Program, 512, NULL, infoLog); cout << “ERROR::SHADER::PROGRAM::LINKING_FAILED\n” << infoLog << endl; } glDeleteShader(vertex); glDeleteShader(fragment); } ~Shader() { glDetachShader(this->Program, vertex); glDetachShader(this->Program, fragment); glDeleteShader(vertex); glDeleteShader(fragment); glDeleteProgram(this->Program); } void Use() { glUseProgram(this->Program); } }; 观察相机:enum Camera_Movement { // 枚举类型 FORWARD, // 向前 BACKWARD, // 向后 LEFT, // 向左 RIGHT, // 向右 UPWARD, // 向上 DOWNWARD // 向下 }; const GLfloat SPEED = 6.0f; // 初始速度 class Camera { public: // 构造函数 Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3 target = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f)) :movementSpeed(SPEED) { this->position = position; this->camera_Z_axis = target; this->camera_Y_axis = up; this->camera_X_axis = glm::normalize(glm::cross(this->camera_Z_axis, this->camera_Y_axis)); this->updateCameraVectors(); // 实时更新 } // 观察矩阵 glm::mat4 GetViewMatrix() { return glm::lookAt(this->position, this->position + this->camera_Z_axis, this->camera_Y_axis); } void ProcessKeyboard(Camera_Movement direction, float deltaTime) { float velocity = this->movementSpeed * deltaTime; if (direction == FORWARD) this->position += this->camera_Z_axis * velocity; if (direction == BACKWARD) this->position -= this->camera_Z_axis * velocity; if (direction == LEFT) this->position -= this->camera_X_axis * velocity; if (direction == RIGHT) this->position += this->camera_X_axis * velocity; if (direction == UPWARD) this->position += this->camera_Y_axis * velocity; if (direction == DOWNWARD) this->position -= this->camera_Y_axis * velocity; } glm::vec3 GetPosition() // 下一篇文章使用 { return this->position; } private: // 摄影机的属性 glm::vec3 position; // 相机当前位置 glm::vec3 camera_Z_axis; // 摄影机的 Z 轴向量 glm::vec3 camera_X_axis; // 摄影机的 X 轴向量 glm::vec3 camera_Y_axis; // 摄影机的 Y 轴向量 GLfloat movementSpeed; // 镜头移动速度 void updateCameraVectors() { this->camera_Z_axis = glm::normalize(this->camera_Z_axis); this->camera_X_axis = glm::normalize(glm::cross(this->camera_Z_axis, this->camera_Y_axis)); this->camera_Y_axis = glm::normalize(glm::cross(this->camera_X_axis, this->camera_Z_axis)); } }; Shader ourShader = Shader(“F:/test/OpenGLTest/shaders/shader_v.txt”, “F:/test/OpenGLTest/shaders/shader_f.txt”); Shader lightShader = Shader(“F:/test/OpenGLTest/shaders/light_v.txt”, “F:/test/OpenGLTest/shaders/light_f.txt”); 主函数的代码如下:float vertices_1[] = { // x、y、z 坐标 // color // normal -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, // red 红色面 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // green 绿色面 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.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 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // cyan 青蓝色面 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f }; const glint width = 600, height = 600; bool keys[1024]; // 专门存储按过的键 camera camera(glm::vec3(1.0f, 1.0f, -5.0f), glm::vec3(-1.0f, -1.0f, 5.0f), glm::vec3(0.0f, 1.0f, 0.0f)); void keycallback(glfwwindow* window, int key, int scancode, int action, int mode); void key_movement(); void square_movement(glfloat&, glfloat&, glfloat&); glfloat deltatime = 0.0f; glfloat lasttime = 0.0f; glm::vec3 lightpos = glm::vec3(0.0f, 0.0f, 1.0f); const double shift_pix = 0.0005; // 正方体移动速度 int main() { /* 初始化 glfw */ glfwinit(); glfwwindowhint(glfw_resizable, gl_false); // 缩放关闭 /* 窗口捕获与处理 */ glfwwindow* window_1 = glfwcreatewindow(width, height, "learn opengl light test", nullptr, nullptr); int screenwidth_1, screenheight_1; glfwgetframebuffersize(window_1, &screenwidth_1, &screenheight_1); glfwmakecontextcurrent(window_1); glfwsetkeycallback(window_1, keycallback); /* 初始化 glew + 光照生成 */ glewinit(); point_light my_light = point_light(); /* 深度测试开启 */ glenable(gl_depth_test); /* 将我们自己设置的着色器文本传进来 */ shader ourshader = shader("f:/test/opengltest/shaders/shader_v.txt", "f:/test/opengltest/shaders/shader_f.txt"); // 相对路径 shader lightshader = shader("f:/test/opengltest/shaders/light_v.txt", "f:/test/opengltest/shaders/light_f.txt"); // 相对路径 /* 设置顶点缓冲对象(vbo) + 设置顶点数组对象(vao) */ gluint vao, vbo; glgenvertexarrays(1, &vao); glgenbuffers(1, &vbo); glbindvertexarray(vao); glbindbuffer(gl_array_buffer, vbo); glbufferdata(gl_array_buffer, sizeof(vertices_1), vertices_1, gl_static_draw); /* 设置链接顶点属性 */ glvertexattribpointer(0, 3, gl_float, gl_false, 9 * sizeof(glfloat), (glvoid*)0); glenablevertexattribarray(0); // 通道 0 打开 glvertexattribpointer(1, 3, gl_float, gl_false, 9 * sizeof(glfloat), (glvoid*)(3 * sizeof(glfloat))); glenablevertexattribarray(1); // 通道 1 打开 glvertexattribpointer(2, 3, gl_float, gl_false, 9 * sizeof(glfloat), (glvoid*)(6 * sizeof(glfloat))); glenablevertexattribarray(2); // 通道 2 打开 glfloat up_down_move = 0.0f; // 上下移动的变量 glfloat left_right_move = 0.0f; // 左右移动的变量 glfloat front_back_move = 0.0f; // 前后移动的变量 /* draw loop 画图循环 */ while (!glfwwindowshouldclose(window_1)) { /* 时间获取 */ glfloat currenttime = glfwgettime(); deltatime = currenttime - lasttime; lasttime = (float)currenttime; /* 视口 + 键鼠捕获 */ glviewport(0, 0, screenwidth_1, screenheight_1); glfwpollevents(); // 获取键盘鼠标 key_movement(); // 获取键盘 square_movement(up_down_move, left_right_move, front_back_move); // 正方体移动 /* 渲染 + 清除颜色缓冲 */ glclea 可以使用qt相关方式修改自定义着色器,也可以使用QT相关函数替代相关功能,去除自定义着色器,只有相关功能可以实现,之前着色器时从文件中读取的,依旧需要保留从文件中读取,请给出实现后的完整代码以及详细的中文注释,头文件和源文件需要分开,不同类在不同文件,不要省略代码,functions()函数不存在
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值