The program is to input an integer, whose scope is [-100000, 100000], then, printing out every digit of this integer in Chinese PinYin.
For instance, if the input is 1234, then the output should be as following:
yi er san si
Note that there is a space between the Pinyin of every two digits, but there is no space behind the last digit. When encountering a negative number, add the "fu" at the beginning of the output. For example, -2341 should be output as:
fu er san si yi
Have you had a try?Here is my program.
//Spelling An Integer In Chinese PinYin
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = in.nextInt(), digit = 0;
if (number>=-100000&&number<=100000)
{
if (number == 0)
System.out.print("ling");
else
{
if (number < 0)
{
System.out.print("fu ");
number = -number;
}
int temp = number;
while (temp > 0)
{
temp = temp/10;
digit++;
}
do
{
switch (number/(int)(Math.pow(10, digit-1)))
{
case 0: System.out.print("ling");
break;
case 1: System.out.print("yi");
break;
case 2: System.out.print("er");
break;
case 3: System.out.print("san");
break;
case 4: System.out.print("si");
break;
case 5: System.out.print("wu");
break;
case 6: System.out.print("liu");
break;
case 7: System.out.print("qi");
break;
case 8: System.out.print("ba");
break;
case 9: System.out.print("jiu");
}
if (digit > 1)
System.out.print(" ");
number -= number/(int)(Math.pow(10, digit-1))*(int)(Math.pow(10, digit-1));
digit--;
} while (digit > 0);
}
}
}
}
Saturday, April 16, 2016
Java Program: Sum of Primes
We think that 2 is the first prime, 3 is the second prime, 5 is the third prime, and so on.
Now, two integers n and m are given, 0<n<=m<=200. The program is to calculate the sum of n-th to m-th primes, including the n-th and the m-th prime.
Pay attention that it is to calculate the sum of n-th to m-th primes, but not the primes between n and m.
The program is following.
//Sum of Primes
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt(), count = 0, number = 2, sum = 0;
while (count < m)
{
boolean IsPrime = true;
for (int i=2;i<number;i++)
{
if (number%i==0)
{
IsPrime = false;
break;
}
}
if (IsPrime)
{
count++;
if (count>=n)
sum += number;
}
number++;
}
System.out.println(sum);
}
}
Now, two integers n and m are given, 0<n<=m<=200. The program is to calculate the sum of n-th to m-th primes, including the n-th and the m-th prime.
Pay attention that it is to calculate the sum of n-th to m-th primes, but not the primes between n and m.
The program is following.
//Sum of Primes
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt(), count = 0, number = 2, sum = 0;
while (count < m)
{
boolean IsPrime = true;
for (int i=2;i<number;i++)
{
if (number%i==0)
{
IsPrime = false;
break;
}
}
if (IsPrime)
{
count++;
if (count>=n)
sum += number;
}
number++;
}
System.out.println(sum);
}
}
Saturday, November 21, 2015
Java Program: Points In Three-dimensional Space
As is known to all, points in three-dimensional space can be expressed via space rectangular coordinates.There is a program, Point.java, in which I define a class named Point and do some simple operations on it.
//Point.java
public class Point
{
int x, y, z; //3 coordinates of a point
Point(int _x, int _y, int _z) //Constructor
{
x = _x;
y = _y;
z = _z;
}
//To set the value of x, y and z coordinates
void setx(int _x)
{
x = _x;
}
void sety(int _y)
{
y = _y;
}
void setz(int _z)
{
z = _z;
}
//To calculate the square of distance between origin and the point
int SquareOfDistance()
{
return x * x + y * y + z * z;
}
//To test the program
public static void main(String[] args)
{
Point point = new Point(3, 4, 5);
System.out.println("The coordinate of this point is (" + point.x + "," + point.y + "," + point.z + "),and the square of distance between origin and the point is " + point.SquareOfDistance() + ".");
point.setx(1);
point.sety(2);
point.setz(3);
System.out.println("The coordinate of this new point is (" + point.x + "," + point.y + "," + point.z + "),and the square of distance between origin and the point is " + point.SquareOfDistance() + ".");
}
}
The running results are following:
The coordinate of this point is (3,4,5),and the square of distance between origin and the point is 50.
The coordinate of this new point is (1,2,3),and the square of distance between origin and the point is 14.
Have you had a try?
//Point.java
public class Point
{
int x, y, z; //3 coordinates of a point
Point(int _x, int _y, int _z) //Constructor
{
x = _x;
y = _y;
z = _z;
}
//To set the value of x, y and z coordinates
void setx(int _x)
{
x = _x;
}
void sety(int _y)
{
y = _y;
}
void setz(int _z)
{
z = _z;
}
//To calculate the square of distance between origin and the point
int SquareOfDistance()
{
return x * x + y * y + z * z;
}
//To test the program
public static void main(String[] args)
{
Point point = new Point(3, 4, 5);
System.out.println("The coordinate of this point is (" + point.x + "," + point.y + "," + point.z + "),and the square of distance between origin and the point is " + point.SquareOfDistance() + ".");
point.setx(1);
point.sety(2);
point.setz(3);
System.out.println("The coordinate of this new point is (" + point.x + "," + point.y + "," + point.z + "),and the square of distance between origin and the point is " + point.SquareOfDistance() + ".");
}
}
The running results are following:
The coordinate of this point is (3,4,5),and the square of distance between origin and the point is 50.
The coordinate of this new point is (1,2,3),and the square of distance between origin and the point is 14.
Have you had a try?
Wednesday, November 18, 2015
What's Wrong With C4droid?
As an Android phone user,meanwhile a programming learner,I have got to seek for compilers which can be setup on my phone.I used C4droid to compile and run programs before,with its inside compiler TCC,GCC and G++,mainly to practice C language.However,some terrible things happened.
About the problem related to "int a[10];",which I mentioned in my blogs before,I consulted several people,but there was no exact conclusion.But I found that when I change the compiler TCC to GCC or G++,that problem disappeared.As expected, it's associated with the compiler.As to exact reason,I don't know.
What's worth,I run a C++ program by G++ successfully a few days ago,but it couldn't be run today.It's a simple program about conversion of one's height.
//Conversion Of Your Height
#include <iostream>
int main(void)
{
using namespace std;
int inchheight,foot,inch;
const int ConversionFactor=12;
cout<<"Input your height in inches(only integer allowed):__\b\b";
cin>>inchheight;
foot = inchheight / ConversionFactor;
inch = inchheight % ConversionFactor;
cout<<"You are "<<foot<<" feet(foot) "<<inch <<" inch(es) high."<<endl;
return 0;
}
Here I just display the errors shown on the screen.
What's wrong with C4droid?
About the problem related to "int a[10];",which I mentioned in my blogs before,I consulted several people,but there was no exact conclusion.But I found that when I change the compiler TCC to GCC or G++,that problem disappeared.As expected, it's associated with the compiler.As to exact reason,I don't know.
What's worth,I run a C++ program by G++ successfully a few days ago,but it couldn't be run today.It's a simple program about conversion of one's height.
//Conversion Of Your Height
#include <iostream>
int main(void)
{
using namespace std;
int inchheight,foot,inch;
const int ConversionFactor=12;
cout<<"Input your height in inches(only integer allowed):__\b\b";
cin>>inchheight;
foot = inchheight / ConversionFactor;
inch = inchheight % ConversionFactor;
cout<<"You are "<<foot<<" feet(foot) "<<inch <<" inch(es) high."<<endl;
return 0;
}
Here I just display the errors shown on the screen.
What's wrong with C4droid?
Subscribe to:
Posts (Atom)
