xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
// Function to check if elements are
// pairwise consecutive in queue
bool pairWiseConsecutive(queue<int> q)
{
// Transfer elements of q to aux.
stack<int> aux;
while (!q.empty()) {
aux.push(q.front());
q.pop();
}
// Again transfer the
// elements of aux to aux2
stack<int> aux2;
while (!aux.empty()) {
aux2.push(aux.top());
aux.pop();
}
// Traverse aux2 and see if
// elements are pairwise
// consecutive or not. We also
// need to make sure that original
// content is retained.
bool result = true;
while (aux2.size() > 1) {
// Fetch current top two
// elements of aux2 and check
// if they are consecutive.
int x = aux2.top();
aux2.pop();
int y = aux2.top();
aux2.pop();
if (abs(x - y) != 1)
result = false;
// Push the elements to queue
q.push(x);
q.push(y);
}
if (aux2.size() == 1)
q.push(aux2.top());
return result;
}
// Driver program
int main()
{
// Pushing elements into the queue
queue<int> q;
q.push(4);
q.push(5);
q.push(-2);
q.push(-3);
q.push(11);
q.push(10);
q.push(5);
q.push(6);
if (pairWiseConsecutive(q))
cout << "Yes" << endl;
else
cout << "No" << endl;
// Printing the original queue
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
cout << endl;
return 0;
}
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
int recurFunct(int n)
{
if (n==1)
return 1;
return recurFunct(n-1) + 2*n-1;
}
int main()
{
int n;
cin>>n;
for (int i=2; i<=n+1; i++)
{
cout<<recurFunct(i)<<" ";
if (i%8 ==0 )
cout<<"\n";
}
return 0;
}
xxxxxxxxxx
#include<bits/stdc++.h>
using namespace std;
void printWords(string str)
{
// word variable to store word
string word;
// making a string stream
stringstream iss(str);
// Read and print each word.
while (iss >> word){
reverse(word.begin(),word.end());
cout<<word<<" ";
}
}
// Driver code
int main()
{
string s = "GeeksforGeeks is good to learn";
printWords(s);
return 0;
}
// This code is contributed by Nikhil Rawat
xxxxxxxxxx
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
float srednia, suma=0;
int ilosc_liczb=0;
int liczba;
cout<<"program obliczajacy srednia liczb parzystych (0 – konczy wpis): "<<endl;
do
{
cout<<"Podaj liczbe: ";
cin>>liczba;
if (liczba%2!=0 && liczba!=0) // jesli liczba jest nieparzysta
{
cout<<"Podaj liczbe: ";
cin>>liczba;
}
if (liczba!=0) //jesli liczba jest jakakolwiek liczba parzysta
{
suma=suma+liczba;
ilosc_liczb++;
}
}
while(liczba!=0);
srednia=suma/ilosc_liczb;
cout<<"Srednia: "<<srednia;
getch();
return 0;
}
xxxxxxxxxx
public class Mythread {
public static void main(String[] args) {
Runnable r = new Runnable1();
Thread t = new Thread(r);
t.start();
Runnable r2 = new Runnable2();
Thread t2 = new Thread(r2);
t2.start();
}
}
class Runnable2 implements Runnable{
public void run(){
for(int i=0;i<11;i++){
if(i%2 == 1)
System.out.println(i);
}
}
}
class Runnable1 implements Runnable{
public void run(){
for(int i=0;i<11;i++){
if(i%2 == 0)
System.out.println(i);
}
}
xxxxxxxxxx
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e9+5;
int t,n,x;
signed main(){
cin >> t;
while (t--){
cin>>n;
int sum=0;
for(int i=0;i<n;++i){
cin>>x;
sum+=x;
}
if(sum<n) cout<<1<<'\n';
if(sum==n) cout<<0<<'\n';
if(sum>n) cout<<sum-n<<'\n';
}
}