SuperK
BAN USERProgrammer!
- 0of 0 votes
Answersget a date(mon/day/year) from user. Print exact the week of dates(Sun to Sat)
- SuperK in United States
ex) input: 2/20/2001 if the day is Wednesday
output: Sunday 2/17/2001
Monday 2/18/2001
Tuesday 2/19/2001
Wednesday 2/20/2001
Thursday 2/21/2001
Friday 2/22/2001
Saturday 2/23/2002| Report Duplicate | Flag | PURGE
Epic Systems Software Engineer / Developer
<pre lang="" line="1" title="CodeMonkey20598" class="run-this">import java.io.*;
import java.util.*;
public class ReplaceWord {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
br.close();
String output = "";
StringTokenizer st = new StringTokenizer(input);
while (st.hasMoreTokens()) {
output += getConvertString(st.nextToken());
if (st.hasMoreTokens()) {
output += " ";
}
}
System.out.println(output);
}
public static String getConvertString(String input) {
String output = "";
int length = 0;
for (int i = 0; i < input.length(); ++i) {
char current = input.charAt(i);
if (current != ',' && current != '!' && current != '"' && current != '?' && current != '\'') {
++length;
}
}
if (length % 2 == 0 && length >= 4) {
int size = length / 2;
int setted = 0;
for (int i = 0; i < input.length(); ++i) {
char current = input.charAt(i);
if (setted < size) {
if (current != ',' && current != '!' && current != '"' && current != '?' && current != '\'') {
++setted;
}
}
output += current;
if (setted == size) {
output += " ";
++setted;
}
}
return output;
}
return input;
}
}
</pre><pre title="CodeMonkey20598" input="yes">
</pre>
<pre lang="" line="1" title="CodeMonkey14753" class="run-this">public class RobotMove {
public static int x = 0;
public static int y = 0;
public static int steps = 0;
public static int away = 0;
public static String direction = "";
public static void main(String[] args) {
x = 5;
y = 100;
getSteps(1);
System.out.println(away + " " + steps + " " + direction);
steps = 0;
if(direction.equals("front")) {
getStepsBack(1);
}
else {
getSteps(1);
}
System.out.println(away + " " + steps + " " + direction);
}
public static void getStepsBack(int n) {
if(y == 0) return;
if( y >= steps + x*n) {
steps += x*n;
away -= x*n;
} else {
steps += y-steps;
away -= y-steps;
direction = "back";
}
if(y > steps) {
if(y >= steps + x + n) {
steps += x+n;
away += x+n;
}
else {
steps += y-steps;
away += y-steps;
direction = "front";
}
}
if(y > steps){
getStepsBack(n+1);
}
}
public static void getSteps(int n) {
if(y == 0) return;
if( y >= steps + x*n) {
steps += x*n;
away += x*n;
} else {
steps += y-steps;
away += y-steps;
direction = "front";
}
if(y > steps) {
if(y >= steps + x + n) {
steps += x+n;
away -= x+n;
}
else {
steps += y-steps;
away -= y-steps;
direction = "back";
}
}
if(y > steps){
getSteps(n+1);
}
}
}
It seems that many people misunderstand the question..
It has 2 type of moving(start point facing front and facing back)
Steps
1.Move y steps with the rule.
2.turn 180 degree, then move y steps again.
If the Robot facing front after step 1, it will move to start point after step 2.
If the Robot facing back after step 1, it will move to twice more then step 1.</pre><pre title="CodeMonkey14753" input="yes">
</pre>
<pre lang="" line="1" title="CodeMonkey96874" class="run-this">public static void main(String[] args) {
setMatrix();
int minRow = 0;
int[] maxCol = new int[matrix.length];
int rowI = 0;
int rowJ = 0;
int[] colI = new int[matrix.length];
int[] colJ = new int[matrix.length];
for(int i = 0; i < matrix.length; ++i) {
for(int j = 0; j < matrix[i].length; ++j) {
if(j == 0) {
minRow = matrix[i][j];
rowI = i; rowJ = j;
} else {
if(minRow > matrix[i][j]) {
minRow = matrix[i][j];
rowI = i; rowJ = j;
}
}
if(i == 0) {
maxCol[j] = matrix[i][j];
colI[j] = i;
colJ[j] = j;
} else {
if(maxCol[j] < matrix[i][j]){
maxCol[j] = matrix[i][j];
colI[j] = i;
colJ[j] = j;
}
}
}
System.out.println(minRow + " [i] : " + rowI + " [j] : " + rowJ);
}
for(int i = 0; i < matrix.length; ++i) {
System.out.println(maxCol[i] + " " + colI[i] + " " + colJ[i]);
}
}
It works clearly. total complexity is O(n).</pre><pre title="CodeMonkey96874" input="yes">
</pre>
public static String numToTextChanger(String input) {
String output = "";
char preNumber = 0;
int prequency = 0;
if (input.length() == 0) {
return output;
} else {
for (int i = 0; i < input.length(); i++) {
char currentNum = input.charAt(i);
if (i == 0) {
preNumber = currentNum;
prequency = 0;
} else {
if (currentNum == preNumber) {
++prequency;
} else {
if (preNumber == '*') {
for (int j = 0; j < prequency + 1; ++j) {
output += " ";
}
} else if (preNumber == '#') {
output += "";
} else {
int index = Integer.parseInt(Character.toString(preNumber));
int length = numToText.elementAt(index).length;
output += numToText.elementAt(index)[prequency % length];
}
preNumber = currentNum;
prequency = 0;
}
}
if (i == input.length() - 1) {
if (currentNum == '*') {
for (int j = 0; j < prequency + 1; ++j) {
output += " ";
}
} else if (currentNum == '#') {
output += "";
} else {
int index = Integer.parseInt(Character.toString(currentNum));
int length = numToText.elementAt(index).length;
output += numToText.elementAt(index)[prequency % length];
}
}
}
}
return output;
}
Use hashmap then store element addresses, if key is exists then it is merge point.
- SuperK December 12, 2011