#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define MOD 1000000007 #define MOD1 998244353 #define st segment_tree #define jor pair #define koj find #define jol compress #define as insert #define sa cout<<"YES"<<endl #define jh cout<<"NO"<<endl #define noob next_permutation #define ub upper_bound #define moch erase #define atz substr #define jor pair #define pore next_permutation #define elif else if #define maxi(v) *max_element(all(v)) #define mini(v) *min_element(all(v)) #define nich lower_bound #define ma(c,x) (upper_bound(c.begin(),c.end(),x)-c.begin()) #define pa(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define pb push_back #define no ~ #define khali empty #define ppb pop_back //#define mp make_pair #define sn segNode #define pq priority_queue #define ff first #define ss second #define PI 3.141592653589793238462 #define sp " " #define sez(x) (int)(x.size()) #define all(x) x.begin(),x.end() #define beloo() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define forup(i,a,b) for(ll i = (ll)(a) ; i <(ll)(b) ; i++) #define fordo(i,a,b) for(ll i = (ll)(a) ; i >= (ll)(b) ; i--) #define ain(a,n) for(ll i=0 ; i <(ll)n ; i++) cin >> a[i] #define aout(a,n) for(ll i = 0 ; i < (ll)n ; i++) cout << a[i] << " "; #define ain2(a , n , m) for(ll i = 0 ; i < n ; i++){for(ll j = 0 ; j < m ; j++){cin >> a[i][j];}} #define aout2(a , n , m) for(ll i = 0 ; i < n ; i++){for(ll j = 0 ; j < m ; j++){cout << a[i][j] << " ";} cout << endl;} #define __gcd(a,b) #define INF 0X3F3F3F3F //#define endl '\n' typedef unsigned long long ull; typedef long double lld; typedef long long ll; typedef tree<ll, null_type, less_equal<ll>,rb_tree_tag, tree_order_statistics_node_update> pbds; ll gosa(ll a, ll b) {if (b > a) {return gosa(b, a);} if (b == 0) {return a;} return gosa(b, a % b);} ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} void extendgosa(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgosa(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} ll mominv(ll a, ll b) {ll arr[3]; extendgosa(a, b, arr); return arr[0];} // prime b noy tader jonno ll mominvprime(ll a, ll b) {return expo(a, b - 2, b);} bool revsort(ll a, ll b) {return a > b;} void swap(int &x, int &y) {int temp = x; x = y; y = temp;} ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;} void google(int t) {cout << "Case #" << t << ": ";} vector<ll> shiva(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;} ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mominvprime(b, m), m) + m) % m;} //sudhu prime m er jonno ll phinkata(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} const ll hoi=(6e5+5); const ll poi=(1e9); const ll boi=(998244353); void op(ll l,ll r,ll c){ if(l>r) return; ll mi=(l+r)/2; cout<<"-"<<sp<<mi<<endl; ll m; cin>>m; if(m==c) cout<<"!"<<sp<<mi<<endl; elif(m>c) op(l,mi-1,c); else op(mi+1,r,c); } void cf() { ll c; cin>>c; op(1,poi,c); } int main() { beloo(); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); ll t=1; cin>>t; forup(i,1,1+t) { //google(i); cf(); } }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}