I am new to C++. I know the output would be 1024, 10.
I just have no clue as to why log would print out 10, instead of 1.
int n = 1024;
int log = 0;
for (int i = 1; i < n; i = i * 2);
log++;
cout << n << " " << log << endl;
I believe you are missing just the brackets if you are trying to iterate over the loop and print out the log.
int n = 1024;
int log = 0;
for (int i = 1; i < n; i = i * 2)
{
log++;
cout << n << " " << log << endl;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Given a 2D array, To find the element maximum for given sub rectangle corresponding to each query.
For example
for array of 3*4
1 2 3 7
13 6 34 7
12 5 7 8
for eg:
MAX in sub array between (0,0) and (1,1) is 13
MAX in sub array between (1,1) and (2,2) is 34
I believe you are looking something like this:
int row = 0, col = 0;
cout << "please enter row and column"<<endl;
cin >> row >> col;
int** p = (int**) new int[row*col];
for (int i = 0; i < row; ++i)
{
p[i] = new int[col];
for (int j = 0; j < col; ++j)
{
cout << "Enter the number at position p[" << i << "][" << j << "]" << endl;
int temp = 0;
cin >> temp;
p[i][j] = temp;
}
}
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
cout << p[i][j] << "\t";
cout << endl;
}
cout << "Now find the max number provided rectangle co-ordinate[a,b] to [x,y]"<<endl;
cout << "please specify the starting and ending co-ordinates"<<endl;
int a = 0, b = 0, c = 0, d = 0;
cin >> a >> b >> c >> d;
int max = p[a][b];
for (int i = a; i <= c; ++i)
{
for (int j = b; j <= d; ++j)
{
if (p[i][j] > max)
{
max = p[i][j];
}
}
}
cout << "max of the rectangle = " << max<<endl;
I have this code where I have to edit the works of M with 2 values when required .
Mat is a distance given by distancetrasform . If I try to start facciene release me so I do not how I want the value int 2 . I tried to cast but nothing . How do I change the pixel values of M ? with int value ?
Mat M = Mat::zeros(300, 300, CV_8U);
int i, j;
for (i = 0; i < distanza.cols ; i++)
{
for (j = 0; j < distanza.rows ; j++)
{
if ((int)distanza.at<float>(i,j) > 0 )
{
M.at <uchar>(i,j) = 2 ; //here as i write ?
cout << " " << M.at <uchar>(i, j) << endl;
}
}
}
Working under a time crunch here. Struggling to understand exactly what this problem is asking. Any help or pointers in the right direction would be greatly appreciated! Thanks in advanced.
The original problem is based on this given information:
for (int k = 0; k < 2*n; k++) {
cout << k << endl;
for (int i = k+1; i < n; i++)
{
m[i][j] = a[i][j] + b[i][j];
cout << m[i][j] << endl;
}
cout << i * k << endl;
}
For T(n) = http://www4c.wolframalpha.com/Calculate/MSP/MSP63941h503ff0a609230100002eieg6bhfe5gi70g?MSPStoreType=image/gif&s=23&w=167.&h=49.
And here is my problem:
Modify the code above to find the number of times the basic operation occurs (i.e. how many times does it go in the inner for loop?).
include
using namespace std;
int main()
{
int count = 0;
int n = 10;
for (int k = 0; k < 2*n; k++) {
cout << "outer: " << k << endl;
for (int i = k+1; i < n; i++) {
cout << "\tinner: " << i << endl;
count++;
}
}
cout << count << endl;
}
Write a summation based on the output of Step 1
Based on this, is T(n) equivalent to O(n) or O(n^2)
I'm confused about specifically what part 2 is asking for. But I found:
http://www4c.wolframalpha.com/Calculate/MSP/MSP4561hgb5f47a07e05g00000112a53ahh0670che?MSPStoreType=image/gif&s=30&w=109.&h=49.
To me this looks like O(N^2)?
I apologize for the formatting. I'm on mobile.
Let me see if I guide:
1. I think the count should be inside like this:
int main() {
int count = -1;
int n = 10;
for (int k = 0; k < 2*n; k++) {
count = 0;
cout << "outer: " << k << endl;
for (int i = k+1; i < n; i++) {
cout << "\tinner: " << i << endl;
count++;
}
cout << count << endl; //<<<here
}
}
Now collect the output (#here marker) and form a formula for the summation. I think this is Task#2.
Based on your formula (or summation) you will be able to generalize whether its o(n) or o(n^2).
This is definitely not linear.
I am trying to solve this problem, I think I have come up with a correct answer, but I am keep getting WA (wrong answer) response from the judge.
http://www.spoj.com/problems/FISHER/
The problem distilled, is, given a complete graph with a time and a toll associated with each edge, find a path from the first node to the last node within time constraint and minimize toll.
As with any problems, there are many ways to solve it. My idea is to extend the Floyd-Warshall algorithm to keep track of all non-dominated paths. At the end of the algorithm, we extract the path with minimal cost, and if there are multiple paths with the same cost, choose the one that spent least time.
Complexity aside, the bad thing is, wrong answer. I have no idea what is wrong. I have generated some random graphs and used a brute force solver (one that try all possible paths) and they matches exactly on small (i.e. less than 11 nodes) graphs. Without further ado, here is the code:
#include "stdafx.h"
// http://www.spoj.com/problems/FISHER/
// #define LOG
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
while (true)
{
int num_cities;
int time_budget;
vector<vector<int> > distances;
vector<vector<int> > tolls;
cin >> num_cities;
cin >> time_budget;
if (num_cities == 0 && time_budget == 0)
{
break;
}
distances.resize(num_cities);
tolls.resize(num_cities);
for (int i = 0; i < num_cities; i++)
{
distances[i].resize(num_cities);
tolls[i].resize(num_cities);
}
for (int i = 0; i < num_cities; i++)
{
for (int j = 0; j < num_cities; j++)
{
int distance;
cin >> distance;
distances[i][j] = distance;
}
}
for (int i = 0; i < num_cities; i++)
{
for (int j = 0; j < num_cities; j++)
{
int toll;
cin >> toll;
tolls[i][j] = toll;
}
}
// Try Floyd Warshall
// Denote the set of shortest paths from i to j going through {0,1,...k - 1} be shortest_paths[i][j][k],
// It is a set of shortest paths because there can be multiple shortest paths with different time used.
// We should record if using longer time can lead to lower cost, or similarly higher cost but less time
// The first element in the pair is the cost, the second element in the pair is time used
vector<vector<vector<vector<pair<int, int> > > > > shortest_paths;
shortest_paths.resize(num_cities);
for (int i = 0; i < num_cities; i++)
{
shortest_paths[i].resize(num_cities);
for (int j = 0; j < num_cities; j++)
{
shortest_paths[i][j].resize(num_cities + 1);
}
}
// Initialization - there is only one path without going through any node
#ifdef LOG
cout << "k = " << 0 << endl;
cout << "<table border='1'>" << endl;
#endif
for (int i = 0; i < num_cities; i++)
{
#ifdef LOG
cout << "<tr>" << endl;
#endif
for (int j = 0; j < num_cities; j++)
{
#ifdef LOG
cout << "<td>(" << tolls[i][j] << ", " << distances[i][j] << ")</td>";
#endif
shortest_paths[i][j][0].push_back(pair<int, int>(tolls[i][j], distances[i][j]));
}
#ifdef LOG
cout << "</tr>" << endl;
#endif
}
#ifdef LOG
cout << "</table>" << endl;
#endif
// Iteration - the shortest path
for (int k = 1; k <= num_cities; k++)
{
#ifdef LOG
cout << "k = " << k << endl;
cout << "<table border='1'>" << endl;
#endif
for (int i = 0; i < num_cities; i++)
{
#ifdef LOG
cout << "<tr>";
#endif
for (int j = 0; j < num_cities; j++)
{
// Step 1: Generate all candidate shortest paths
map<pair<int, int>, bool> candidates;
for (vector<pair<int, int> >::iterator pi = shortest_paths[i][j][k - 1].begin(); pi != shortest_paths[i][j][k - 1].end(); pi++)
{
candidates.insert(pair<pair<int, int>, bool>(*pi, false));
}
for (vector<pair<int, int> >::iterator fi = shortest_paths[i][k - 1][k - 1].begin(); fi != shortest_paths[i][k - 1][k - 1].end(); fi++)
{
for (vector<pair<int, int> >::iterator si = shortest_paths[k - 1][j][k - 1].begin(); si != shortest_paths[k - 1][j][k - 1].end(); si++)
{
int first_path_cost = fi->first;
int first_path_time_used = fi->second;
int second_path_cost = si->first;
int second_path_time_used = si->second;
int new_path_cost = first_path_cost + second_path_cost;
int new_path_time_used = first_path_time_used + second_path_time_used;
if (new_path_time_used <= time_budget)
{
candidates.insert(pair<pair<int, int>, bool>(pair<int, int>(new_path_cost, new_path_time_used), false));
}
}
}
vector<pair<pair<int, int>, bool> > candidates_list;
for (map<pair<int,int>, bool>::iterator ci = candidates.begin(); ci != candidates.end(); ci++)
{
candidates_list.push_back(*ci);
}
// Eliminate the bad ones
for (unsigned int p = 0; p < candidates_list.size(); p++)
{
for (unsigned int q = 0; q < candidates_list.size(); q++)
{
if (p != q)
{
int first_path_cost = candidates_list[p].first.first;
int first_path_time_used = candidates_list[p].first.second;
int second_path_cost = candidates_list[q].first.first;
int second_path_time_used = candidates_list[q].first.second;
// First take less time and less cost than second, second is eliminated
if (first_path_time_used <= second_path_time_used && first_path_cost <= second_path_cost)
{
candidates_list[q].second = true;
}
}
}
}
#ifdef LOG
cout << "<td>";
#endif
for (unsigned int p = 0; p < candidates_list.size(); p++)
{
if (candidates_list[p].second == false)
{
#ifdef LOG
cout << "(" << candidates_list[p].first.first << ", " << candidates_list[p].first.second << ")<br>";
#endif
shortest_paths[i][j][k].push_back(candidates_list[p].first);
}
}
#ifdef LOG
cout << "</td>";
#endif
}
#ifdef LOG
cout << "</tr>" << endl;;
#endif
}
#ifdef LOG
cout << "</table>" << endl;
#endif
}
bool first = true;
int best_cost = -1;
int best_cost_time = -1;
for (vector<pair<int, int> >::iterator pi = shortest_paths[0][num_cities - 1][num_cities].begin(); pi != shortest_paths[0][num_cities - 1][num_cities].end(); pi++)
{
if (first)
{
best_cost = pi->first;
best_cost_time = pi->second;
first = false;
}
else
{
if (pi->first < best_cost)
{
best_cost = pi->first;
best_cost_time = pi->second;
}
if (pi->first == best_cost && pi->second < best_cost_time)
{
best_cost_time = pi->second;
}
}
}
cout << best_cost << " " << best_cost_time << endl;
}
return 0;
}
/*
4 7
0 5 2 3
5 0 2 3
3 1 0 2
3 3 2 0
0 2 2 7
2 0 1 2
2 2 0 5
7 2 5 0
0 0
*/
Turn on the LOG you will be able to see the Floyd Warshall table for each iteration, each cell has set of a (cost, time) pair. They are supposed to be the cost/time pairs of all non-dominated paths.
I would really appreciate if someone can tell me what's wrong. Thanks a lot in advance!
Try this test:
4 10
0 1 1 1000
1 0 1 1
1 1 0 1
1000 1 1 0
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
Basically you need to ensure distances[i][j] <= time_budget before
shortest_paths[i][j][0].push_back(pair<int, int>(tolls[i][j], distances[i][j]));