
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Code for Calculation of a Physics Experiment
Suppose, we are performing a physics experiment. We are given n pairs of values and a threshold value k. Each the first value of the pair is added to a total value and the second value of the pair is also added to another total value. Now, we check if a total value is minimum or the (k - total) value is minimum. We do this for both the totals and then add them and print the output.
So, if the input is like n = 4, k = 20, values = {{3, 5}, {4, 3}, {2, 1}, {4, 4}}, then the output will be 14.
Steps
To solve this, we will follow these steps −
a := 0, b = 0 for initialize i := 0, when i < n, update (increase i by 1), do: a := a + first value of values[i] b := b + second value of values[i] print(min((a, k - a) + minimum of b and k - b))
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, int k, vector<pair<int,int>> values) { int a = 0, b = 0; for(int i = 0; i < n; i++){ a += values[i].first; b += values[i].second; } cout<<min(a, k - a) + min(b, k - b); } int main() { int n = 4, k = 20; vector<pair<int,int>> values = {{3, 5}, {4, 3}, {2, 1}, {4, 4}}; solve(n, k, values); return 0; }
Input
4, 20, {{3, 5}, {4, 3}, {2, 1}, {4, 4}}
Output
14
Advertisements