10340 - All in All

10340 - All in All

Problem Link

import java.util.*;
public class uva10340 {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()) {
String s = scn.next();
String t = scn.next();
int b=0,c=0,y;
for(int x=0;x<s.length();x++) {
char a = s.charAt(x);
for(y=b;y<t.length();y++) {
if(a == t.charAt(y)) {
c++;
break; 
}
}
b=y+1;
}
if(c == s.length())
System.out.println("Yes");
else
System.out.println("No");
}
}
}
10346 - Peter's Smokes

10346 - Peter's Smokes

Problem Link

import java.util.*;
public class uva10346 {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNextInt()) {
int n = scn.nextInt();
int k = scn.nextInt();
int t = n;
while(n >= k) {
int c = n%k;
if((n%k)!=0) {
int b = n - c;
n = b/k;
t = t+n;
n = n+c;
}
else {
n = n/k;
t = t+n;
n = n+c;
}
}
System.out.println(t);
}
}
}

Assignment C++ (Example 1)


Assignment C++

Write a program that asks user to enter the type of cakes and the quantity. Based on the selection, display the user’s choice and calculate the total price. If the choice is not in the list, display error message. If the user choose to exit from the system, put exit(0); statement and put #include <stdlib.h>. The program should allow users to make more than one choice. You are required to provide the output for each choice as the given sample output:
Sample output

Solution : 

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
 char choice='Y';
 int cake,quantity,total;

 while(choice=='Y')
 {
  cout << "\n         Eve n Soul Cakes House            "<<endl;
  cout << "###########################################"<<endl;
  cout << "1. Chocolate Moist Cake (1 kg)- RM45       "<<endl;
  cout << "2. Blueberry Cheese Cake (1 kg)- RM40      "<<endl;
  cout << "3. Cupcakes (25 pieces) - RM50             "<<endl;
  cout << "4. Exit                                    "<<endl;

  cout << "Please enter your choice:";
  cin >> cake;

if(cake == 1)
{
cout << "You select Chocolate Moist Cake";
cout << "\nPlease enter quantity : ";
cin >> quantity;
total = quantity * 45;
cout << "The total price is : RM"<<total;

cout << "\n\nDo you want to continue with other options? (Y/N):";
cin >> choice;
}
else
if (cake == 2)
{
cout << "You select Blueberry Cheese Cake";
cout << "\nPlease enter quantity : ";
cin >> quantity;
total = quantity * 40;
cout << "The total price is : RM"<<total;

cout << "\n\nDo you want to continue with other options? (Y/N):";
cin >> choice;
}
else
if (cake == 3)
{
cout << "You select Cupcakes";
cout << "\nPlease enter quantity : ";
cin >> quantity;
total = quantity * 50;
cout << "The total price is : RM"<<total;

cout << "\n\nDo you want to continue with other options? (Y/N):";
cin >> choice;
}
else
if(cake == 4)
exit(0);



 }
 exit(0);
 return 0;
}
C-PROM 2013 (PROBLEM 5: COMPUTATION OF MULTIPLICATION)

C-PROM 2013 (PROBLEM 5: COMPUTATION OF MULTIPLICATION)

PROBLEM 5: COMPUTATION OF MULTIPLICATION


Computes the multiplication of numbers between the two integers (both are inclusive) and the result of the multiplication is divided by 6. The two numbers could be a negative or positive numbers.

Input
The first line of the input contains an integer N (1 ≤ N ≤ 5), the number of test cases. Following the first line are the test cases. Each line in a test case contains a pair of Integer numbers seperated by a space.

The input must be read from standard input.

Output
The output of the program should print the result of the multiplication which divided by 6 and print 0 otherwise.

The output must be written to standard output.

Sample Input
Sample Output

3
3 5
10 11
-3 -1



60
0
-6


Solution : 

