#include <stdio.h>#include <stdlib.h>int main( int argc, char *argv[] ){ if( argc != 2 ) { printf("%s \"<string>\"\n\n",argv[0]); return 1; } char *end = argv[1]; while( *end ) ++end; char temp, *word_start, *word_end, *next_word = argv[1]; while( next_word != end ) { while( *next_word == ' ' ) ++next_word; if( *next_word == '\0' ) break; word_start = word_end = next_word; while( (word_end < end) && (*word_end != ' ') ) ++word_end; next_word = word_end; --word_end; while( word_start <= word_end ) { temp = *word_start; *word_start++ = *word_end; *word_end-- = temp; } } printf("Output: %s\n\n",argv[1]); return 0; }
public static String splitReverse(String in){ String out= "", word=""; while(true){ if(in.equals("")){ out+=reverse(word); return out; } else if((in.charAt(0)>=65 && in.charAt(0)<=89) || (in.charAt(0) >=97 && in.charAt(0) <= 122) ) word+=in.charAt(0); else{ out += reverse(word) + in.charAt(0); word=""; } in = in.substring(1); }}public static String reverse(String s){ String out= ""; return reverse2(s,out);}public static String reverse2(String s, String out){ if(s.equals("")) return out; out = s.charAt(0) + out; s = s.substring(1); return reverse2(s,out);}}
#include <iostream>#include <fstream>#include <algorithm>using namespace std;int main(int argc, char** argv) { int i, j, k, n, m; int x[50], y[50], z[100]; // read some data ifstream fin("nr.in"); fin >> n; for (i = 0; i < n; ++i) { fin >> x[i]; } fin >> m; for(j = 0; j < m; ++j) { fin >> y[j]; } fin.close(); // sort input arrays (i assume the input arrays are not sorted) sort(x, x + n); sort(y, y + m); // do the merge i = j = 0; k = -1; while ((i < n) && (j < m)) { if (x[i] < y[j]) { z[++k]=x[i++]; } else { z[++k]=y[j++]; } } if (i < n) { for (j = i; j < n; ++j) { z[k++] = x[j]; } } else { for (i = j; i < m; ++i) { z[k++] = y[i]; } } // Output the result cout << endl << "New array \"z\":" << endl; for (i = 0; i < k; ++i) { cout << z[i] << " "; } return 0;}
Find the minimum spanning tree (aka MST) in a graph with N nodes and E edges.
N EEach edge definition has this form: X Y C. Interpret it like this: There is an edge from node X to node Y with a cost C.
The total cost of the MSTNumber of edges in the MSTEdge 1 from MSTEdge 2 from MSTand so on
9 141 2 101 3 -112 4 112 5 115 6 133 4 104 6 124 7 53 7 43 8 58 7 58 9 49 7 36 7 11
3783 17 97 39 87 42 15 27 6
Implement a function that take a real (float, better double) number as parameter and returns the binary logarithm of that number. You are allowed to use only this mathematical operations: +, -, *, /, sqrt.
log2(321) = log2(2 * [321/2]) = = 1 + log2(160) = 1 + log2(2 * [160/2]) = 2 + log2(80) = 2 + log2(2 * [80/2]) = 3 + log2(40) = 4 + log2(20) = 5 + log2(10) = 6 + log2(5) = 7 + log2(2) = 8 + log2(1).
#include <stdio.h>#include <algorithm>#include <vector>using namespace std;struct Node { int rank; int father;};struct Edge { int cost; int x; int y;};void init_forest(int node, Node* forest) { forest[node].rank = 0; forest[node].father = node;}int find_root(int node, Node* forest) { if (forest[node].father != node) { forest[node].father = find_root(forest[node].father, forest); } return forest[node].father; }void union_trees(int node_a, int node_b, Node* forest) { int x_root = find_root(node_a, forest); int y_root = find_root(node_b, forest); if (x_root == y_root) { return; } if (forest[x_root].rank < forest[y_root].rank) { forest[x_root].father = y_root; } else if (forest[x_root].rank > forest[y_root].rank) { forest[y_root].father = x_root; } else { forest[y_root].father = x_root; forest[y_root].rank += 1; }}int edge_cmp_func(Edge x, Edge y) { return x.cost < y.cost;}#define MAXN 400100int main(int argc, char** argv) { int n, e; Node forest[MAXN]; Edge edges[MAXN]; FILE* fin = fopen("apm.in", "r"); FILE* fout = fopen("apm.out","w"); fscanf(fin, "%d%d", &n, &e); for (int i = 0; i < e; ++i) { fscanf(fin, "%d %d %d", &edges[i].x, &edges[i].y, &edges[i].cost); } fclose(fin); for (int i = 0; i < n; ++i) { init_forest(i, forest); } sort(edges, edges + e, edge_cmp_func); int answear = 0; vector<int> answear_edges_index; for (int i = 0; i < e; ++i) { if (find_root(edges[i].x, forest) != find_root(edges[i].y, forest)) { union_trees(edges[i].x, edges[i].y, forest); answear += edges[i].cost; answear_edges_index.push_back(i); } } fprintf(fout, "%d\n%d\n", answear, answear_edges_index.size()); for (int i = 0; i < answear_edges_index.size(); ++i) { fprintf(fout, "%d %d\n", edges[answear_edges_index[i]].x, edges[answear_edges_index[i]].y); } fclose(fout); return 0;}