实现一个反转程序,要求:不能使用库函数。such as: 输入:123 输出:321
输入:asdf 输出:fdsa 欢迎大家试试。不难,考基础。 今天考的不好,看来还要多多练习编程。
输入:asdf 输出:fdsa 欢迎大家试试。不难,考基础。 今天考的不好,看来还要多多练习编程。
解决方案 »
- ibatis简单例子求助。
- JAVA的SOCKET连接问题
- 一个简单的程序
- Java中代理和委托有什么区别
- 如何在匿名类中获得 JCheckBox 的标签
- BufferedReader的readLine 什么时候返回null啊?
- 如何用javaa读取新浪首页?
- 简单问题,为什么会出错!!!在main()中
- 一个十分简单的问题,大家来抢分
- 字符串转换:+-*/()转为add,subtract,multiply,divide
- 跪求大侠请救,我的程序为什么不能查询,我绞尽脑汁而不得其解,我要疯了...
- Sun Java(TM) Studio Creator2 Update1 ,与Jboss 部署的问题______(分多,大家来帮顶啊,来者不拒)
{
StringBuffer sb = new StringBuffer();for(int i=s.length()-1; i>=0;i--)
{
sb.append(s.charAt(i));
}return sb.toString() ;
}
人家不让用,怎么办
return sb.reverse().toString();
int read = 0;
int total = 0;
while((read=System.in.read())!=-1 && read!='\n'){
if(total+1>container.length){
byte[] oldArray = container;
container = new byte[total*2];
for(int i=0;i<total;i++){
container[i] = oldArray[i];
}
}
container[total] = (byte)read;
total++;
}
for(int i=total;i>0;i--){
System.out.print((char)container[i-1]);
}
}
import java.lang.*;public class Jex1
{
public static void main(String[] args){
int rev=0,c,i=0,sign=0;
System.out.println("Please input a 3 bits int:");
try{
while ((c=System.in.read())>=48 && c<58 || c=='+' || c=='-')
{
if (c>=48 && c<58)
{
rev=rev+(c-48)*(int)Math.pow(10,i);
i++;
}
else if(c=='-')
sign=1;
}
if (sign==1)
rev=-rev;
System.out.println("The reverse of the int is" +rev);
}
catch(Exception e){
System.err.println("Input error!");
}
}
}
String tempStr = "";
if (str != null) {
for (int i = 0; i < str.length(); i++) {
tempStr = str.charAt(i) + tempStr;
}
}
return tempStr;
}}=================================================================
import java.io.*;public class ReverseStringDemo { public ReverseStringDemo() {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader cin = new BufferedReader(reader);
try {
StringBuffer str = new StringBuffer(cin.readLine());
System.out.println(str.reverse().toString());
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
new ReverseStringDemo();
}}
import java.awt.event.*;class Transfer
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please input:");
String inputString = in.readLine();
char[] inputChar = inputString.toCharArray();
System.out.print("The result:----->");
for(int i = inputChar.length-1;i >= 0;i--)
System.out.print(inputChar[i]);
System.out.println();
}
}
呵呵,开玩笑
String s = "123";
for(int i = (s.length()-1);i>=0;i--){
System.out.println(s.charAt(i));
}
}
}
-=l---------------------------------
垃圾公司,有函数不让用。
out.println("321");
}
嘻嘻^_^
return sb.reverse().toString();这样就可以了阿
如果这个不让用酒可以用流,放入数据然后从后面往前读
System.in
让不让用??
out.println("321");
}
~!~~`就是这个答案了,,公司考的是智力题,,.真是绝了...
StringBuffer str = new StringBuffer("abcdefgh");
int len = str.length(), left, right;
char c;
for(left = 0; left < len/2; left++) {
right = len - left - 1;
c = str.charAt(left);
str.setCharAt(left, str.charAt(right));
str.setCharAt(right, c);
}
System.out.println(str);
所以用char[], 很简单...
同意,不如换成考让自己建个队列或栈的数据结构并完成操作实现
return sb.reverse().toString();
不过,就是不知道效率如何
out.println("321");
}
~!~~`就是这个答案了,,公司考的是智力题,,.真是绝了...
================
强啊,哈哈
out.println("321");
}
if(String s=="asdf"){
out.println("fdsa");
}
public class DaoZhuang
{
public static void main(String[] args) throws java.io.IOException
{
String s1;
int i, numcount;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("Enter a text: ");
s1 = br.readLine();
numcount = s1.length();
for (i = numcount-1; i >=0; i--)
System.out.print(s1.charAt(i));
}
}
而且读入的时候把回车读进去了,所以会输出多一个回车
说得对,脑筋急转弯,呵呵。。开玩笑。。
for(int i=s.length()-1;i>=0;i--)
System.out.print(s.charAt(i));
}
*可用除法,321-((321/10)*10)可得1
*依次类推
*/
用除法怎么倒序字母??
望指点
private String input; private String output; public StringReverserThroughStack(String in) {
input = in;
} public String doRev() {
int stackSize = input.length();
Stack theStack = new Stack(stackSize); for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
theStack.push(ch);
}
output = "";
while (!theStack.isEmpty()) {
char ch = theStack.pop();
output = output + ch;
}
return output;
} public static void main(String[] args) throws IOException {
String input = "Java Source and Support";
String output;
StringReverserThroughStack theReverser = new StringReverserThroughStack(input);
output = theReverser.doRev();
System.out.println("Reversed: " + output);
}
class Stack {
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}
out.println("321");
}
~!~~`就是这个答案了,,公司考的是智力题,,.真是绝了...
================
强啊,哈哈
================
================同哈哈
public class ReverseDemo { public static void main(String[] args) {
try {
System.out.print("Type any number of characters and press Enter: ");
String s = "";
int i = 0;
while( i != 13){
i = System.in.read();
s = (char)i + s;
}
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
用
命令行参数+getchar()+for()
class strrev{
public static void main(String args[]){
char[] s={'a','b','c','d','e'};
int i=0;
char a;
while((i+1)<=s.length/2){
a=s[i];
s[i]=s[s.length-1-i];
s[s.length-1-i]=a;
i++;
}
System.out.print(s);
}
}
String str = "12345";
String rts = new StringBuffer(str).reverse().toString(); // 54321
for(int i=str.length()-1;i>=0;i--)
System.out.print(str.charAt(i));
}
=================================================
public void getStr(String str){
StringBuffer sb=new StringBuffer(str);
System.out.print( sb.reverse() );
}
String Reverse(String str) {
byte[] bt = str.getBytes();
byte temp;
for (int i = 0; i < bt.length / 2; i++) {
temp = bt[i];
bt[i] = bt[bt.length - i - 1];
bt[bt.length - i - 1] = temp;
}
return new String(bt);
}
是不是也不能用 JAVA,C++,C ,VB,PB...
用栈来实现,公司应该考的就是这个了
import java.io.IOException;
import java.io.InputStreamReader;
public class Convert{
public static void main(String args[]){
String str=null;
System.out.println("please input a string:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println("the string is:"+ str);
char[] temp = str.toCharArray();
char[] temp1 = new char[temp.length];
for(int i=temp.length-1, j=0;i>=0;i--,j++){
temp1[j] = temp[i];
}
str=temp1.toString();
System.out.println("the converted string is:"+ str);
}
}
out.println("321");
}
~!~~`就是这个答案了,,公司考的是智力题,,.真是绝了...
================
强啊,哈哈
================
================同哈哈
public static void main(String[] args) throws IOException { byte[] container = new byte[8189];
int read = 0;
int total = 0;
while ((read = System.in.read()) != -1 && read != '\n') {
if (total + 1 > container.length) {
byte[] oldArray = container;
container = new byte[total * 2];
for (int i = 0; i < total; i++) {
container[i] = oldArray[i];
}
}
System.out.print (read);
container[total] = (byte) read;
total++;
}
for (int i = total; i > 0; i--) {
System.out.print((char) container[i - 1]);
}
}
}翅膀的代码的确有用..不过我有些不明白的地方请大家指教一下.
就是这段
if (total + 1 > container.length) {
byte[] oldArray = container;
container = new byte[total * 2];
for (int i = 0; i < total; i++) {
container[i] = oldArray[i];
}
}
我是初学者请大家多多指教.
import java.io.IOException;
import java.io.InputStreamReader;
public class Convert{
public static void main(String args[]){
String str=null;
System.out.println("please input a string:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println("the string is:"+ str);
Convert c = new Convert();
String s = c.reverse(str);
System.out.println("the converted string is:"+ s);
}
public String reverse(String str){
char[] bt=str.toCharArray();
char temp;
for(int i=0;i<bt.length/2;i++){
temp = bt[i];
bt[i] = bt[bt.length - i - 1];
bt[bt.length - i - 1] = temp;
}
return new String(bt);
}
}
{
public static void main(String[] args)
{
String str;
if (args.length<0)
{
System.out.println("请输入参数!");
}
str = args[0];
char[] ch = str.toCharArray();
char[] b;
b = new char[ch.length];
for (int i=0; i<ch.length;i++)
{
b[i] = ch[ch.length-1-i];
}
for (int i=0; i<b.length; i++)
{
System.out.print(b[i]);
}
/*就地逆置
char temp;
for (int i=0; i<ch.length/2;i++)
{
temp = ch[i];
ch[i]=ch[ch.length-1-i];
ch[ch.length-1-i] = temp;
}
for (int i=0; i<ch.length; i++)
{
System.out.print(ch[i]);
}*/ /*利用栈来实现
Stack st = new Stack();
for (int i=0; i<ch.length; i++)
{
st.push(ch[i]);
}
for (int i=0; i<ch.length; i++)
{
System.out.println(st.pop());
}*/
}
}---------------------------------------------------------------------
以上三种方法都可以。他说不让用库函数是指不让用系统的Reverse函数而已。