sears1234
BAN USER#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main(int argc, char **argv)
{
if(argc != 2)
{
printf("usage: %s [test_chars]\n", argv[0]);
return 0;
}
char *test_chars = argv[1];
char temp[256];
memset(temp, 0, sizeof(temp));
for(int i = strlen(test_chars); i >= 0; i--)
{
if(temp[test_chars[i]] == 0)
{
printf("%c", test_chars[i]);
temp[test_chars[i]]++;
}
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int get_block_size(int all[], int all_size, int damage[], int damage_size, int rollar)
{
int start_index = 0, end_index = 0;
int size = 0;
for(start_index = 0; start_index < damage_size; start_index++)
{
if(damage[start_index] != 0)
{
break;
}
}
for(end_index = damage_size - 1; end_index >- 0; end_index--)
{
if(damage[end_index] != 0)
{
break;
}
}
int start = damage[start_index];
int end = damage[end_index];
while(all[start_index] != start)
{
start_index++;
}
while(all[end_index] != end)
{
end_index--;
}
while(start_index < end_index)
{
bool found = false;
for(int i = start_index; i < rollar + start_index; i++)
{
if(damage[i] != 0)
{
found = true;
size++;
}
}
if(found)
{
start_index += rollar;
}
found = false;
for(int i = end_index; i > end_index - rollar; i--)
{
if(damage[i] != 0)
{
size++;
}
}
if(found)
{
end_index -= rollar;
}
}
return size;
}
int main(int argc, char** argv)
{
int all_road[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int damage_road_1[] = {0, 2, 3, 0, 0, 6, 0, 0, 0, 0 };
int damage_road_2[] = {0, 0, 0, 4, 0, 6, 0, 8, 0, 10};
int rollar_size = 0;
int all_road_size = sizeof(all_road)/sizeof(int);
int damage_road_size_1 = sizeof(damage_road_1)/sizeof(int);
int damage_road_size_2 = sizeof(damage_road_2)/sizeof(int);
rollar_size = 3;
printf("block size 1 = %d\n", get_block_size(all_road, all_road_size, damage_road_1, damage_road_size_1, rollar_size));
rollar_size = 2;
printf("block size 2 = %d\n", get_block_size(all_road, all_road_size, damage_road_2, damage_road_size_2, rollar_size));
return 0;
}
block size 1 = 5
block size 2 = 6
The basic idea is walk every left node until it reach the end.
#include <stdio.h>
#include <stdlib.h>
#define null 0
using namespace std;
typedef struct s_node
{
private:
int m_val;
s_node* m_left_node;
s_node* m_right_node;
public:
s_node(int val)
{
m_val = val;
m_left_node = null;
m_right_node= null;
}
void add_left_node(s_node* node)
{
m_left_node = node;
}
void add_right_node(s_node* node)
{
m_right_node = node;
}
s_node* get_left_node()
{
return m_left_node;
}
s_node* get_right_node()
{
return m_right_node;
}
int get_val()
{
return m_val;
}
~s_node()
{
if(m_left_node)
{
m_left_node = null;
}
if(m_right_node)
{
m_right_node = null;
}
}
}SNODE;
int main(int argc, char** argv)
{
//the following is examples of this test.
SNODE *root = new SNODE(1);
SNODE *two = new SNODE(2);
SNODE *three = new SNODE(3);
SNODE *four = new SNODE(4);
SNODE *five = new SNODE(5);
SNODE *six = new SNODE(6);
//build relationship
root->add_left_node(two);
root->add_right_node(three);
two->add_left_node(four);
two->add_right_node(five);
three->add_left_node(six);
four->add_left_node(six);
four->add_right_node(five);
five->add_right_node(six);
six->add_right_node(four);
SNODE* walk = root;
int height = 0;
while(walk)
{
walk = walk->get_left_node();
height++;
}
height--;
printf("height = %d\n", height);
delete root;
delete two;
delete three;
delete four;
delete five;
delete six;
return 0;
}
#include <string>
#include <algorithm> // std::sort
#include <vector> // std::vector
#include <stdio.h>
#include <stdlib.h>
using namespace std;
bool sort_by_length(const string &str1, const string &str2)
{
return str1.length() >= str2.length();
}
void sort_vector_by_len(vector<string> &vec)
{
sort(vec.begin(), vec.end(), sort_by_length);
}
void display_new_string(const string &str1, const string &str2)
{
string nStr;
int start = str1.find(str2), end = start + str2.length();
for(int i = 0; i < str1.length(); i++)
{
if(start == i)
{
nStr += "[";
}
nStr += str1[i];
if(end == i + 1)
{
nStr += "]";
}
}
printf("NEW STRING: %s\n", nStr.c_str());
}
int main(int argc, char** argv)
{
vector<string> names, symbols;
names.push_back("Amazon");
names.push_back("Microsoft");
names.push_back("Google");
symbols.push_back("I");
symbols.push_back("Am");
symbols.push_back("cro");
symbols.push_back("Na");
symbols.push_back("le");
symbols.push_back("abc");
vector<string>::iterator it1, it2;
sort_vector_by_len(symbols);
for(it1 = names.begin(); it1 != names.end(); it1++)
{
for(it2 = symbols.begin(); it2 != symbols.end(); it2++)
{
int start = (*it1).find((*it2)), end;
if(start != string::npos)
{
display_new_string(*it1, *it2);
break;
}
}
}
printf("\n");
return 0;
}
- sears1234 June 03, 2016