import java.util.*;
public class CompMul {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
int x = scn.nextInt();
if(x >= 1 && x<=5) {
for(int b=0;b<x;b++) {
int y = scn.nextInt();
int z = scn.nextInt();
int temp=0;
if(z < y) {
temp=z;
z=y;
y=temp;
}
int total=1;
for(int r=y;r<=z;r++)
total = total*r;
if((total%6)==0)
System.out.println(total);
else
System.out.println("0");
}
}
}
}
C-PROM 2013 (PROBLEM 4: COUNT THE KEYWORD)

C-PROM 2013 (PROBLEM 4: COUNT THE KEYWORD)

PROBLEM 4: COUNT THE KEYWORD


To find out and count manually the keyword in the passage is not easy. Imagine if the passage is more than 10 pages and you have to read through the whole passage from the first page until at the end of the passage but the keyword that you search is not found. You will be frustrated. As an expert in computer programming you are required to write a program to find and count how many times the keyword repeats in the passage.

Input
The first line of input is an Integer number (1 ≤ N ≤ 10) which indicate the test cases (number of passages). The second line is the keyword (String). Following the second line are the test cases. Each line in a test case contains a word, phrase or passage.

The input must be read from standard input.

Output
The output is either YES or NO and followed by how many times the keyword found in the passage.


Sample Input
Sample Output

4
Computer
Yesterday i bought 2 computer and ipad.
Computer and commuter are different things.
Computer Sciences and Computer Multimedia.
Bachelor in Business Computing.





YES 1
YES 1
YES 2
NO 0



Solution :


import java.util.*;
public class KeyWord {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
String ls = System.getProperty("line.separator");
scan.useDelimiter(ls);
int no = scan.nextInt();
String var = scan.next(); 
for(int x=0;x<no;x++) {
String sentence=scan.next();
int total = 0;
StringTokenizer st = new StringTokenizer(sentence); 
while(st.hasMoreTokens()) {
String w = st.nextToken();
if(w.equalsIgnoreCase(var)) 
total = total+1;
}
if(total > 0) 
System.out.println("YES " +total);
else
System.out.println("NO " +total);
}
}
}
C-PROM 2013 (PROBLEM 3 : THE SQUARE MATRIC)

C-PROM 2013 (PROBLEM 3 : THE SQUARE MATRIC)

PROBLEM 3 : THE SQUARE MATRIC


In mathematics, a square matrix is a matrix with a same number of rows and columns. An n-by-n matrix is known as a square matrix of order n. For instance, this is square matrix of order 3:


Write a program that is able to display a square matrix of any order as specified by the user. All the entries of the matrix are 1s except the entries in the main diagonal and anti-diagonal. The entries in both diagonals should be 0s.

Input
Any integer number.

Output
The square matrix of any order as specified by the user with its entries in principal diagonal and anti-diagonal is 0s. The rest of the entries remain to be 1s.

Sample Input
Sample Output
7







4
0 1 1 1 1 1 0
1 0 1 1 1 0 1
1 1 0 1 0 1 1
1 1 1 0 1 1 1
1 1 0 1 0 1 1
1 0 1 1 1 0 1
0 1 1 1 1 1 0

0 1 1 0
1 0 0 1
1 0 0 1
0 1 1 0


Solution : 
import java.util.*;
public class diagonal {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int num=0;
while(scan.hasNextInt()) {
num = scan.nextInt();
int no = num-1;
for(int y=0;y<num;y++) {
for(int z=0;z<num;z++) {
if( z==y || z==(no-y))
System.out.print("0");
else
System.out.print("1");
}
System.out.print("\n");
}
   System.out.println("");
}
}
}
C-PROM 2013 (PROBLEM 2 : BUILD A PYRAMID)

C-PROM 2013 (PROBLEM 2 : BUILD A PYRAMID)

PROBLEM 2 : BUILD A PYRAMID


Build a pyramid is not an easy job. But as a computer experts you are able to build a pyramid within a second. Your task is to enter a number and a character. The material of the pyramid is the character’s entered while the height of the pyramid is the number’s entered.

