add prob 10

This commit is contained in:
2025-08-22 00:22:53 +08:00
parent eb2426e4de
commit febe7547bf
5 changed files with 96 additions and 63 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
*.exe *.exe
*.out

View File

@@ -1,19 +1,12 @@
{ {
"configurations": [ "configurations": [
{ {
"name": "Win32", "name": "Linux",
"includePath": [ "includePath": [
"${workspaceFolder}/**" "${workspaceFolder}/**"
], ],
"defines": [ "defines": [],
"_DEBUG", "compilerPath": "/usr/bin/g++"
"UNICODE",
"_UNICODE"
],
"compilerPath": "E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
} }
], ],
"version": 4 "version": 4

49
.vscode/launch.json vendored
View File

@@ -1,29 +1,26 @@
{ {
// Use IntelliSense to learn about possible attributes. "version": "0.2.0",
// Hover to view descriptions of existing attributes. "configurations": [
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 {
"version": "0.2.0", "name": "g++ - Build and debug active file",
"configurations": [ "type": "cppdbg",
{ "request": "launch",
"name": "g++.exe - Build and debug active file", "program": "${fileDirname}/${fileBasenameNoExtension}.out",
"type": "cppdbg", "args": [],
"request": "launch", "stopAtEntry": false,
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "cwd": "${fileDirname}",
"args": [], "environment": [],
"stopAtEntry": false, "externalConsole": false,
"cwd": "${fileDirname}", "MIMode": "gdb",
"environment": [], "setupCommands": [
"externalConsole": false, {
"MIMode": "gdb", "description": "Enable pretty-printing for gdb",
"miDebuggerPath": "E:\\Applications\\MSYS2\\ucrt64\\bin\\gdb.exe", "text": "-enable-pretty-printing",
"setupCommands": [ "ignoreFailures": true
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
} }
] ],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
} }

52
.vscode/tasks.json vendored
View File

@@ -1,28 +1,28 @@
{ {
"version": "2.0.0", "tasks": [
"tasks": [ {
{ "type": "cppbuild",
"type": "cppbuild", "label": "C/C++: g++ build active file",
"label": "C/C++: g++.exe build active file", "command": "/usr/bin/g++",
"command": "E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe", "args": [
"args": [ "-fdiagnostics-color=always",
"-fdiagnostics-color=always", "-g",
"-g", "${file}",
"${file}", "-o",
"-o", "${fileDirname}/${fileBasenameNoExtension}.out"
"${fileDirname}\\${fileBasenameNoExtension}.exe" ],
], "options": {
"options": { "cwd": "${fileDirname}"
"cwd": "E:\\Applications\\MSYS2\\ucrt64\\bin" },
}, "problemMatcher": [
"problemMatcher": [ "$gcc"
"$gcc" ],
], "group": {
"group": { "kind": "build",
"kind": "build", "isDefault": true
"isDefault": true },
}, "detail": "Task generated by Debugger."
"detail": "compiler: E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe" }
} ],
] "version": "2.0.0"
} }

42
10-238.c Normal file
View File

@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
int *left = (int *)malloc(numsSize * sizeof(int));
int *right = (int *)malloc(numsSize * sizeof(int));
int par_prod = 1;
for (int i = 0; i < numsSize; ++i) {
par_prod *= nums[i];
left[i] = par_prod;
}
par_prod = 1;
for (int i = numsSize - 1; i >= 0; --i) {
par_prod *= nums[i];
right[i] = par_prod;
}
int *result = (int *)malloc(numsSize * sizeof(int));
result[0] = right[1];
result[numsSize - 1] = left[numsSize - 2];
for (int i = 1; i < numsSize - 1; ++i) {
result[i] = left[i - 1] * right[i + 1];
}
*returnSize = numsSize;
return result;
}
int main() {
int nums[] = {1, 2, 3, 4};
int result_size;
int *result = productExceptSelf(nums, sizeof(nums) / sizeof(int), &result_size);
for (int i = 0; i < result_size; ++i) {
printf("%d ", result[i]);
}
printf("\n");
free(result);
return 0;
}