GIU_2478_61_14171_2023-12-02T08_18_05
GIU_2478_61_14171_2023-12-02T08_18_05
* * * * * *
* *
* *
* *
* *
* * * * * *
Solution:
import j a v a . u t i l . S c a n n e r ;
public c l a s s Square
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t ( " E n t e r s i d e : " ) ;
int n = sc . n e x t I n t ( ) ;
int i , j ;
f o r ( i = 1 ; i <= n ; i ++)
{
f o r ( j = 1 ; j <= n ; j ++)
{
i f ( i == 1 | | i == n | | j == 1 | | j == n )
System . o u t . p r i n t ( " * " ) ;
else
System . o u t . p r i n t ( " " ) ;
}
System . o u t . p r i n t l n ( ) ;
}
}
}
1
123
1
12345
1234567
123456789
Solution:
import j a v a . u t i l . * ;
public c l a s s TriangleNumbers {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
String line ;
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r a number b e t w e e n 1 and 9 " ) ;
int n = sc . n e x t I n t ( ) ;
int i , j , z ;
f o r ( i = 1 ; i <= ( n / 2 ) + 1 ; i ++)
{
f o r ( z = ( n /2) − i + 1 ; z > 0 ; z−−)
System . o u t . p r i n t ( " " ) ;
f o r ( j = 1 ; j <= ( i * 2 −1); j ++)
System . o u t . p r i n t ( j ) ;
System . o u t . p r i n t l n ( ) ;
}
}
}
Another s o l u t i o n :
import j a v a . u t i l . * ;
public c l a s s TriangleNumbers {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
String line ;
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r a number b e t w e e n 1 and 9 " ) ;
int n = sc . n e x t I n t ( ) ;
int i , j , z ;
i n t odd = 1 ;
f o r ( i = 1 ; i <= ( n / 2 ) + 1 ; i ++)
{
f o r ( j = i ; j <=n / 2 ; j ++)
System . o u t . p r i n t ( " " ) ;
f o r ( z = 1 ; z <= odd ; z ++)
System . o u t . p r i n t ( z ) ;
System . o u t . p r i n t l n ( ) ;
odd +=2;
}
}
}
2
Exercise 5-3 Word Count
Write a program that reads a sentence and a word from the user and finds the number of occurrences of the given
word in the sentence. For example, the following could be a run of your program
Solution:
import j a v a . u t i l . * ;
p u b l i c c l a s s WordFinder {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r t h e s e n t e n c e " ) ;
S t r i n g l i n e = sc . nextLine ( ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r t h e key word " ) ;
S t r i n g word = s c . n e x t L i n e ( ) ;
w h i l e ( i < l i n e L e n g t h −1)
{
/ / Get word by word
do
{
c = l i n e . charAt ( p o i n t e r ) ;
p o i n t e r ++;
} w h i l e ( ( c ! = ’ ’ ) && ( c ! = ’ ; ’ ) && ( c ! = ’ . ’ )
&& ( c ! = ’ , ’ ) && ( p o i n t e r < l i n e L e n g t h ) ) ;
/ / s u b s t r i n g ( x , y ) r e t u r n s t h e word s t a r t i n g f r o m x t o y−1
toBeCompared = l i n e . s u b s t r i n g ( i , p o i n t e r − 1 ) ;
3
}
i = pointer ;
}
System . o u t . p r i n t l n ( " Number o f O c c u r e n c e : " + w o r d C o u n t e r ) ;
}
}
Solution:
import j a v a . u t i l . * ;
public class DigitCounter {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
System . o u t . p r i n t l n ( " P l e a s e e n t e r t h e f i r s t number " ) ;
i n t num1 = s c . n e x t I n t ( ) ;
i n t c o u n t , num2 ;
w h i l e ( num1 ! = −1)
{
count = 1;
num2 = num1 ;
w h i l e ( num2 / 1 0 ! = 0 )
{
num2 = num2 / 1 0 ;
c o u n t ++;
}
System . o u t . p r i n t l n ( " Number o f d i g i t s i n " + num1 + " i s " + c o u n t ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r t h e n e x t number " ) ;
num1 = s c . n e x t I n t ( ) ;
}
}
}
4
Exercise 5-5 Divisors
To be solved in Labs
Which integer between 1 and 10000 has the largest number of divisors, and how many divisors does it have? Write
a program to find the answers and print out the results. It is possible that several integers in this range have the
same, maximum number of divisors. Your program has to print out one of them.
Solution:
public class MostDivisors {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
int maxDivisors = 1;
i n t numWithMax = 1 ;
f o r ( i n t n = 2 ; n <= 1 0 0 0 0 ; n ++)
{
int divisorCount = 0;
f o r ( i n t d = 1 ; d <= n ; d ++)
{
i f ( n % d == 0 )
d i v i s o r C o u n t ++;
}
Solution:
import j a v a . u t i l . * ;
public c l a s s ExtractNumbers {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r y o u r s t r i n g " ) ;
5
S t r i n g s = sc . nextLine ( ) ;
int flag = 0;
f o r ( i n t i = 0 ; i < s . l e n g t h ( ) ; i ++ )
{
/ / t h e end o f t h e s t r i n g i s n o t r e a c h e d and t h e r e i s a number b e t w e e n 0 and
w h i l e ( i < s . l e n g t h ( ) && s . c h a r A t ( i ) <= ’ 9 ’ && s . c h a r A t ( i ) >= ’ 0 ’ )
{
System . o u t . p r i n t ( s . c h a r A t ( i ) ) ;
i ++;
f l a g ++;
}
i f ( f l a g > 0)
{
flag = 0;
System . o u t . p r i n t l n ( ) ;
}
}
}
}
a) Given a String containing uppercase characters (A-Z), write a Java program that compresses repeated ’runs’
of the same character by storing the length of that run.
Example:
Input: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
Output: 12W1B12W3B24W1B14W
Solution:
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
String input = "
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
";
i n t i =0;
i n t count =0;
char c u r r e n t ;
S t r i n g o u t p u t =" " ;
while ( i < i n p u t . l e n g t h ( ) )
{
c u r r e n t = input . charAt ( i ) ;
w h i l e ( i < i n p u t . l e n g t h ( ) && i n p u t . c h a r A t ( i ) == c u r r e n t )
{
c o u n t ++;
i ++;
}
6
System . o u t . p r i n t l n ( " I n p u t : " + i n p u t ) ;
System . o u t . p r i n t l n ( " O u t p u t : " + o u t p u t ) ;
Input: 12W1B12W3B24W1B14W
Output: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
Solution:
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S t r i n g i n p u t = " 12W1B12W3B24W1B14W" ;
i n t i =0;
i n t count =0;
char c u r r e n t ;
S t r i n g o u t p u t =" " ;
S t r i n g temp= " " ;
while ( i < i n p u t . l e n g t h ( ) )
{
c u r r e n t = input . charAt ( i ) ;
/ / f i n d count n
w h i l e ( i < i n p u t . l e n g t h ( ) && i n p u t . c h a r A t ( i ) <=57 && i n p u t
. c h a r A t ( i ) >=48) / / i t ’ s a d i g i t
{
temp += i n p u t . c h a r A t ( i ) ;
i ++;
}
c o u n t = I n t e g e r . p a r s e I n t ( temp ) ;
/ / get character
c u r r e n t = input . charAt ( i ) ;
/ / concatenate n times
f o r ( i n t j = 0 ; j < c o u n t ; j ++)
o u t p u t += c u r r e n t ;
/ / r e s e t and u p d a t e
temp= " " ;
count =0;
i ++;
}
System . o u t . p r i n t l n ( " I n p u t : " + i n p u t ) ;
System . o u t . p r i n t l n ( " O u t p u t : " + o u t p u t ) ;
7
s1 = "abc"
s2 = "ababc"
Output: true
s1 = "a"
s2 = "ababc"
Output: true
s1 = "abc"
s2 = "ababa"
Output: false
Solution:
import j a v a . u t i l . * ;
public class subString {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
S t r i n g s1 = sc . n e x t L i n e ( ) ;
S t r i n g s2 = sc . n e x t L i n e ( ) ;
boolean f = f a l s e ;
f o r ( i n t i = 0 ; i < s 2 . l e n g t h ( ) ; i ++)
{
S t r i n g temp = " " ;
f o r ( i n t j = 0 ; j < s 1 . l e n g t h ( ) && ( i + s 1 . l e n g t h ( ) ) <= s 2 . l e n g t h ( ) ; j ++)
{
temp += s 2 . c h a r A t ( i + j ) ;
}
i f ( temp . e q u a l s ( s 1 ) )
f = true ;
}
System . o u t . p r i n t l n ( f ) ;
}
}