当前位置:编程学习 > JAVA >>

面试题

希望各位能不吝赐教
1.   1分2分5分的硬币,组成1角,共有多少种组合?
2、   请使用代码计算    1234567891011121314151617181920*2019181716151413121110987654321 
3.    如果用一个循环数组q[0..m-1]表示队列时,该队列只有一个队列头指针front,不设队列尾指针rear,求这个队列中从队列投到队列尾的元素个数(包含队列头、队列尾)
4.        设计相应的数据结构和算法,尽量高效的统计一片英文文章(总单词数目)里出现的所有英文单词,按照在文章中首次出现的顺序打印输出该单词和它的出现次数。

5.        有一棵树(树上结点为字符串或者整数),请写代码将树的结构和数据写到一个文件中,并能通过读取该文件恢复树结构 。
--------------------编程问答-------------------- 来顶顶 --------------------编程问答-------------------- 给自己顶顶   别沉了 --------------------编程问答-------------------- 第一个 

public class Test {
public static void main(String[] args) {
int a = 1; 
int b = 2;
int c = 5;
int count = 0;
for(int i = 0; i <= 10; i++) {
for(int j = 0; j <= 5; j++) {
for(int k = 0; k <= 2; k++ ){
if((i*a)+(j*b)+(k*c) == 10) {
count++;
System.out.println("i = " + i + ", j = " + j +", k = " + k);
}
  }
}
}
System.out.println("count = " + count);
}
}
--------------------编程问答--------------------


public class Test {
    public static void main(String[] args) {
        int a = 1; 
        int b = 2;
        int c = 5;
        int count = 0;
        for(int i = 0; i <= 10; i++) {
            for(int j = 0; j <= ((10-i)/2); j++) {
             int k = (10-i-2*j)/5 ;
                         if((i*a)+(j*b)+(k*c) == 10) {
                        count++;
                        System.out.println("i = " + i + ", j = " + j +", k = " + k);
                    
                 }
            }
        }
        System.out.println("count = " + count);
    }
}


就楼上的优化一下 --------------------编程问答-------------------- 第二个用BigInteger --------------------编程问答-------------------- 花了一个小时写了第二题的代码,开始主要对位数算错了


import java.math.BigInteger;

public class CodePlus {

public static void main(String[] args) {
int m = 0;
BigInteger total = new BigInteger("0");
for(int i=1; i<21; i++) {
int n = 0;
BigInteger powi;
BigInteger max = new BigInteger("0");
if(i<10) {
powi = new BigInteger("10").pow(31-i);
}
else {
powi = new BigInteger("10").pow(20-2*m++);
}
BigInteger bi = new BigInteger(String.valueOf(i));
for(int j=20; j>0; j--) {
BigInteger powj;

if(j>9) {
powj = new BigInteger("10").pow(31-2*n++);
}
else {
powj = new BigInteger("10").pow(j-1);
}
BigInteger bj = new BigInteger(String.valueOf(j));
max = powj.multiply(bj).multiply(powi).multiply(bi).add(max);
}
total = total.add(max);
}
System.out.println("1234567891011121314151617181920*2019181716151413121110987654321结果:");
System.out.println(total);
}
}
--------------------编程问答--------------------

String[] ss = "1234567891011121314151617181920*2019181716151413121110987654321".split("\\*") ;
BigInteger bigInteger = new BigInteger(ss[0]) ;
BigInteger bigInteger2 = new BigInteger(ss[1]) ;
System.out.println(bigInteger.multiply(bigInteger2).toString());

--------------------编程问答-------------------- package thirdp;

public class Coin {
//定义3个变量,作为1,2,5的对应系数
     int i=0;int a=1;
 int j=0;int b=2;
 int k=0;int c=5;
public void play()
{
//外层循环以k外变化,p可以取三个值:0,1,2
for(int k=0;k<=2;k++)
{
//第二层循环以j为变化,j可以取0,1,2,3,4,5
for(int j=0;j<=5;j++)
{
//第三层循环以i为变化,i可以去0,1,2,3,4,5,6,7,8,9
for(int i=0;i<=9;i++)
{
if(a*i+j*b+k*c==10)
{
System.out.println("一分"+i+"两分"+j+"五分"+k);
    }
}
}
}
}
}
我觉得把5分的循环放外边是不是会执行得快一点 --------------------编程问答--------------------
引用 5 楼  的回复:
第二个用BigInteger

看玩笑呢吧,分段处理吧!
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,