AndyC
BAN USERHere is C++ solution...
#include<iostream>
using namespace std;
void printProduct(int* arr, int n){
if (n <= 0)
return;
int product = 1;
int zero = 0;
for (int i = 0; i < n; ++i){
if (!arr[i])
zero++;
else
product *= arr[i];
}
if (zero>1){
for (int i=0; i < n; ++i)
cout << 0 << " ";
}
else if (zero == 1){
for (int i = 0; i < n; ++i)
if(!arr[i])
cout << product << " ";
else
cout << 0 << " ";
}
else{
for (int i = 0; i < n; ++i)
cout << product/arr[i] << " ";
}
cout << endl;
}
int main(){
int arr[5] = { 5, 1, 0, 3, 0};
printProduct(arr, 5);
return 0;
}
Here is C++ solution...!!!
#include<iostream>
#include<vector>
using namespace std;
void G2B(const vector<int>& Gvec, vector<int>& result){
int bSize = Gvec.size();
int gSize = result.size();
if (bSize == gSize)
return;
if (!gSize)
result.push_back(Gvec[0]);
else
{
int temp = Gvec[gSize] ^ result[gSize - 1];
result.push_back(temp);
}
G2B(Gvec, result);
}
void B2G(const vector<int>& Bvec, vector<int>& result){
int bSize = Bvec.size();
int gSize = result.size();
if (bSize == gSize)
return;
if (!gSize)
result.push_back(Bvec[0]);
else
{
int temp = Bvec[gSize] ^ Bvec[gSize - 1];
result.push_back(temp);
}
B2G(Bvec, result);
}
int main(){
vector<int> vec = { 1, 0, 1, 1 ,0 ,1 ,0 ,1 ,1 }; //Input
vector<int> Gresult, Bresult; //Output
B2G(vec, Gresult); //Binary to Gray
G2B(Gresult, Bresult); //Gray to Binary
for (int i = 0; i < Bresult.size(); i++)
cout << Bresult[i] << " ";
cout << endl;
return 0;
}
Here is the solution c++...!!!
#include<iostream>
#include<vector>
#include<unordered_map>
void getMaxSequence(vector<int>& vec){
unordered_map<int, int> hash;
unordered_map<int, int>::iterator it, prev, next;
for (int i = 0; i < vec.size(); ++i){
it = hash.find(vec[i]);
if (it == hash.end()){
prev = hash.find(vec[i] - 1);
next = hash.find(vec[i] + 1);
if (prev != hash.end() && next != hash.end()){
int temp = prev->second;
hash[prev->second] = next->second;
hash[next->second] = temp;
hash[vec[i]] = prev->first;
}
else if (prev != hash.end()){
int temp = prev->second;
hash[temp] = vec[i];
hash[vec[i]] = temp;
}
else if (next != hash.end()){
int temp = next->second;
hash[temp] = vec[i];
hash[vec[i]] = temp;
}
else{
hash[vec[i]] = vec[i];
}
}
}
int max = 0;
int start = 0, end = 0;
for (it = hash.begin(); it != hash.end(); it++){
int result = it->first - it->second;
if (result > max)
{
start = it->second;
end = it->first;
max = result;
}
}
for (int i = start; i <= end; i++)
cout << i << " ";
cout << endl;
}
int main(){
int arr[9] = { 1, 12, 10, 4, 7, 9, 5, 5, 8 };
vector<int> vec(arr, arr + 9);
getMaxSequence(vec);
return 0;
}
Here is C++ Solution...!!!
- AndyC April 24, 2015