Wednesday, October 26, 2016

UVa 913

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{

 long long N,x;

 while(cin>>N)
 {
  x = 2*(((N/2)+1)*((N/2)+1)) -3;
  cout<<3*x<<endl;
 }

 return 0;
}

Monday, October 24, 2016

UVa 10499

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{

 ll n,p;

 while(cin>>n)
 {
  if(n<0) break;
  p=(n*100)/4;
  if(n==1) cout<<0<<"%"<<endl;
  else cout<<p<<"%"<<endl;
 }

 return 0;
}

UVa 10170

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 ll s,d,n,i;

 while(cin>>s>>d)
 {
  n=0;
  for(i=s;;i++){

   n+=i;
   if(n>d || n==d) {
    cout<<i<<endl;
    break;
   }
  }
 }
 return 0;
}

UVa 12027

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 
 string a;
 while(cin>>a)
 {
  if(a=="0") break;
  int l,num;

  l=a.length();
  num=a[0]-'0';
  if(l>1 && l%2==0) num = num*10 + a[1]-'0';

  l = (l-1)/2;
  cout<<int(sqrt(num));

  for(int i=0; i<l; i++){
   cout<<0;
  }
  cout<<endl;
  
 }

 return 0;

}

Thursday, October 13, 2016

UVa 371 - Ackermann Functions

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 ll x, y;
 
 int j,k;
 
 while(cin>>x>>y)
 {
   if(x==0 || y==0) break;
   
   vector<ll>a;
   vector<ll>b;
   vector<ll>c;
   
  j=k=0;
  if(x>y) swap(x,y);
  
  for(ll i=x; i<=y; i++ )
  {
   
   ll ti=0;
   ll tn=i;
   
   if(i==1)
   {
    ti=3;
   }
   
   else {
    while(tn!=1)
   {
    if(tn%2==0){
      tn= tn/2;
      ti++;
    }
    else {
     tn=3*tn+1;
     ti++;
    }
   }
   }
   
   a.push_back(i);
   b.push_back(ti);
   c.push_back(ti);
   
  }
   sort(b.begin(),b.end());
  
   for(ll l=0; l<c.size(); l++)
   {
    if(b[b.size()-1]==c[l]) {
     
     cout<<"Between "<<x<<" and "<<y<<", "<<a[l];
     cout<<" generates the longest sequence of "<<b[b.size()-1]<<" values."<<endl;
     break;
    }
   }
 
 }
 return 0;
}

UVa 12149 - Feynman

#include<bits/stdc++.h>
#define ll long long

using namespace std;

ll fynman(ll n)
{
 if(n==0) return 0;
 else return n*n + fynman(n-1);
}

int main()
{
 ll n;
 
 while(cin>>n && n)
 {
  ll f=fynman(n);
  cout<<f<<endl;
 }
 
 return 0;
}

UVa 10469 - To Carry or not to Carry

#include<bits/stdc++.h>

using namespace std;

int  main()
{
 int x,y;
 
 while(cin>>x>>y)
 {
  int c=x^y;
  cout<<c<<endl;
 }
 
 return 0;
}

Tuesday, October 4, 2016

CF 723A

#include<bits/stdc++.h>

using namespace std;

int main()
{
 int x,xx,xxx;
 cin>>x>>xx>>xxx;
 
 int result = (max(x,max(xx,xxx))) -( min(x,min(xx,xxx)));
 
 cout<<result<<endl;
 
 return 0;
}

Sunday, October 2, 2016

Link List - Insertion and Print inserted data

#include<bits/stdc++.h>  

using namespace std;

struct node
 {
  int data;
  int key;
  struct node *next;
 };

struct node *head = NULL;
struct node *current = NULL;

void InsertFirst(int key,int data)
 {
  struct node *link = (struct node*) malloc(sizeof(struct node));
  link->key=key;
  link->data=data;
 
  link->next = head;
  head = link;
 
 } 

void printList()
 { 

  struct node *ptr=head;
 
  while(ptr != NULL)
  {
   cout<<"("<<ptr->key<<","<<ptr->data<<")";
   ptr=ptr->next;
  }
   cout<<endl;
 } 


int main()
{
 int data,key;
 for(int i=0; i<4; i++)
 {
  cin>>data>>key;
  
  InsertFirst(data,key);
 }
 
 printList();
 
 return 0;  
}