Input
The first line of the input contains an integer N (1 ≤ N ≤ 5), the number of test cases. Following the first line are the test cases. Each line of a test case contains a non-negative integer number and a character separated by a single space. The non-negative number is smaller than 10 and the character is could be a number, alphabet or symbol.

The input must be read from standard input.

Output
The output of the program should display the design of the pyramid.

The output must be written to standard output.


Sample Input
Sample Output

3
2 *
5 ^
3 @




  *
*  *


        ^
      ^  ^
    ^  ^  ^
  ^  ^  ^  ^
^  ^  ^  ^  ^


     @
  @  @
@  @  @





Solution :

import java.util.*;
public class pyramid {
public static  void main(String [] args) {
Scanner scan = new Scanner(System.in);
int no = scan.nextInt();
for(int a=0;a<no;a++) {
int num = scan.nextInt();
String s = scan.next();
int t=num;
for(int x=1;x<=num;x++) {
for(int y=t; y>0; y--)
System.out.print(" ");
for(int z=0; z<x; z++)
System.out.print(s+" ");
System.out.println("");
t--;
}
System.out.println("\n\n");
}
}
}


C-PROM 2013 (PROBLEM 1 : FACTORIAL OF A NUMBER)

C-PROM 2013 (PROBLEM 1 : FACTORIAL OF A NUMBER)

PROBLEM 1 : FACTORIAL OF A NUMBER


In mathematics, the factorial of a non-negative integer n denoted by n!, is the product of all positive integers less than or equal to n. For example:

6! = 6 x 5 x 4 x 3 x 2 x 1 = 720

Input

The first line of the input contains an integer N (1 ≤N ≤ 5), the number of test cases. Following the first line are the test cases. Each line in a test case contains a non-negative integer number.

The input must be read from standard input.

Output

The output of the program should print the result of the factorial of the numbers.

The output must be written to standard output.

Sample Input
Sample Output

3
6
7
8



720
5040
40320


Solution :

import java.util.*;
public class factorial {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
for(int x=0;x<num;x++) {
int total=1;
int no = scan.nextInt();
for(int y=1;y<=no;y++)
total = total*y;
System.out.print(total + "\n");
}
}
}
10812 - Beat the Spread!

10812 - Beat the Spread!

Problem Link

import java.util.*;
public class uva10812 {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for(int x=0;x<n;x++) {
int z = scn.nextInt();
int y = scn.nextInt();
if((z+y)%2==0 &&(z-y)%2==0) {
if(z < y)
System.out.println("impossible");
else {
int a = (z+y)/2;
int b = a-y;
System.out.println(a +" " +b);
}
}
else
System.out.println("impossible");
}
}
}
401 - Palindromes

401 - Palindromes

Problem Link

