Sunday, April 17, 2016

Java Program: Word Length

The program is to read a line of text, which is divided into several words by spaces and ends with '.'. You should output the length of each word in this line of text. The words here have nothing to do with language, which include a variety of symbols. For example, "it's" is a word, whose length is 4. Note that there may be continuous spaces in the row.
 
Input format:
 
Enter a line of text ending with '.' in a row, but '.' can not be calculated in the last word length.
 
Output format:
 
Output the lengths of each word corresponding to this line of text in one line, every 2 length divided by a space. There are no spaces at the end of the line.
 
Input sample:
 
It's great to see you here.
 
Output sample:
4 5 2 3 3 4

Here is my program.

 //Word Length
import java.util.Scanner;

public class Main {
   
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        int count = 0, i;
        boolean isFirst = true;
        for (i=0;i<s.length()-1;)
        {
            while (s.charAt(i)==(' '))
                i++;
            while (s.charAt(i)!=('.')&&s.charAt(i++)!=(' '))
                count++;
            if (isFirst)
            {
                System.out.print(count);
                isFirst = false;
            }
            else
                System.out.print(" "+count);
            count = 0;
        }
    }
}

Java Program: Polynomial Addition

A polynomial can be expressed as the sum of every product, of the power of x and the coefficient. For example:

2x6+3x5+12x3+6x+20

Now, your program should read two polynomials, then output their sum, which is to add up corresponding coefficients and print the new polynomial.

The program is to deal with the power to a maximum of 100.

Input format:

Two polynomials are to be entered, the input format of each polynomial is as follows:
In each row two integers will be input, the first of which represents a power, the second represents the coefficient of this power, and all the coefficients are integers. The first row must be the highest power, the last row must be the 0 power.

Note that rows between the first and last row are not in accordance with the order of powers; if a power(not 0 power) coefficient is 0, this power will not appear in the input data; if 0 power coefficient is 0, it will appear in the input data.

Output format:

From the beginning of the highest power down to the 0 power, such as:

2x6+3x5+12x3-6x+20

Note that the x is a lowercase x, and there are no spaces between all of the symbols. If the coefficient of a power is 0, don't need to print it.

Sample input:

6 2
5 3
3 12
1 6
0 20
6 2
5 3
2 12
1 6
0 20

Sample output
 
4x6+6x5+12x3+12x2+12x+40

Can you write this program?Whether you  hava had a try, I had one.

//Polynomial Addition
import java.util.Scanner;

public class Main {
   
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        final int SIZE = 101;
        int[] m = new int[SIZE];
        int i, j;
        i=in.nextInt();
        j = i;
        while (i>0)
        {
            m[i]=in.nextInt();
            i=in.nextInt();
        }
        m[0] = in.nextInt();
        i=in.nextInt();
        if (j<i)
            j = i;
        while (i>0)
        {
            m[i] += in.nextInt();
            i=in.nextInt();
        }
        m[0] += in.nextInt();
        while (m[j]==0)
        {
            j--;
            if (j<0)
            {
                System.out.print(0);
                break;
            }
        }
        if (j==0)
            System.out.print(m[0]);
        else if (j==1)
        {
            if (m[j]!=-1&&m[j]!=1)
                System.out.print(m[j]);
            if (m[j]==-1)
                System.out.print("-");
            System.out.print("x");
        }
        else if (j>1)
        {
            if (m[j]!=-1&&m[j]!=1)
                System.out.print(m[j]);
            if (m[j]==-1)
                System.out.print("-");
            System.out.print("x"+j);
        }
        while (--j>=0)
        {
            while (j>=0&&m[j]==0)
                j--;
            if (j<0)
                break;
            if (m[j]>0)
                System.out.print("+");
            if (j>1)
            {
                if (m[j]!=-1&&m[j]!=1)
                    System.out.print(m[j]);
                if (m[j]==-1)
                    System.out.print("-");
                System.out.print("x"+j);
            }
               
            else if (j==1)
            {
                if (m[j]!=-1&&m[j]!=1)
                    System.out.print(m[j]);
                if (m[j]==-1)
                    System.out.print("-");
                System.out.print("x");
            }
            else
                System.out.print(m[j]);
        }   
    }
}