by Jesmin Akther | Jan 10, 2019 | Problem Solving, UVa
#include <stdio.h>
#include<iostream>
#include<math.h>
#define max 1000005
using namespace std;
bool flag[max];
int primes[max];
int cnt;
int sieve(int z)
{
int i,j;
for(i=2; i<=max; i++)
{
primes[i]=1;
}
for(i=2; i<=sqrt(max); i++)
{
for(j=2; i*j<=max; j++)
{
primes[i*j]=0;
}
}
}
int main()
{
int count,b,k,n;
sieve(max);
while(scanf("%d",&n) && n)
{
count=0;
for(k=2; k<n; k++)
{
b=n-k;
if(primes[b]&& primes[k])
{
count++;
break;
}
}
if(count>0)
{
printf("%d:\n",n);
printf("%d+%d\n",k,b);
}
else
{
printf("%d:\n",n);
printf("NO WAY!\n");
}
}
return 0;
}
by Jesmin Akther | Jan 10, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
long int temp,male,female,total,y;
int i;
while(cin>>y && y>=0)
{
male=0,female=1;
for( i=0;i<y;i++)
{
temp=male;
male=female+temp;
female=temp+1;
}
cout<<male<<" "<<female+male<<endl;
}
return 0;
}
by Jesmin Akther | Jan 10, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m,i,j,k,l,a,b,ans;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&a,&b);
cout<<( (a/3) * (b/3) )<<endl;
}
return 0;
}
by Jesmin Akther | Jan 10, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
int v[10010];
//vector<int>v;
int i,j,n,k,max,price1,a,price2,m,diff,tmp;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>v[i];
//v.push_back(i);
cin>>m;
max=1000000;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if((v[i]+v[j])== m)
{
diff=abs(v[i]-v[j]);
if(diff<max)
{
max=diff;
price1=v[i];
price2=v[j];
}
}
}
if(price1>price2)
{
tmp=price1;
price1=price2;
price2=tmp;
}
printf("Peter should buy books whose prices are %d and %d.\n\n",price1,price2);
}
return 0;
}
by Jesmin Akther | Jan 10, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
#define pi 2*acos(0)
using namespace std;
int main()
{
double s,a,b,c,tri_area,red_out,red_in;
double area_circum,area_inscribed,area_triangle;
while(scanf("%lf %lf %lf", &a, &b, &c)==3)
{
s=(a+b+c)/2;
tri_area=sqrt(s*(s-a)*(s-b)*(s-c));
red_out=((a*b*c)/(4*tri_area));
red_in=(tri_area/s);
area_circum=(pi*pow(red_out, 2))-tri_area;
area_inscribed=(pi*pow(red_in, 2));
tri_area=tri_area-area_inscribed;
printf("%.4lf %.4lf %.4lf\n", area_circum, tri_area, area_inscribed);
}
}
by Jesmin Akther | Jan 10, 2019 | Problem Solving, UVa
#include <stdio.h>
int main()
{
long int dn,r,q;
long int bn[10000],i,j;
while(1)
{
scanf("%ld",&dn);
if(dn<0) break;
else if(dn==0) printf("0\n");
else
{
q=dn;
i=0;
while(q>0)
{
bn[i++]=q%3;
q=q/3;
}
i=i-1;
for(j=i;j>=0;j--)
printf("%ld",bn[j]);
printf("\n");
}
}
return 0;
}
by Jesmin Akther | Jan 6, 2019 | JAVA
String VS StringBuffer VS StringBuilder
String is immutable that is if you want to change a string value another object gets created. On the other hand StringBuffer and StringBuilder are mutable they can change their values.
More about String :
Generally, an object that represents sequence of char values known as String. An array of characters works same as string in Java language. For instance:
-
- char[] charactersType ={‘h’,‘a’,‘r’,‘u’,‘n’};
- String stringType =new String(charactersType );
Equivalent to below statements,
- String stringType=“String Same”;
So when to use String?
Java String class or java.lang.String provides class implements Serializable, Comparable etc interfaces. The Java String is immutable. Therefore it cannot be changed. Whenever want to change any string, a new instance is created everytime. For mutable strings, you can use StringBuffer and StringBuilder classes. If you use string that is not going to change values use a string class then.
More about StringBuffer
StringBuffer is thread safe. mutable and synchronous which is like a String, but can be modified. Certain method calls can be changed length and contents of chartecters. One of the major feature is can handle multiple thread. StringBuffer implements class and object below.
public final class StringBuffer
extends Object
implements Serializable, CharSequence
For multiple threads, synchronized is necessary operations. Two principal StringBuffer activities are the ‘ append’ and ‘insert’ methods, which are overloaded and need to accept data of any type.
For example,
If ‘p defines to a string buffer object whose current character sequence are “Harun”, then the method call p.append(“or”) would cause the string buffer to contain “Harunor”. On the other hand, if p.insert(4, “ni”) would alter the string buffer to contain “harunin”.
when to use StringBuffer?
If your string can change and will be accessed by a multiple thread then use StringBuffer. Because it can deal with multiple threads, synchronizations necessary operations.
More about StringBuilder
StringBuilder is used for multiple thread.It also can represent a mutable sequence of characters which is an alternative to String Class. Very much similar to StringBuffer class. Though StringBuffer class synchronizations are guaranteed, where StringBuilder provides no guarantee of synchronization. Additionally can not smoothly deal multiple thread. Here is the snippet of StringBuilder extandable object and classes that can be implements.
public final class StringBuilder
extends Object
implements Serializable, CharSequence
Constructions and method with StringBuilder overview
StringBuilder(): This constructs with no characters in it and an initial capacity of 16 characters.
StringBuilder(CharSequence seq):This constructs same characters as the specified CharSequence.
StringBuilder(String str): This constructs a string builder initialized to the contents of the specified string.
when to use StringBuilder?
If your string can change lots of logic and operation in construction of the string and only be accessed by a single thread.
Three of topic implements on java given below:
/*Java program toimplement String VS StringBuffer VS StringBuilder */
class draftsbook
{
/* String Concatenation */
public static void cut1(String sString)
{
sString = sString + “harunorr”;
}
/*StringBuilder Concatenation*/
public static void cut2(StringBuilder sStringBuilder)
{
sStringBuilder.append(“harunorr”);
}
/* StringBuffer Concatenation*/
public static void cut3(StringBuffer sStringBuffer )
{
sStringBuffer .append(“harunorr”);
}
public static void main(String[] args)
{
String sString_1 = “jees”;
/* sString_1 is not changed as different refence object*/
cut1(sString_1 );
System.out.println(“String: ” + sString_1 );
StringBuilder sString_2 = new StringBuilder(“jees”);
/* sString_2 changed */
cut2(sString_2);
System.out.println(“StringBuilder: ” + sString_2);
StringBuffer sString_3 = new StringBuffer(“jees”);
/* sString_3 got changed */
cut3(sString_3);
System.out.println(“StringBuffer: ” + sString_3);
}
}
OUTPUT
String: jees
StringBuilder: jeesharunorr
StringBuffer: jeesharunorr
See more Topic on Java OOP with real-time example
Nested class in JAVA with EXAMPLES
What is the defference between String, StringBuffer and StringBuilder.
Java Interface tutorials with Example.
JAVA abstract class with EXAMPLES
What Does Method Signature Mean in Java?
by Jesmin Akther | Jan 6, 2019 | JAVA
What is OOP in Java?
OOP (Object oriented programming) refers to a programming methodology based on objects instead of just functions and procedures. In this blog, guide how to do encapsulation in java program.
What are the main feature of OOP?
Encapsulation is Combination of data and function in a single unit. This is use to hide the implementation details from user. That is why is also known as “data Hiding“ for this hiding approach. The main method for data invoke is setter and getter method.
Inheritance is a process by which object of any class have access of other classes data and methods or all the properties. It is a mechanism in which one object acquires all the properties and behaviors of a parent object. Derived class known as child class. The class that inherits from another class · The base class namely parent class. In the class being inherited from.
Polymorphism refers the ability to take more than one form. And the most common use of polymorphism in OOP is, when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one is considered to be polymorphic.
Abstraction in OOP able to hides implementation details in class. It can also be referred to as modeling and is nearly conjuncted to the ideas of theory and design.
Now discuss about Encapsulation in java:
Encapsulation( known as data hiding) is a mechanism of wrapping the variables and methods together as a single unit. No outside class can access private data member or variable of other class. That is variables of a class hidden from other classes. In order to invoke that variable and method, one can be accessed only through the getter setter methods provided by current class. This way data can only be accessed by public methods thus making the private fields and their implementation hidden for other classes.
Encapsulation achieve −
- Declare the variables of a class as private.
- Provide getter and setter methods in public (modify and view the data values).
- Java encapsulation keep related fields and methods together in order to easy to code and read.
It control the values of data fields.
For example,
class Encap{
private int counts;
public void setCounts(int counts) {
if (counts >= 0) {
this.counts = counts;
}
}
}
Here, counts variable private and applying logic inside the setCounts() method. Can be achieved data hiding using encapsulation. In the above example, if change the length and breadth variable into private, then the access to these fields is restricted.
Following is an example of Encapsulation in Java :
public class MySelf
{
private String name;
private String deptName;
private int age;
public String getName()
{
return name;
}
public String getdeptName()
{
return deptNamename;
}
public int getAge() {
return age;
}
public void setName(String name)
{
this.name = name;
}
public void setdeptName(String dName)
{
this.dName = dName;
}
public void setAge(int age)
{
this.age = age;
}
}
Following MySelf class, if any class wants to access the variables(Name,Age,deptName) should access them through these getters(i.e getName() ,getdName(),getAge()) and setters(i.e setName(),setdName(),setAge()).
The variables of the MySelf class can be accessed using the following program −
public class UseMySelfClass
{
public static void main(String args[])
{
MySelf myself= new MySelf ();
myself.setName(“Harun”);
myself.setdeptName(“ICE”);
myself.setAge(22);
System.out.print(“Name : ” + myself.getName());
System.out.print(“Dept-Name : ” + myself.getdeptName());
System.out.print(” Age : ” + myself.getAge());
}
}
This will produce the following result −
Output
Name : Hello
Dept-Name : ICE
ICE Age : 22
Advantages of encapsulation:
Encapsulation use improves flexibility and re-usability. For instance, In the above implemented code of void setName(String name) ,void setAge(int age) and setdeptName(String dName) can be changed at any point of time. Since the implementation is wholy hidden for other classes, yet they would still access the private field Name using the same methods (setName(String name) and getName()). Therefore, the code can be maintained at any point of time without breaking the classes that uses the code. This improves the re-usability of the class.
The fields can be made read-only, if don’t define setter methods in the class. In other saying, field can be write-only, if don’t define the getter methods in the class, such as,
getName() // provides read-only access
setName() // provides write-only access
Example, If class have only a field or variable that don’t want to be changed, simply define the variable as private and instead of setter, getter. Both just need to define the get method for that variable. As the set method is not present there is no way an outside class can modify the value of that field.
User would know update a field by calling set method and to read a field call get method. set and get methods activity hidden from user.
See more Topic on Java OOP with real-time example
Nested class in JAVA with EXAMPLES
What is the defference between String, StringBuffer and StringBuilder.
Java Interface tutorials with Example.
JAVA abstract class with EXAMPLES
What Does Method Signature Mean in Java?
by Jesmin Akther | Jan 3, 2019 | Database
Subject : SQL
How to find salary of friends whose salary is Second highest;
Table Name: friends
friendsName |
salary |
Nihar |
30000 |
Bihar |
40000 |
Saiyar |
50000 |
Nahar |
55000 |
Query :
SELECT friendsName,MAX(salary) as salary FROM friends
WHERE salary < (SELECT MAX(salary) FROM salary);
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s[200];
int n,i,j,l,a;
while(cin>>n)
{
if(n==0) break;
scanf("%s",s);
l=strlen(s);
a=l/n;
for(i = 0; i < l; i += a) {
for(j = i+a-1; j >= i; j--)
printf("%c",s[j]);
}
puts("");
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
string ch1,ch2;
map<string,string>m;
int n,l,i,j,k;
char c1,c2;
cin>>l>>n;
for(i=0;i<l;i++)
{
cin>>ch1>>ch2;
m[ch1]=ch2;
}
for(i=0;i<n;i++){
cin>>ch2;
if(m[ch2]!="") cout<<m[ch2]<<endl;
else{
c1=ch2[ch2.size()-1];
c2=ch2[ch2.size()-2];
if(c1=='y'&&(c2!='a'&&c2!='e'&&c2!='i'&&c2!='o'&&c2!='u'))
{
ch2.erase(ch2.size()-1,1);
cout<<ch2<<"ies"<<endl;
}
else if(c1=='o'||c1=='s'||c1=='x'||(c1=='h'&&c2=='c')||
(c1=='h' && c2=='s'))
cout<<ch2<<"es"<<endl;
else
cout<<ch2<<"s"<<endl;
}
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int func_tion(int n)
{
int j,sum=0;
if(n<10)
return n;
else
{
for(j=0;; j++)
{
sum+=n%10;
n=n/10;
if(n==0)
break;
}
return func_tion(sum);
}
}
int main()
{
int a,n,res,sum,k;
while(cin>>n && n!=0)
{
cout<<func_tion(n)<<endl;
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
using namespace std;
char ch1[10002];
int main()
{
int n,k,m,value[102],nn;
char ch[302];
double s;
scanf("%d",&n);
while(n--)
{
s=0;
scanf("%d\n",&k);
for(int i=0;i<k;i++)
scanf("%c%d\n",&ch[i],&value[i]);
scanf("%d\n",&nn);
for(int i=0;i<nn;i++)
{
gets(ch1);
int l=strlen(ch1);
for(int j=0;j<k;j++)
{
for(int p=0;p<l;p++)
if(ch[j]==ch1[p])
s+=value[j];
}
}
printf("%0.2lf$\n",s/100.00);
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
char m[1002];
int tc, l, n, i, j, r,sum,a[100];
bool flag;
cin>>tc;
while (tc--)
{
scanf("\r");
gets(m);
l = strlen(m);
cin>>n;
for (i = 0; i < n; i++)
cin>>a[i];
flag = false;
for (i = 0; i < n; i++)
{
r= 0;///remainder
for (j = 0; j < l; j++)
r = (r*10 + (m[j]-'0'))% a[i] ;
if (r)
{
flag = true;
break;
}
}
if (flag) printf("%s - Simple.\n", m);
else printf("%s - Wonderful.\n", m);
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
string n,a;
long long s,b,c,d,l;
while(cin>>n)
{
sort(n.begin(),n.end());
a=n;
if(a[0]=='0')
{
for(int i=1;i<n.size();i++)
{
if(a[i]!='0')
{
swap(a[0],a[i]);
break;
}
}
}
reverse(n.begin(),n.end());
b=atoll(n.c_str());
c=atoll(a.c_str());
if(b<c)
swap(b,c);
s=b-c;
d=s/9;
printf("%lld - %lld = %lld = 9 * %lld\n",b,c,s,d);
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,a,b,i,j,x,y;
bool penalty;
while(cin>>n)
{
if(n==0) break;
penalty=false;
for(i=1;i<n;i++)
{
for(j=0;j<=i;j++)
{
if((i*i*i-j*j*j)==n)
{
cout<<i<<" "<<j<<endl;
penalty=true;
break;
}
}
if(penalty==true)
break;
}
if(penalty==false)
cout<<"No solution\n";
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long int n,m,i,b,count;
while(scanf("%lld %lld",&n,&m)==2)
{
if(n==0 && m==0) break;
count=0;
for(i=n;i<=m;i++)
{
b=sqrt(i);
if(i==(b*b))
count++;
}
cout<<count<<endl;
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
long int ary[2000007];
int main()
{
int n,i,j,k;
while (scanf("%ld",&n)==1)
{
if (n==0)
{
break;
}
for (i=0;i<n;i++)
{
scanf("%ld",&ary[i]);
}
sort(ary,ary+n);
for (i=0;i<n;i++)
{
printf("%ld",ary[i]);
if (i<(n-1))
printf(" ");
}
printf("\n");
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include<stdio.h>
int main()
{
long int t,a,b,c,i;
while(scanf("%ld",&t)==1)
{
for(i=1; i<=t; i++)
{
scanf("%ld %ld %ld", &a, &b, &c);
if ((a+b)<=c || (b+c)<=a || (c+a)<=b || a<=0 || b<=0 || c<=0)
printf("Case %ld: Invalid\n",i);
else if(a==b && b==c&&c==a)
printf("Case %ld: Equilateral\n",i);
else if(a==b || b==c || c==a)
printf("Case %ld: Isosceles\n",i);
else
printf("Case %ld: Scalene\n",i);
}
}
return 0;
}
by Jesmin Akther | Jan 2, 2019 | Problem Solving, UVa
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x1,x2,y1,y2;
while(cin>>x1>>y1>>x2>>y2)
{
if(x1==0 && y1==0 && x2==0 && y2==0)
break;
if(x1==x2 and y1==y2) printf("0\n");
else if( x1==x2 ||y1==y2|| abs(y1-y2)==abs(x1-x2)) printf("1\n");
else if ( abs(y1-y2)!=abs(x1-x2)) printf("2\n");
}
return 0;
}