import java.util.*;
public class uva401 {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
String pal = "";
while(scn.hasNext()) {
pal = scn.next();
int y = pal.length()-1;
int x,z=0,w=0;
int mid = (pal.length()/2) +1;
for(x=0;x<mid;x++) {
if(z==0) {
char a = pal.charAt(x);
char b = pal.charAt(y-x);
if(a == b)
z=0;
else
z=1;
}
else
break;
}
for(x=0;x<mid;x++) {
if(w==0) {
char a = pal.charAt(x);
char b = pal.charAt(y-x);
if(a=='E'){
if(b=='3')
w=0;
else
w=1;
}
else if(a=='3'){
if(b=='E')
w=0;
else
w=1;
}
else if(a=='S'){
if(b=='2')
w=0;
else
w=1;
}
else if(a=='2'){
if(b=='S')
w=0;
else
w=1;
}
else if(a=='Z'){
if(b=='5')
w=0;
else
w=1;
}
else if(a=='5'){
if(b=='Z')
w=0;
else
w=1;
}
else if(a=='J'){
if(b=='L')
w=0;
else
w=1;
}
else if(a=='L'){
if(b=='J')
w=0;
else
w=1;
}
else if(a=='A'){
if(b=='A')
w=0;
else
w=1;
}
else if(a=='Y'){
if(b=='Y')
w=0;
else
w=1;
}
else if(a=='M'){
if(b=='M')
w=0;
else
w=1;
}
else if(a=='O'){
if(b=='O')
w=0;
else
w=1;
}
else if(a=='T'){
if(b=='T')
w=0;
else
w=1;
}
else if(a=='U'){
if(b=='U')
w=0;
else
w=1;
}
else if(a=='V'){
if(b=='V')
w=0;
else
w=1;
}
else if(a=='W'){
if(b=='W')
w=0;
else
w=1;
}
else if(a=='1'){
if(b=='1')
w=0;
else
w=1;
}
else if(a=='8'){
if(b=='8')
w=0;
else
w=1;
}
else if(a=='X'){
if(b=='X')
w=0;
else
w=1;
}
else if(a=='H'){
if(b=='H')
w=0;
else
w=1;
}
else if(a=='I'){
if(b=='I')
w=0;
else
w=1;
}
else if (a=='0') {
w=1;
}
else
w=1;
}
else
break;
}
if(z==1 && w==1)
System.out.println(pal +" -- is not a palindrome.");
else if(z==0 && w==1)
System.out.println(pal +" -- is a regular palindrome.");
else if(z==1 && w==0)
System.out.println(pal +" -- is a mirrored string.");
else if(z==0 && w==0)
System.out.println(pal +" -- is a mirrored palindrome.");
System.out.println("");
}
}
}
 11727 - Cost Cutting

11727 - Cost Cutting

Problem Link

import java.util.*;
public class Main {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for(int x =1; x<=n;x++) {
int y = scn.nextInt();
int z = scn.nextInt();
int a = scn.nextInt();

if(y >= z && y <= a || y >= a && y <= z)
System.out.println("Case "+x +": " +y);
else if (z >= y && z <= a || z >= a && z <= y)
System.out.println("Case "+x +": " +z);
else if (a >= y && a <= z || a >= z && a <= y)
System.out.println("Case "+x +": " +a);
}
}
}
2013 ACM-ICPC Malaysia National Programming Contest (QUESTION A-TRAILING SIFAR)

2013 ACM-ICPC Malaysia National Programming Contest (QUESTION A-TRAILING SIFAR)

A
TRAILING SIFAR

Problem Description

Sifar is a malay word for zero or 0. In mathematics, trailing sifar is a sequence of 0s in the
decimal representation of a number after which no other digits follow.
The number of trailing sifar in the decimal representation of N! (5 <= N <= 1,000,000) is simply
the multiplicity of the prime factor 5 in N!. Given a decimal integer N, you are to find the
number of trailing sifar for N! For example, 10! = 3,628,800. Thus, the number of trailing sifar
for 10! is 2.

Input

Each line of input contains an integer N where 5 <= N <= 1,000,000. The input is terminated
with a line containing 0.

Output

For each test case, the output contains a line in the format Case #x: M, where x is the case
number (starting from 1) and M is the number of trailing sifar for N!
Example of sample I/O

Sample Input
5
10
118

Sample Output
Case #1: 1
Case #2: 2

Case #3: 27

Cara nak selesaikan soalan ini adalah dengan bahagikan nilai yang diberi kepada 5. Bakinya kita tambah sehinggalah baki yang terakhir no dibahagi itu kurang daripada 5.

Solution :
import java.util.*;
public class acmA {
public static void main(String [] args) {
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int x=0;
while( num != 0) {
int t = num%5;
int d = (num-t)/5;
t =d;
int tot = d;
while(t >= 5) {
t = t%5;
d = (d-t)/5;
t=d;
tot = tot + d;
}
x++;
System.out.println("Case #"+x +": "+tot);
num = s.nextInt();
}
}
}