I was writing a code to sort and output the number by using "pair".
I tried some cases bat there was no output.
How should I rewrite the code?
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
int k, i, n;
cin >> n;
pair<int, int> a[n];
for (i = 0; i < n; i++) {
cin >> k;
a[i].first = -k;
a[i].second = i + 1;
}
sort(a, a + n);
for (i = 0; i++; i < n) {
cout << a[i].second;
}
}
for(i=0;i++;i<n){
You meant to write this as:
for(i=0;i<n;i++){
Related
The formula is listed in the following article: https://en.wikipedia.org/wiki/Formula_for_primes. I am trying to implement it but to no success, for whatever reason the code is producing number which seem to be nth power of two + 1, which is obviously not what I want to achieve.
#include <iostream>
#include <cmath>
using namespace std;
int nth_prime(int n) {
double s = 1;
for (int i = 1; i <= pow(2, n); i++) {
double c = 0;
for (int j = 1; j <= i; j++) {
double f = (tgamma(j)+1)/j;
c+=floor(pow(cos(M_PI*f), 2));
}
s+=floor(pow(n/c, 1/n));
}
return s;
}
int main() {
int n;
while (cin >> n) {
cout << nth_prime(n) << endl;
}
return 0;
}
So i have 2 lines of arrays. First line is M, second line is N.
On the first line the user inputs 2 digits, first for the length of M, and second for the length of N.
On the second line user inputs M, on the third - N. All i want is to display what elements of N are not contained in m, but in ascending order.
Here's an example:
7 5
3.12 7.96 3.51 4.77 10.12 1.11 9.80
10.12 3.51 3.12 9.80 4.77
Output:
1.11 7.96
And here is my attempt at the program:
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
int ifexists(double z[], int u, int v)
{
int i;
if (u == 0)
return 0;
for (i = 0; i <= u; i++)
if (z[i] == v)
return (1);
return (0);
}
void main()
{
double bills[100], paid[100], result[100];
int m, n;
int i, j, k;
cin >> m >> n;
if (n > m) {
cout << "Wrong input.";
exit(3);
}
for (int i = 0; i < m; i++) {
cin >> bills[i];
}
for (int i = 0; i < n; i++) {
cin >> paid[i];
}
for (i = 0; i < n; i++)
k = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (bills[i] == paid[j]) {
break;
}
}
if (j == n) {
if (!ifexists(result, k, bills[i])) {
result[k] = bills[i];
k++;
}
}
}
for (i = 0; i < k; i++)
cout << result[i] << " ";
}
The output i'm getting is almost correct - 7.96 1.11. Basically in reverse order. How can i flip this around? Also, chances are my report is too slow. Is there a way to increase the speed?
You can use std::set_difference:
std::sort(bills, bills + m);
std::sort(paid, paid + n);
auto end = std::set_difference(bills, bills + m, paid, paid + n, result);
for (auto it = result; it != end; ++it) {
std::cout << *it << " ";
}
I am making a set of pairs of Max and Min elements of Every Subset in an Array.But its giving me these errors. And at last I need Size of set.
(Edited with some suggestions)
In function 'int main()':
27:12: error: 'max_element' was not declared in this scope
27:12: note: suggested alternative: 'max_align_t'
28:12: error: 'min_element' was not declared in this scopeIn function 'int main()':
Code:
#include <iostream>
#include <set>
#include <vector>
#include <utility>
typedef std::pair<int,int> pairs;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, max, min;
set<pairs> s;
cin >> n;
int a[n];
for(int i=0;i<n;i++) {
cin >> a[i];
}
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) {
vector<int> v;
v.push_back(a[j]);
if(v.size() > 1) {
max = *max_element(v.begin(),v.end());
min = *min_element(v.begin(),v.end());
pairs p1 = make_pair(max, min);
s.insert(p1);
max = 0;
min = 0;
}
}
}
cout << s.size() << endl;
}
typedef pair<int,int> pairs;
should be
typedef std::pair<int,int> pairs;
(Or you could move using namespace std; so that it is before your typedef).
Plus typedefing a single pair as the plural pairs is a really really bad idea, that is going to confuse you and anyone else reading your code for the rest of this programs existence. If you want a typedef for a pair of ints, then call it that
typedef std::pair<int,int> pair_of_ints;
To make your last programme works, it was needed to move the declaration of std::vector<int> v;
Moreover, your code has a complexity O(n^3). In practice, it is possible to get a complexity O(n^2), by calculating
iteratively the max and min values.
This code compares your code and the new one. The results are identical. However, I cannot be sure
that your original code does what you intended to do.
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
typedef std::pair<int,int> pairs;
//using namespace std;
void print (const std::set<pairs> &s) {
for (auto& p: s) {
std::cout << "(" << p.first << ", " << p.second << ") ";
}
std::cout << "\n";
}
int count_pairs_op (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
std::vector<int> v;
for(int j = i; j < n; j++) {
v.push_back(a[j]);
if(v.size() > 1) {
max = *std::max_element(v.begin(), v.end());
min = *std::min_element(v.begin(), v.end());
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
}
print (s);
return s.size();
}
int count_pairs_new (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
min = a[i];
max = a[i];
for(int j = i+1; j < n; j++) {
max = std::max (max, a[j]);
min = std::min (min, a[j]);
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
print (s);
return s.size();
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_op(a) << std::endl;
std::cout << count_pairs_new(a) << std::endl;
}
It appears that there was a mistake in the understanding of the problem.
For each subarray, we have to consider the maximum and the second maximum.
Moreover, we know that all elements are distinct.
As the size can be up to 10^5, we have to look for a complexity smaller than O(n^2).
In practice, each element can be the second element of two subarrays,
if there exist a greater element before and after it.
We just have to check it.
This can be perfomed by calculating, for each index i, the maximum value before and after it.
Total complexity: O(n)
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
int count_pairs_2nd_max (const std::vector<int>& a) {
int n = a.size();
int count = 0;
std::vector<int> max_up(n), max_down(n);
max_up[0] = -1;
for (int i = 1; i < n; ++i) {
max_up[i] = std::max(max_up[i-1], a[i-1]);
}
max_down[n-1] = -1;
for (int i = n-2; i >= 0; --i) {
max_down[i] = std::max(max_down[i+1], a[i+1]);
}
for(int i = 0; i < n; ++i) {
if (max_up[i] > a[i]) count++;
if (max_down[i] > a[i]) count++;
}
return count;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_2nd_max(a) << std::endl;
}
I saw some algorithm for a problem and rewrote it but I changed the fast_count_segment function's inner loop exit condition j=n1 to the break statement,the code timed out. Why does this happen?
The code runs fine otherwise but when I submit to the online grader, it shows code timeout, but as soon as I replace break with j=n1, it passes.
Shouldn't both be doing the same thing?
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> fast_count_segments(vector<pair<int,int>> &v, vector<int> points) {
vector<int> cnt(points.size());
int n1=v.size(),n2=points.size(),i,j,count;
for(i=0;i<n2;i++){
count=0;
for(j=0;j<n1;j++){
if(points[i]>=v[j].first && points[i]<=v[j].second)
count++;
else if(points[i]<v[j].first)
break; //j=n1; Changed this statement to break
}
cnt[i]=count;
}
return cnt;
}
int main() {
int n, m,a,b;
cin >> n >> m;
vector<pair<int,int>> v;
for (size_t i = 0; i < n; i++) {
cin >>a>>b;
v.push_back(make_pair(a,b));
}
vector<int> points(m);
for (size_t i = 0; i < points.size(); i++) {
cin >> points[i];
}
sort(v.begin(),v.end());
vector<int> cnt = fast_count_segments(v, points);
for (size_t i = 0; i < cnt.size(); i++) {
cout << cnt[i] << ' ';
}
cout<<endl;
return 0;
}
I wrote a really simple program, but it's crashing when I try to write the size of a queue (created with STL). I have no idea why, please help.
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, i, x, cut = 0;
queue<int> que;
vector<int> vec;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x;
vec.push_back(x);
}
sort(vec.begin(), vec.end());
for (i = 0; i < n; i++)
que.push(vec[i]);
while (!que.empty()) {
cout << que.size() << '\n';
cut += que.front();
while (que.front() <= cut)
que.pop();
}
return 0;
}
You get an error because you call front while the queue is empty.
Just check if the queue is empty in your inner loop:
#include <queue>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, i, x, cut = 0;
queue<int> que;
vector<int> vec;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x;
vec.push_back(x);
}
sort(vec.begin(), vec.end());
for (i = 0; i < n; i++)
que.push(vec[i]);
while (!que.empty()) {
cout << que.size() << '\n';
cut += que.front();
while (!que.empty() && que.front() <= cut )
que.pop();
}
return 0;
}
Your code is actually crashing on the line:
while (que.front() <= cut)
Because you have a loop that may be true for the whole queue. The next line pops a value. At some point, your queue is empty and que.front() will crash.