当前位置: 代码迷 >> Java相关 >> 几道编程题不会,大家看看要怎么做?
  详细解决方案

几道编程题不会,大家看看要怎么做?

热度:814   发布时间:2006-04-27 03:57:00.0
几道编程题不会,大家看看要怎么做?

1。.求解两个输入正整数的最大公约数。
public class GreatestCommonDivisor
{
public int getnum(int a,int b)
{
.....
}
}



2。从命令行读入一个以逗号隔开数字序列,对其排序并按照降序排列输出
public class NumberInputSort{
public String sortNumberInput(String input)
{
,,,,,,
}
}


3。从命令行读入一个字符串和一个文件名,输出文件中字符串存在与否,如果存在那么输出出现的次数
public class FindString
{
public boolean isStringExist(String filePath,String stringToFind)
{
...
}
public int existCount(String filePath,String stringToFind)
{
......
}


4。给定一个字符串,求这个字符串中的从左边开始的第一个最长的子串,这个子串必须在该字符串的逆串中也被包含。
如:"XBCDEFYWFEDCBZ" 中包含"BCDEF"子串,而其逆串”ZBCDEFWYFEDCBX"中同样包含这个子串。
Returns :"BCDEF"
public class ReverseSubstring
{
public string findReversed(String input)
{
.....
}
}

谢谢!!!

[此贴子已经被作者于2006-4-27 4:15:04编辑过]

搜索更多相关的解决方案: class  编程  public  公约数  正整数  

----------------解决方案--------------------------------------------------------
这是我做的第一题,编译出错,缺少返回语句,大家看是哪里有错:
import java.io.*;
class GreatestCommonDivisor
{
public int getcd(int a,int b)
{
int i,j;
j=(a>b)?b:a;
for(i=j;i>0;i--)
if(a%i==0&&b%i==0)
return i;// 缺少返回语句。
}
public static void main(String args[])
{
try
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=new String();
String str1=new String();
System.out.println("Enter two numbers:");
str=br.readLine();
str1=br.readLine();
int m=Integer.parseInt(str);
int n=Integer.parseInt(str1);
GreatestCommonDivisor cd=new GreatestCommonDivisor();
int e=cd.getcd(m,n);
System.out.println("The Greatest Common Divisor is: "+e);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
----------------解决方案--------------------------------------------------------

import java.io.*;
public class Gongyueshu {
public int Great(int a,int b){
int c=1;
while(c!=0){

c=a%b;
a=b;
if(c!=0)
b=c;

}
return b;
}
public static void main(String[] args){
Gongyueshu gg=new Gongyueshu();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int x=0,y=0;
System.out.println("please input the x and y");
try{
x=Integer.parseInt(br.readLine());
y=Integer.parseInt(br.readLine());
} catch(IOException e){System.out.println(e.getMessage());}

int z=gg.Great(x,y);
System.out.print(z);

}
}


----------------解决方案--------------------------------------------------------

有必要这么麻烦么?


----------------解决方案--------------------------------------------------------

麻烦吗


----------------解决方案--------------------------------------------------------
用递归求最大公约数方法
int GCD(int a,int b){
if(a%b==0) //当a除以b的余数为0的时候,返回b
return b;
else
return GCD(b,a%b); //当除不尽的时候,再调用这个方法重复以前的动作
}

----------------解决方案--------------------------------------------------------

像这种基础的题目应该自己好好想想,而没有必要希望别人能帮你完成


----------------解决方案--------------------------------------------------------
可我就是完成不了啊,否则我也不拿到论坛上问了。

这几道题都是基础题目吗,我怎么觉得这么难哪?
----------------解决方案--------------------------------------------------------

这些都是很基础的呀?
我觉得楼主应该多学一点基础的问题!
我就来提示一下吧!
第二题:StringTokenizer getNum=new StringTokenizer(num," ,")//用到字符解析器,至于排序就用选择排序吧,很容易实现的!
第三题:也很简单,用到Properties类就可以轻松实现了!我顺便写了代码,楼主你看看吧!
import java.util.*;
import java.io.*;
public class PropertiesFile {

public static void main(String[] args) {

Properties setting=new Properties();
try
{
setting.load(new FileInputStream("count.txt"));
}
catch(Exception e)
{
setting.setProperty("count",String.valueOf(0));
}
int c=Integer.parseInt(setting.getProperty("count"))+1;//第一次运行时,count=0;
System.out.println("这是第"+c+"次运行");
setting.setProperty("count",String.valueOf(c));
try
{
setting.store(new FileOutputStream("count.txt"),"Program is used:");
}
catch(Exception e)
{
e.printStackTrace();
}

}
}


----------------解决方案--------------------------------------------------------

哈哈………………!!!
俺解决了一个,好高兴啊
虽然简单了点,可是很高兴,毕竟是新手么!!!
大家理解点吧
import java.io.*;
class GreatestCommonDivisor
{
public int getcd(int a,int b)
{
int i,j,m;
m=0;
j=(a>b)?b:a;
for(i=j;i>0;i--)

if(a%i==0&&b%i==0)
m=i;
return m;// 缺少返回语句。

}
public static void main(String args[])
{
try
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=new String();
String str1=new String();
System.out.println("Enter two numbers:");
str=br.readLine();
str1=br.readLine();
int m=Integer.parseInt(str);
int n=Integer.parseInt(str1);
GreatestCommonDivisor cd=new GreatestCommonDivisor();
int e=cd.getcd(m,n);
System.out.println("The Greatest Common Divisor is: "+e);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

[此贴子已经被作者于2006-4-28 16:57:04编辑过]


----------------解决方案--------------------------------------------------------
  相关解决方案