Posts

Showing posts from December, 2015

UVA 10235 - Simply Emirp Solution

UVA 10235 - Simply Emirp Solution /* It is a simple and easy problem. But has critical test case. Critical case:  If a number is prime and reverse number is equal to the given number then the output is "prime" not "emirp". For example: Given number 11. 11 number is prime. But reverse number is also 11.So, it is not "emirp". It is prime. */ Code: #include<iostream> #include<string> #include<string.h> #include<algorithm> #define MAX 1000001 using namespace std; bool prime[MAX]; int reverseInt(int num) {     string str=to_string(num);     reverse(str.begin(),str.end());     num=stoi(str);     return num; } int main() {     int i,j,x;     memset(prime,1,sizeof(prime));     for(i=4;i<MAX;i+=2)         prime[i]=0;     for(i=3;i<1001;i+=2)     {       ...

UVA 389 - Basically Speaking Solution

  UVA 389 - Basically Speaking Solution #include<iostream> #include<string> using namespace std; string input; int m,n; int convertToDes() {     int i,j,k,len,sum,temp,val,mul;     len=input.length();     char ch;     bool flag=false;     j=0;     sum=0;     mul=1;     for(i=len-1;i>=0;i--)     {         ch=input[i];         if(ch>='0' && ch<='9')             val=ch-48;         else             val=ch-55;         if(flag)             mul=mul*m;         flag=true;         sum=sum+val*mul;     ...

UVA 11479 Is this the easiest problem? Solution

UVA 11479 Is this the easiest problem? #include<iostream> using namespace std; int main() {     long int test,a,b,c,count;     cin>>test;     count=1;     while(count<=test)     {         cin>>a>>b>>c;         cout<<"Case "<<count++<<": ";         if(a+b>c && a+c>b && b+c>a)         {             if(a==b && b==c)                 cout<<"Equilateral"<<endl;             else if(a==b || b==c || c==a)                 cout<<"Isosceles"<<endl;         ...

UVA 11340 Newspaper Solution

UVA 11340 Newspaper Solution #include<iostream> #include<string.h> #include<map> #include<stdio.h> using namespace std; map<char,int> mymap; map<char,int>::iterator mit; int main() {     freopen("input.txt","r",stdin);     int i,k,m,test,value;     char ch;     double sum;     char str[10010];     cin>>test;     while(test--)     {         cin>>k;         mymap.clear();         while(k--)         {             cin>>ch>>value;             mymap[ch]=value;         }         cin>>m;         getchar();  ...

UVA 493 Knight Moves Solution

#include<iostream> #include<string.h> #include<algorithm> #include<queue> using namespace std; typedef pair<int,int> pii; typedef pair<pii,int> piii; bool visit[8][8]; int m,n; int dx[]={-2,-2,-1,-1,1,1,2,2,},dy[]={-1,1,-2,2,-2,2,-1,1}; int bfs(char *str1,char *str2) {     queue<piii> q;     pii p1;     piii p2;     int x=str1[0]-97,y=str1[1]-49,xf=str2[0]-97,yf=str2[1]-49;     p1=pair<int,int>(x,y);     p2=pair<pii,int>(p1,0);     q.push(p2);     int i,xx,yy,zz;     while(!q.empty())     {         p2=q.front();         q.pop();         p1=p2.first;         zz=p2.second+1;         x=p1.first;         y=p1.sec...

UVA 572 Oil Deposits Solution

UVA 572 Oil Deposits Solution #include<iostream> #include<map> #include<algorithm> #include<queue> using namespace std; typedef pair<int,int> pii; char arr[100][100]; bool visit[100][100]; int m,n; int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={-1,0,1,-1,1,-1,0,1}; void bfs(int x,int y) {     queue<pii> q;     int i,xx,yy;     pii p=pair<int,int>(x,y);     q.push(p);     while(!q.empty())     {         p=q.front();         q.pop();         x=p.first;         y=p.second;         for(i=0;i<8;i++)         {             xx=x+dx[i];             yy=y+dy[i];          ...

UVA 11470 Square Sums Solution

UVA 11470 Square Sums Solution       #include<iostream> using namespace std; int arr[10][10],n,res[5]; int calculateSquare(int x) {     int sum=0,i,j;     i=n-1-x;     for(j=x;j<n-x;j++)         sum=sum+arr[x][j]+arr[i][j];     j=n-1-x;     for(i=x+1;i<j;i++)         sum=sum+arr[i][x]+arr[i][j];     return sum; } int main() {     int i,j,k,sum,lim,count,test=1;     while(cin>>n)     {         if(n==0)             break;         for(i=0;i<n;i++)         {             for(j=0;j<n;j++)             {        ...