The digits 0, 1, and 8 look much the same if rotated 180 degrees on the page (turned
upside down). Also, the digit 6 looks much like a 9, and vice versa, when rotated 180
degrees on the page. A multi-digit number may also look like itself when rotated on the
page; for example 9966 and 10801 do, but 999 and 1234 do not.
You are to write a program to count how many numbers from a given interval look like
themselves when rotated 180 degrees on the page. For example, in the interval [1..100]
there are six : 1, 8, 11, 69, 88, and 96.
Your program should take as input two integers, m and n, which define the interval to be
checked, 1 ≤ m ≤ n ≤ 32000. The output from your program is the number of rotatable
numbers in the interval.
You may assume that all input is valid.
Input/output is not from/to files for this question. Keyboard input and screen output is
expected.
Sample Session User input is in italics.
Enter the lower bound of the interval:
1
Enter the upper bound of the interval:
100
The number of rotatable numbers is:
6
I am kind of stuck withe logic... help me please.
Here's what i have done so far...
import java.util.Scanner;
public class j2 {
/**Gaurav Sharma
*Date: Feb 26th 2011
*Rotatable numbers.
*/
public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=orNum.charAt(length--);
length--;
System.out.print (revVar);
}return revVar;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//input
System.out.println("Enter the lower bound of the interval:");
int lowerbound = sc.nextInt();
System.out.println("Enter the upper bound of the interval:");
int upperbound = sc.nextInt ();
System.out.println ("The number of rotatable numbers is:");
int numofrotate = 0;
int rangevalues = 0;
String orNum;
for (int i=lowerbound; i<upperbound;i++){
orNum = i+"";
/*if (orNum.equals ("9")){
revVar = revVar+"6";
}
if (orNum.equals ("8")){
revVar= revVar+"8";
}
if (orNum.equals ("1")) {
revVar = revVar+ "1";
}
if (orNum.equals ("0")) {
revVar = revVar+"0";
}
if (orNum.equals ("6")){
revVar= revVar+"9";
}
else {
revVar = revVar+"$";
}
///System.out.println(revVar);
}
if (orNum.equals(revVar))
{
numofrotate++;
System.out.print(numofrotate);
}
*/}
}
}
15 answers
The input/output part looks fine.
I suggest the following hints. If you need further help, post.
1. The input number should be better stored as a string, because you would like to manipulate the number digit by digit.
2. You would need a table of conversion, for example, 6->9, 1->1, 8->8, 0->0, etc. This can be implemented as an array of 10 strings, which will contain the strings
0,1,x,x,x,5,9,x,8,6 rekspectively
where x represents the non-rotatable numbers.
So a[0]="0", a[6]="9", a[8]="8", a[2]="x", etc.
3. Make a result string (s1) and assign "" as its value, i.e. it contains nothing.
3. Read the input string (s) from the left, converting each digit into a "char" type variable using s.charAt(i) for the ith character. This is also a number. Character '0' has a value of 48, '1' has a value of 49, etc.
4. Convert this character into a string using the previous array (a): using
a[s.charAt(i)-48], assuming input values are all digits.
5. add this digit to the left of the new string (s1):
s1=a[s.charAt(i)-48]+s1;
6. Process all the characters of the string the same way until all characters have been converted.
7. Print the resulting string s1.
If you have difficulties, post your latest code as a follow-up to this post, not a new post. Describe where your difficulties are.
Here is the code:
public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=orNum.charAt(length--);
length--;
System.out.print (revVar);
}return revVar;
}
i want to see if this function works or not. i am printing out the string in the function but i wanna print out the string inside the main. any help would be appreciated. I just wanna see if i input 1234...i wanna print out the revesrsed num and that's why i am used a new function. please help!
1. you have put in length-- in two places. That skips characters. Also, the first occurrence needs to be decremented before, so it is --length.
2. Characters are not automatically (and correctly) upgraded to String. The use of the function Character.toString(char) is recommended.
revVar+=Character.toString(orNum.charAt(--length));
Other than that, you can put in a standard main program to test your code.
public static void main(String[] args){
System.out.println(reversing("65432"));
return;
}
Post if you have other questions.
Also, the algorithm for reversing could be simply read characters from left to right and append each character to the left of the new string.
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=Character.toString(orNum.charAt(--length));
length--;
System.out.print (revVar);
}return revVar;
}
public static void main(String[] args) {
System.out.println(reversing("65432"));
return;
i did the correction but i get 224246
The extra print statement (inside the for-loop) helps to mess up the results. By removing it, you will see "23456".
Also, I noted that even without the Character.toString() function, Java does the character to string promotion correctly. However, personally I would not mind to see this promotion done manually, because it demonstration an expected promotion.
Second step is the rotate the numbers that are rotatable so i need to make another function...
public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=Character.toString(orNum.charAt(--length));
}return revVar;
}
public static String rotnum(String orNum){
String revVar="";
if (orNum.equals ("9")){
revVar = revVar+"6";
}
if (orNum.equals ("8")){
revVar= revVar+"8";
}
if (orNum.equals ("1")) {
revVar = revVar+ "1";
}
if (orNum.equals ("0")) {
revVar = revVar+"0";
}
if (orNum.equals ("6")){
revVar= revVar+"9";
}
else {
revVar = revVar+"$";
}
System.out.println(revVar);
}return rotnum;
public static void main(String[] args) {
System.out.println(reversing("123456"));
return;
Scanner sc = new Scanner(System.in);
//input
System.out.println("Enter the lower bound of the interval:");
int lowerbound = sc.nextInt();
System.out.println("Enter the upper bound of the interval:");
int upperbound = sc.nextInt ();
System.out.println ("The number of rotatable numbers is:");
int numofrotate = 0;
int rangevalues = 0;
String orNum;
String revVar="";
for (int i=lowerbound; i<upperbound;i++){
orNum = i+"";
if (orNum.equals(revVar))
{
numofrotate++;
System.out.print(numofrotate);
}
}
}
}
i did create another function but i am getting syntax error at return?
public class j2 {
/**Gaurav Sharma
*Date: Feb 26th 2011
*Rotatable numbers.
*/
public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=Character.toString(orNum.charAt(--length));
}return revVar;
}
public static String rotnum(String orNum){
String revVar="";
if (orNum.equals ("9")){
revVar = revVar+"6";
}
if (orNum.equals ("8")){
revVar= revVar+"8";
}
if (orNum.equals ("1")) {
revVar = revVar+ "1";
}
if (orNum.equals ("0")) {
revVar = revVar+"0";
}
if (orNum.equals ("6")){
revVar= revVar+"9";
}
else {
revVar = revVar+"";
}return revVar;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//input
System.out.println("Enter the lower bound of the interval:");
int lowerbound = sc.nextInt();
System.out.println("Enter the upper bound of the interval:");
int upperbound = sc.nextInt ();
System.out.println ("The number of rotatable numbers is:");
int numofrotate = 0;
int rangevalues = 0;
String orNum;
String revVar="";
for (int i=lowerbound; i<upperbound;i++){
orNum = i+"";
if (orNum.equals(revVar))
{
numofrotate++;
System.out.print(numofrotate);
}
return;
}
}
}
this is my corrected code...my lowerbound was 1 and upper was 100... i don't get any rotatable number. I have created a function. what am i wrong?
There are unmatched braces in your code. I suppose they were introduced when you transferred it to the post.
One way to debug the program is to print each of the eligible cases. Don't just count. You can suppress the printing when everything works.
Also, try to write your code incrementally, not put all the code, and when it does not work, you don't know where to look. Always stay on firm ground, check one part, then write a few more lines, and check again. This way, if things don't work, you only have a few lines to check.
This line in main:
if (orNum.equals(revVar))
does NOT call your routines, so it does not work for you.
Also in your reversing() function, you have to reject numbers that contain 2,3,4,5,7. You can do this by something like:
char ch=orNum.charAt(--length);
if(ch=='2'||ch=='3'||ch=='4'||ch=='5'||ch=='7')return null;
revVar+=Character.toString(ch);
and check for a return String of null, which you can reject.
After that, you can work on the rotate function.
public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=Character.toString(orNum.charAt(--length));
char ch=orNum.charAt(--length);
if(ch=='2'||ch=='3'||ch=='4'||ch=='5'||ch=='7')return null;
revVar+=Character.toString(ch);
}return revVar;
}
public static String rotnum(String orNum){
String revVar="";
if (orNum.equals ("9")){
revVar = revVar+"6";
}
if (orNum.equals ("8")){
revVar= revVar+"8";
}
if (orNum.equals ("1")) {
revVar = revVar+ "1";
}
if (orNum.equals ("0")) {
revVar = revVar+"0";
}
if (orNum.equals ("6")){
revVar= revVar+"9";
}
else {
revVar = revVar+"";
}return revVar;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//input
System.out.println("Enter the lower bound of the interval:");
int lowerbound = sc.nextInt();
System.out.println("Enter the upper bound of the interval:");
int upperbound = sc.nextInt ();
System.out.println ("The number of rotatable numbers is:");
int numofrotate = 0;
String orNum="";
String revVar="";
for (int i=lowerbound; i<upperbound;i++){
orNum = i+"";
if (orNum.equals(revVar))
{
numofrotate++;
System.out.print(numofrotate);
}
return;
}
}
}
You can add them in and give it a try.
Meanwhile I will see if there are other things.
I have a few lines of code that you can take a look. You can see how it works, and add or change whatever you want. But whatever you do, test and check the results before coding other parts. If it does not work, you know what you have just changed, and it's easier to locate the problem.
import java.util.Scanner;
public class Anon3{
public static String reversing(String orNum){
String revVar = "";
for (int i = 0; i < orNum.length(); i++)
{
char ch=orNum.charAt(i);
// reverse 6 and 9; 0,1,8 do not change
if(ch=='6')revVar="9"+revVar;
else if(ch=='9')revVar="6"+revVar;
else if(ch=='0'||ch=='1'||ch=='8')revVar=Character.toString(ch)+revVar;
// last character put in front
else return null; // any other character is not reversible
} // end for
return revVar;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter number to rotate");
String strIn=sc.next();
String str=reversing(strIn); // reverse string
if(str!=null)System.out.println(str);
else System.out.println("The number "+strIn+" is not reversible");
return;
} // end main
} // end class
A series of streams run down the side of a mountain. The mountainside is very rocky so
the streams split and rejoin many times. At the foot of the mountain, several streams
emerge as rivers. Your job is to compute how much water flows in each river.
At any given elevation there are n streams, labelled 1 to n from left-to-right. As we
proceed down the mountainside, one of the streams may split into a left fork and a right
fork, increasing the total number of streams by 1, or two streams may rejoin, reducing the
total number of streams by 1. After a split or a rejoining occurs, the streams are
renumbered consecutively from left-to-right. There is always at least one stream and
there are never more than 100 streams.
The first line of input contains n, the initial number of streams at some high altitude. The
next n lines give the flow in each of the streams from left-to-right. Proceeding down the
mountainside, several split or rejoin locations are encountered. For each split location,
there will be three lines of input;
a line containing 99 (to indicate a split)
a line containing the number of the stream that is split
a line containing a number between 0 and 100, the percentage of flow from the split
stream that flows to the left fork. (The rest flows to the right fork).
For each join location, there will be two lines of input;
a line containing 88 (to indicate a join)
a line containing the number of the stream that is rejoined with the stream to its right
The flow from both joined streams is combined. After the last split or join location will
be:
a single line containing 77 (to indicate end of input)
Your job is to determine how many streams emerge at the foot of the mountain and what
the flow is in each. Your output is a sequence of real numbers, rounded to the nearest
integer, giving the flow in rivers 1 through n.
Sample Input (Input file : brooks.in)
3
10
20
30
99
1
50
88
3
88
2
77
Output for Sample Input (Output file : brooks.out)
5 55
can you help me with this one...i don't understand it.
Please make a new post if you still need help.