ms
BAN USER#include <iostream>
#include <vector>
class ball_passing
{
int no_of_frnds;
int jump;
int max_count;
public:
ball_passing(int n, int m, int l) :jump(l), max_count(m), no_of_frnds(n){};
void ball_throw()
{
std::vector<int> frnds(no_of_frnds);
int next = 0; //start with frnd 1
int passes = 0;
frnds[next]++;
while (frnds[next] < max_count)
{
++passes;
if (frnds[next] % 2 == 0)
{
next = (next + jump) < no_of_frnds ? (next + jump) : (next + jump - no_of_frnds);
}
else
{
next = (next + no_of_frnds - jump) < no_of_frnds ? (next + no_of_frnds - jump) : (next - jump) ;
}
++frnds[next];
}
std::cout << "\nTotal passes:" << passes;
}
};
int main()
{
ball_passing bp(5,3,2);
bp.ball_throw();
return 0;
}
logic derivation:
Change of sign a element in set will return the target number.
Assume R is the resultant element and we will change the sign of R
So in the sum of the all elements of set (Say Sum), R is also included. First exclude that.
So we have sum of all elements except R => Sum – R.
Now, we changed sign of R and add it to get new sum of all elements.
( Sum –R ) + ( –R) = target //we changed sign of R
i.e, Sum –R –R = target
i.e, Sum – 2*R = target
Now for which i in set, this equation will satisfy, that is the result.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void my_swap(string &str, int a, int b)
{
--a;
--b;
char temp = str[a];
str[a] = str[b];
str[b] = temp;
}
void lex_larger(string &str, vector<pair<int, int>> p)
{
char max = str[0];
for (int i = 1; i < str.size(); i++)
{
if (max < str[i])
max = str[i];
}
vector<pair<int, int>>::iterator it;
while (str[0] != max)
{
for (it = p.begin(); it != p.end(); it++)
my_swap(str, it->first, it->second);
}
}
int main()
{
string s ="abdc";
vector<pair<int, int>> v;
v.push_back(make_pair( 1, 4));
v.push_back(make_pair(3, 4));
lex_larger(s, v);
return 0;
}
#include<iostream>
#include<string>
#include<unordered_map>;
using namespace std;
string convertkeypad(string s)
{
int i;
char n;
string out = "";
unordered_map<char, string> pad;
pad['2'] = "ABC";
pad['3'] = "DEF";
pad['4'] = "GHI";
pad['5'] = "JKL";
pad['6'] = "MNO";
pad['7'] = "PQRS";
pad['8'] = "TUV";
pad['9'] = "WXYZ";
pad['#'] = " ";
for (i = 0; i < s.length();++i)
{
n = s[i];
int k = 0;
if (s[i+1] == n)
{
++k;
++i;
if (s[i+1] == n)
{
++k;
++i;
if (s[i+1] == n)
{
++k;
++i;
}
}
}
out = out + (pad[n])[k];
}
return out;
}
int main()
{
string s =convertkeypad("567#76534#453");
return 0;
}
struct bstnode* get_closest_value(struct bstnode* root, int k)
{
struct bstnode* node;
if (root == NULL) return root;
if (root->data == k) return root;
else if (root->data < k)
{
if (root->left == NULL) return root;
else
{
node = get_closest_value(root->left, k);
return(abs(node->data - k) > abs(root->data - k) ? root : node);
}
}
else
{
if (root->right == NULL) return root;
else
{
node = get_closest_value(root->right, k);
return(abs(node->data - k) > abs(root->data - k) ? root : node);
}
}
}
Please comment on solution.
- ms April 01, 2016