Cow Multiplication

From Progteam

Revision as of 18:07, 21 September 2008 by MuGMaN (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Checkmark.jpg This problem has been solved by Eric.


Cow Multiplication
Problem Number 3673
Sorter: Eric
Source: USACO 2008 February Bronze
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=3673



Judging Olympiad is problem number 3673 on the Peking University ACM site.


Contents

Problem Information

Problem Name: Cow Multiplication
Problem Number on PKU: 3673
Synopsis: A pairwise multiplication of two integers.

Solver Information

Solver: Eric Hong
Date: September 21, 2008

Trivia

None.

General Strategy

  1. Using Scanner, take the input.
  2. Add the products of digits in two String using nested for loops.


Solution

import java.util.*;

public class Main
{
	public static Scanner in;

	public static void main(String[] args)
	{
		in = new Scanner(System.in);
		doStuff();
	}

	public static void doStuff()
	{
		String s = in.next();
		String t = in.next();
		solve(s, t);
	}

	public static void solve(String s, String t)
	{
		int len1 = s.length();
		int len2 = t.length();
		
		int sum = 0;
		
		for (int i = 0; i < len1; i++)
		{
			int num1 = s.charAt(i) - '0';
			
			for (int j = 0; j <len2; j++)
			{
				int num2 = t.charAt(j) - '0';
				sum += (num1 * num2);
			}
		}
		
		System.out.println(sum);
	}
}


Additional Remarks

Memory: 2064K
Time: 1641MS

This solution may not be as efficient as it can be.

Personal tools