C++ and Go Code for Point Location Check: Efficient Algorithm Implementation
{ "title": "C++ and Go Code for Point Location Check: Efficient Algorithm Implementation", "description": "This code snippet demonstrates how to efficiently check if a given point (x, y) lies within a rectangular region defined by its bottom-left corner (a, b) and dimensions (g, k) using both C++ and Go programming languages. Learn about the implementation, optimizations, and how to find the region containing the point.", "keywords": "C++, Go, programming, algorithm, point location, rectangle, region, coordinates, optimization, code snippet", "content": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n long long n,i,a[100010],b[100010],g[100010],k[100010];\n cin>>n;\n for(i=1;i<=n;i++)\n {\n cin>>a[i]>>b[i]>>g[i]>>k[i];\n }\n int x,y,sum=0;\n cin>>x>>y;\n for(i=1;i<=n;i++)\n {\n if(x<=a[i]+g[i]&&x>=a[i]&&y<=b[i]+k[i]&&y>=b[i])\n {\n sum=i;\n }\n }\n if(!sum) cout<<"-1";\n else cout<<sum;\n return 0;\n}//转换为Go代码内容:package main\n\nimport "fmt"\n\nfunc main() {\n var n, x, y int\n fmt.Scanf("%d", &n)\n a := make([]int, n+1)\n b := make([]int, n+1)\n g := make([]int, n+1)\n k := make([]int, n+1)\n for i := 1; i <= n; i++ {\n fmt.Scanf("%d %d %d %d", &a[i], &b[i], &g[i], &k[i])\n }\n fmt.Scanf("%d %d", &x, &y)\n sum := 0\n for i := 1; i <= n; i++ {\n if x <= a[i]+g[i] && x >= a[i] && y <= b[i]+k[i] && y >= b[i] {\n sum = i\n }\n }\n if sum == 0 {\n fmt.Println("-1")\n } else {\n fmt.Println(sum)\n }\n}
原文地址: https://www.cveoy.top/t/topic/puUN 著作权归作者所有。请勿转载和采集!