xxxxxxxxxx
#include<bits/stdc++.h>
using namespace std;
string s[128];
int n,i;
int main()
{
cin>>n;
for(int i=0; i<n; i++)
{
cin>>s[i];
}
sort(s,s+n);
cout<<s[n/2]<<endl;
}
xxxxxxxxxx
// Program to print path from root node to destination node
// for N*N -1 puzzle algorithm using Branch and Bound
// The solution assumes that instance of puzzle is solvable
#include <bits/stdc++.h>
using namespace std;
#define N 3
// state space tree nodes
struct Node
{
// stores the parent node of the current node
// helps in tracing path when the answer is found
Node* parent;
// stores matrix
int mat[N][N];
// stores blank tile coordinates
int x, y;
// stores the number of misplaced tiles
int cost;
// stores the number of moves so far
int level;
};
// Function to print N x N matrix
int printMatrix(int mat[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
}
// Function to allocate a new node
Node* newNode(int mat[N][N], int x, int y, int newX,
int newY, int level, Node* parent)
{
Node* node = new Node;
// set pointer for path to root
node->parent = parent;
// copy data from parent node to current node
memcpy(node->mat, mat, sizeof node->mat);
// move tile by 1 position
swap(node->mat[x][y], node->mat[newX][newY]);
// set number of misplaced tiles
node->cost = INT_MAX;
// set number of moves so far
node->level = level;
// update new blank tile cordinates
node->x = newX;
node->y = newY;
return node;
}
// bottom, left, top, right
int row[] = { 1, 0, -1, 0 };
int col[] = { 0, -1, 0, 1 };
// Function to calculate the number of misplaced tiles
// ie. number of non-blank tiles not in their goal position
int calculateCost(int initial[N][N], int final[N][N])
{
int count = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (initial[i][j] && initial[i][j] != final[i][j])
count++;
return count;
}
// Function to check if (x, y) is a valid matrix cordinate
int isSafe(int x, int y)
{
return (x >= 0 && x < N && y >= 0 && y < N);
}
// print path from root node to destination node
void printPath(Node* root)
{
if (root == NULL)
return;
printPath(root->parent);
printMatrix(root->mat);
printf("\n");
}
// Comparison object to be used to order the heap
struct comp
{
bool operator()(const Node* lhs, const Node* rhs) const
{
return (lhs->cost + lhs->level) > (rhs->cost + rhs->level);
}
};
// Function to solve N*N - 1 puzzle algorithm using
// Branch and Bound. x and y are blank tile coordinates
// in initial state
void solve(int initial[N][N], int x, int y,
int final[N][N])
{
// Create a priority queue to store live nodes of
// search tree;
priority_queue<Node*, std::vector<Node*>, comp> pq;
// create a root node and calculate its cost
Node* root = newNode(initial, x, y, x, y, 0, NULL);
root->cost = calculateCost(initial, final);
// Add root to list of live nodes;
pq.push(root);
// Finds a live node with least cost,
// add its childrens to list of live nodes and
// finally deletes it from the list.
while (!pq.empty())
{
// Find a live node with least estimated cost
Node* min = pq.top();
// The found node is deleted from the list of
// live nodes
pq.pop();
// if min is an answer node
if (min->cost == 0)
{
// print the path from root to destination;
printPath(min);
return;
}
// do for each child of min
// max 4 children for a node
for (int i = 0; i < 4; i++)
{
if (isSafe(min->x + row[i], min->y + col[i]))
{
// create a child node and calculate
// its cost
Node* child = newNode(min->mat, min->x,
min->y, min->x + row[i],
min->y + col[i],
min->level + 1, min);
child->cost = calculateCost(child->mat, final);
// Add child to list of live nodes
pq.push(child);
}
}
}
}
// Driver code
int main()
{
// Initial configuration
// Value 0 is used for empty space
int initial[N][N] =
{
{1, 2, 3},
{5, 6, 0},
{7, 8, 4}
};
// Solvable Final configuration
// Value 0 is used for empty space
int final[N][N] =
{
{1, 2, 3},
{5, 8, 6},
{0, 7, 4}
};
// Blank tile coordinates in initial
// configuration
int x = 1, y = 2;
solve(initial, x, y, final);
return 0;
}
xxxxxxxxxx
import torch
import torch.nn as nn
import torch.nn.functional as F
class BidirectionalLSTM(nn.Module):
def __init__(self, n_in, n_hidden, n_out):
super(BidirectionalLSTM, self).__init__()
self.rnn = nn.LSTM(n_in, n_hidden, bidirectional=True)
self.embedding = nn.Linear(n_hidden * 2, n_out)
def forward(self, input):
recurrent, _ = self.rnn(input)
T, b, h = recurrent.size()
t_rec = recurrent.view(T * b, h)
output = self.embedding(t_rec) # [T * b, nOut]
output = output.view(T, b, -1)
return output
xxxxxxxxxx
#include<iostream>
#include<math.h>
#define vL 0.4
using namespace std;
struct generator
{
float IL,theat,Pout;
};
typedef struct generator GENERATOR;
int main()
{
int i=0,n;
float sum=0,avg,low,high,il,theat;
cout<<"Enter the value for 'n' : ";
cin>>n;
GENERATOR g[n];
while(i<n)
{
cout<<"Enter Voltage and Current";
cin>>il>>theat;
if(il>0&&theat>=35&&theat<=45){
g[i].IL=il;
g[i].theat=theat;
g[i].Pout=sqrt(3)*il*cos(theat);
cout<<g[i].Pout;
if(i==0)
{
low=g[i].Pout;
high=g[i].Pout;
}
++i;
}else
cout<<"\nEnter Valid data(il>0 and theat>=-35 and theat<=45)";
}
i=1;
while(i<n)
{
if(low>g[i].Pout)
low=g[i].Pout;
if(high<g[i].Pout)
high=g[i].Pout;
sum+=g[i].Pout;
i++;
}
avg=sum/n;
cout<<"\nThe Lowest Output Power :"<<low;
cout<<"\nThe highest Output Power :"<<high;
cout<<"\nThe Average Output Power :"<<avg;
}
OUTPUT
xxxxxxxxxx
void decrypt(string &text, string &key){
for(size_t i=0;i<text.length()-1;i++){
for (int j=0;j<26;j++){
if (text[i]==key[j]){
text[i]=j+97;
break;
}
}
}
}
xxxxxxxxxx
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void obradi(const vector<int>& particija, int k) {
for (int i = 0; i < k; i++)
cout << particija[i] << " ";
cout << endl;
}
void obradiParticije(int n, int smax, vector<int>& particija, int k) {
if (n <= 0)
obradi(particija, k);
else {
for (int s = 1; s <= min(n, smax); s++) {
particija[k] = s;
obradiParticije(n-s, s, particija, k+1);
}
}
void obradiParticije(int n) {
vector<int> particija(n);
obradiParticije(n, n, particija, 0);
}
int main() {
int n;
cin >> n;
obradiParticije(n);
xxxxxxxxxx
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
struct n{
int d;
struct n*next;
};
void push(struct n**headref, int new_d)
{
struct n* new_node=new n;
new_node->d=new_d;
new_node->next=(*headref);
(*headref)=new_node;
}
float avgofnodes(struct n*head)
{
if(!head){return -1;}
int c=0;
int s=0;
float avg =0.0;
struct n*now=head;
while(now!=NULL)
{
c++;
s+=now->d;
now=now->next;
}
avg=(double)s/c;
return avg;
}
int main()
{
struct n*head=NULL;
push(&head , 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout<<"Average of nodes = "<<avgofnodes(head);
return 0;
}
xxxxxxxxxx
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
struct n{
int d;
struct n*next;
};
void push(struct n**headref, int new_d)
{
struct n* new_node=new n;
new_node->d=new_d;
new_node->next=(*headref);
(*headref)=new_node;
}
float avgofnodes(struct n*head)
{
if(!head){return -1;}
int c=0;
int s=0;
float avg =0.0;
struct n*now=head;
while(now!=NULL)
{
c++;
s+=now->d;
now=now->next;
}
avg=(double)s/c;
return avg;
}
int main()
{
struct n*head=NULL;
push(&head , 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout<<"Average of nodes = "<<avgofnodes(head);
return 0;
}
xxxxxxxxxx
#include<iostream>
using namespace std;
int dado() {
return rand() % 6 + 1;
}
void casasEspeciais( int player){
switch (player) {
case 3:
player = 16;
break;
case 5:
player = 7;
break;
case 15:
player = 25;
break;
case 18:
player = 20;
break;
case 21:
player = 32;
break;
case 12:
player = 2;
break;
case 14:
player = 11;
break;
case 17:
player = 4;
break;
case 31:
player = 19;
break;
case 35 :
player = 22;
break;
default:
break;
}
}
void regrasDoJogo(int p1,int p2, int &c1, int &c2) {
p1 = 1;
p2 = 1;
bool game = true;
int jogadas = 0;
while (game)
{
p1 += dado();
casasEspeciais(p1);
if (p1 >= 36) {
c1++;
game = false;
break;
}
p2 += dado();
casasEspeciais(p2);
if (p2 >= 36){
c2++;
game = false;
break;
}
jogadas++;
};
}
int player1, player2;
int count1, count2;
float TOTAL = 10000.0f;
int main() {
count1 = 0;
count2 = 0;
srand(time(NULL));
for (int i = 0; i < TOTAL; i++){
regrasDoJogo(player1, player2, count1, count2);
}
cout << "vitorias do p1:" << count1 << endl;
cout << "vitorias do p2:" << count2 << endl;
float porcentagem = (count1 / TOTAL)*100;
cout << "a probabilidade do primeiro ganhar é de:" << porcentagem <<"%"<< endl;
system("pause");
return 0;
}
xxxxxxxxxx
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int z,a[3],b[3],al=0,bo=0;
for(int i=0;i<3;i++)
cin>>a[i];
for(int i=0;i<3;i++)
cin>>b[i];
for(int i=0;i<3;i++)
{
if(a[i]==b[i])
{z++;}
else
if(a[i]>b[i])
al++;
else
bo++;
}
cout<<al<<" "<<bo;
return 0;
}using namespace std;
// Function to convert Indian Numeric
// System to International Numeric System
string convert(string input)
{
// Length of the input string
int len = input.length();
// Removing all the separators(, )
// From the input string
for (int i = 0; i < len; i++) {
if (input[i] == ',') {
input.erase(input.begin() + i);
len--;
i--;
}
}
// Initialize output string
string output = "";
int ctr = 0;
// Process the input string
for (int i = len - 1; i >= 0; i--) {
ctr++;
output = input[i] + output;
// Add a separator(, ) after
// every third digit
if (ctr % 3 == 0 && ctr < len) {
output = ',' + output;
}
}
// Return the output string back
// to the main function
return output;
}
// Driver Code
int main()
{
string input1 = "12,34,56,789";
string input2 = "90,05,00,00,000";
cout << convert(input1) << endl;
cout << convert(input2) << endl;
}