50道JAVA基礎プログラミング

27604 ワード

                               50   JAVA          
【   1】
  :    :     ,      3             ,      
             ,       ,            ?
.    :          1,1,2,3,5,8,13,21.... 
  :
(   )
         :                    ,      :
X1,X2,X3 Y1,Y2,Y3 Z1,Z2,Z3
X1:         ;X2         ;X3             。
        :
Y1=X2+X3 ,Y2=X1 ,Y3=X2+X3
Z1=Y2+Y3 ,Z2=Y1 ,Z3=Y2+Y3
Z1+Z2+Z3= Y2+Y3+Y1+(Y2+Y3)=(Y2+Y3+Y1)+(X2+X3+X1)
                     。        ,    。
(   )
         ,     (                ),      :

import java.util.*;
import java.io.*;
class TuZi
{
int nianling=1;
}
public class Text1 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int shuliang=1;
int zhouqi=4;
int yuefen=0;
Vector rongqi=new Vector();
System.out.print("        :");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
shuliang=(new Integer(stdin.readLine()));
System.out.print("        :");
zhouqi=(new Integer(stdin.readLine()));
System.out.print("      :");
yuefen=(new Integer(stdin.readLine()));
for(int i=1;i<=shuliang;i++)
{
rongqi.addElement(new TuZi());
}
for(int i=2;i<=yuefen;i++)
{
for(int j=0;j=zhouqi)
{
rongqi.addElement(new TuZi());
}
}
}
System.out.print("     :"+rongqi.size());
}
}
【   2】
  :   101-200         ,       。
1.    :       :         2   sqrt(   ),      ,
         ,     。
  :
    :
public class Text2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
System.out.print("101 200    : ");
for(int i=101;i<=200;i++)
{
int flag=0;
for(int j=2;j<=Math.sqrt(i);j++)
{
float k=(float)i;
if(k%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.print(i+" ");
sum++;
}
}
System.out.println("");
System.out.print("     : ");
System.out.print(sum);
}
}
【   3】
  :      "    ",  "    "       ,          
    。  :153    "    ",   153=1     +5     +3     。
1.    :   for      100-999   ,        ,  ,  。
  :
    :
public class Text3 {
public static void main(String[] args)
{
int a=0;int b=0;int c=0;
System.out.println("100 999      :");
for(int i=100;i<=999;i++)
{
a=i/100;
b=i/10-a*10;
c=i-b*10-a*100;
if(i==a*a*a+b*b*b+c*c*c)
{
System.out.println(i);
}
}
}
}
【   4】
  :           。  :   90,    90=2*3*3*5。
  :
  :  n        ,            k,         :
(1)          n,               ,     。
(2)   n>k,  n    k   ,      k   ,   n    k   ,        
n,       。
(3)   n     k   ,   k+1    k   ,       。
                  ,  “*”    ,       ,   
 :
import java.io.*;
public class Text4 {
public static void chuLi(int n)
{
for(int i=2;i<=n;i++)
{
if(n==i)
{ 
System.out.print(i);
return;
}
if(n>i&&(n%i==0))
{
n=n/i;
System.out.print(i+"*");
chuLi(n);
break;//     
}
}
}
public static void main(String[] args) throws IOException
{int shu=0;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("      :");
shu=(new Integer(stdin.readLine()));
chuLi(shu);
}
}
【   5】
  :               :    >=90       A   ,60-89   
    B   ,60       C   。
  :
      ,    
import java.io.*;
public class Text5 {
public static void main(String[] agrs) throws IOException
{
System.out.print("     :");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
int chengji=new Integer(stdin.readLine());
char dengji=chengji<60?'C':(chengji>=90?'A':'B');
System.out.print(dengji);
} 
}
【   6】
  :        m   n,             。

  (1)  :
E0.[   m n]   m n,  m n。
E1.[   ]   n   m    r      。(     0 r n。)
E2.[    0?]   r   0,    ,n       。
E3.[  ]   m n,n r,      E1。
    :
(1)   m%n=0,  n         
(2)   m%n 0,           
      n   m%n       ,       m   n       。(   
       )
  :
   a   n   m%n       ,  :
  (m%n)%a=0,    m%n=k*a,k     。  n%a=0,    n=p*a,p    
  。
       :m=t*n+m%n,   t      0    。
   :m=t*p*a+k*a=(t*p+k)*a,     m%a=0
          :
a   m   n         。
     a   m   n       ?(       )
  :   m   n       b,   b>a。
  :
             :
  b    n   m%n     ,  b>a,  a   n   m%n         。
  ,      n   m%n       ,       m   n       。
(3)   。
    E0    :
  m
    :
F1:   m   n     ,      m,     n。
F2:   m     n   ,    m         。
F2:  n     。
F3:  m      n    ,        n   ,             。
       ,    :
import java.io.*;
import java.util.*;
public class Text6 {
public static void fenJie(Vector m,int n)
{
for(int i=2;i<=n;i++)
{
if(n==i)
{ 
m.addElement(i);
return;
}
if(n>i&&(n%i==0))
{
n=n/i;
m.addElement(i);
fenJie(m,n);
break;
}
}
}
public static int gongBeiShu(Vector m,int a,int b)
{
int chengji=1;
if(a%b==0)
return a;for(int i=0;i pool=new Vector();
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("         :");
shu1=(new Integer(stdin.readLine()));
System.out.print("         :");
shu2=(new Integer(stdin.readLine()));
if(shu1=65&&bytes[i]<=90)||(bytes[i]>=97&&bytes[i]<=122))
zimu++;
else if(bytes[i]==32)
kongge++;
else if(bytes[i]>=48&&bytes[i]<=57)
shuzi++;
else if(bytes[i]<0)
hanzishu++;
else
qita++;
}
System.out.println("          :"+bytes.length);
System.out.println("     :"+hanzishu/2);
System.out.println("       :"+zimu);
System.out.println("     :"+kongge);
System.out.println("     :"+shuzi);
System.out.println("       :"+qita);
}
}
【   8】
    :   s=a+aa+aaa+aaaa+aa...a     ,     a           。    
2+22+222+2222+22222(     5     ),          。
  :
    ,          “+”   ,          ,       ,
      :
import java.io.*;
public class Text8 {
public static void main(String[] args) throws IOException
{
int s=0;int a=0;int sum=0;String p="";
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("     a:");
a=new Integer(stdin.readLine());
System.out.print("       s:");
s=new Integer(stdin.readLine());
for(int i=1;i<=s;i++)
{
Character c=(char)(a+'0');
p=c.toString();
for(int j=1;j m,int n)
{
for(int i=1;i<=n/2;i++)
if(n%i==0) 
m.addElement(i);
}
public static boolean panBie(Vector m,int n)
{
int sum=0;
for(int i=0;i pool=new Vector();
fenJie(pool,i);
zhenjia=panBie(pool,i);
if(zhenjia)
System.out.println(i);
}
}
}
【   10】
  :    100        ,              ;   ,     
10     ,      ?  10      ?
  :
      ,      :
import java.io.*;public class Text10 {
public static void main(String[] args) throws IOException {
float heigh=100;int cishu=10;float sum=0;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("        :");
heigh=(new Float(stdin.readLine()));
sum=heigh;
System.out.print("        :");
cishu=(new Integer(stdin.readLine()));
for(int i=1;i m,int n)
{
for(int i=1;i<=Math.sqrt(n);i++)
if(n%i==0) 
m.addElement(i);
}
public static void main(String[] args) throws IOException
{
Vector pool=new Vector();
int m=100;int n=168;int t=0;int flag=0;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("        :");
m=new Integer(stdin.readLine());
System.out.print("        :");
n=new Integer(stdin.readLine());
if(m>n)
{
t=m;
m=n;
n=t;
}
fenJie(pool,n-m);
int a=0;int b=0;int N=0;int x=0;
for(int i=0;i0)
{
flag=1;System.out.println(x);
}
}
if(flag==0)
System.out.println("        !");
}
}
【   14】
  :        ,             ?
  :
     ,           。
1、      4        100       。(  2004      ,1900    
  )
2、      400       。(  2000     ,1900      )
3、         ,        3200,      172800     。 
172800     ,86400      (        3200,      172800)
  :    Scanner    nextInt()       ,               ,
       。        ,    Character.isDigit         ,   
       ,      。(                      , 
211   8   8           :211、8、8)
import java.io.*;
import java.util.*;
public class Text14xin {
public static void main(String[] args) throws IOException
{
long year=0;long month=0;long day=0;
String m=new String("");
String n=new String("");
Vector s=new Vector();
long tianshu=0;
int[] monthday={31,28,31,30,31,30,31,31,30,31,30,31};
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("    :");
m=stdin.readLine();
char[] mass = m.toCharArray();
for(int i=0;i=172800)
{
if(year%3200==0&&year%172800==0)
monthday[1]=29;
}
else
{
if(year%4==0&&year%100!=0)
monthday[1]=29;
if(year%400==0)
monthday[1]=29;
}
for(int j=0;j=1;i--)
{
a=(a+1)*2;
}
System.out.print(a);
}
}
【   18】
  :          ,    。    a,b,c   ,    x,y,z   。   
      。            。a      x  ,c      x,z  ,   
          。
  :
  :
              ,a     x,y,z     ,b            ,
  c           ,    6    。              11,   
   :
public class Text18 {
public static void main(String[] args) {
Character[] b={'x','y','z'};
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
if(j==k)
continue;
for(int h=0;h<3;h++)
{
if(h==j||h==k)continue;
if(b[j]!='x'&&b[h]!='x'&&b[h]!='z')
{
String s1=b[j].toString();
String s2=b[k].toString();
String s3=b[h].toString();
System.out.println(" a"+"(vs)"+s1+" b"+"(vs)"+s2+" 
c"+"(vs)"+s3);
}
}
}
}
}
}
//output:
 a(vs)z b(vs)x c(vs)y
【   19】
  :       (  )
* 
*** 
****** 
******** 
****** 
*** 
* 
  :
1.            ,       ,       ,     for   , 
     ,      。      ,       。
2           ,           ,         。
  :
1、          
2、         n,  (n+1)/2      ,           ,   
           ,      :
import java.io.*;
public class Text19 {
public static void main(String[] args) throws Exception{
int n=0;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("       :");
n=(new Integer(stdin.readLine()));
if(n%2!=0)
{
for(int i=1;i<=(n+1)/2;i++)
{
for(int j=1;j<=(n-1)/2+i;j++)
{
if(j<=(n+1)/2-i)
{
System.out.print(" ");
}
else
System.out.print("*");
}System.out.println("");
}
for(int i=(n+3)/2;i<=n;i++)
{
for(int j=1;j<=(3*n+1)/2-i;j++)
{
if(j<=i-(n+1)/2)
{
System.out.print(" ");
}
else
System.out.print("*");
}
System.out.println("");
}
}
else
System.out.print("        !");
}
}
//output:
       :7
 *
 ***
*****
*******
*****
 ***
 *
【  20】
  :      :2/1,3/2,5/3,8/5,13/8,21/13...        20   。
  :
  :             。             ,         
  ,      :
public class Text20 {
public static void main(String[] args) {
float fenmu=1;float fenzi=2;
float sum=fenzi/fenmu;
float t=0;
for(int i=1;i<20;i++)
{
t=fenmu+fenzi;
fenmu=fenzi;
fenzi=t;
sum=sum+fenzi/fenmu;
}
System.out.print(sum);
}
}
//output:
32.660263
【  21】
  : 1+2!+3!+...+20!  
  :  :      ,         double,       double     , 
  Infinity。        int      double     ,      
    ,  ,        ,     ,     ,       ,   
          。
public class Text21 {
public static void main(String[] args)
{
double sum=0;double jiecheng=1;
for(int i=1;i<=20;i++)
{
jiecheng=i*jiecheng;
sum=sum+jiecheng;
}
System.out.println(sum);
System.out.print(Double.MAX_VALUE);
}
}
//output:
2.5613274941118203E18
1.7976931348623157E308
【  22】
  :       5!。
  :
      ,     ,       200!。
  :
200!  Double     ,            200!    ,      
  。       ,         ,      n       ,    
 (n-n%10)/10,    n%10。            292!(         
     ,      ,       ,        ),     21  
        170!。
      :
import java.util.*;
class ShuJu
{
double h;//   double           ,   text21        
ShuJu(double i)
{
h=i;
}
}
public class Text22 {
public static void jiecheng(Vector m,int a)
{
ShuJu n=new ShuJu(1);
m.addElement(n);
for(int i=1;i<=a;i++)
{
int g=m.size();
for(int j=0;j=10)
{
m.elementAt(j).h=k%10;
if(j+1>=g)
m.addElement(new ShuJu((k-m.elementAt(j).h)/10));
//       ,     double ,              。
else
m.elementAt(j+1).h=m.elementAt(j+1).h+(k-m.elementAt(j).h)/10;
}
else
m.elementAt(j).h=k;
}
}
int flag=m.size()-1;
while(m.elementAt(flag).h>=10)//  while              ,   
 。
//                        
{
double ch=m.elementAt(flag).h;
m.elementAt(flag).h=ch%10;
m.addElement(new ShuJu(((ch-m.elementAt(flag).h)/10)));
flag=flag+1;
}
}
public static void main(String[] args)
{
Vector pool=new Vector();
int x=200;int zeros=0;int flag=0;
jiecheng(pool,x);
for(int i=0;pool.elementAt(i).h==0;i++)
{
zeros++;//             
}
System.out.print((int)pool.elementAt(pool.size()-1).h+".");
//       int  ,            double  ,        
  
for(int i=pool.size()-2;i>=zeros;i--)
{
System.out.print((int)pool.elementAt(i).h);
flag++;
if(flag>=10)//           
break;
}
System.out.print("*E"+(pool.size()-1));//      
}
}
//output:
7.8865786736*E374
【  23】
  : 5      ,        ?    4   2 。  4    ,    3   2 。     ,    2    。  2  ,         。
       ,   10 。        ?
  :
      ,    。
【  24】
  :      5     ,  : 、      , 、         。
  :
1、           s,    new Integer(s)        ,       ,
        ,        。
2、           s,  s.getBytes()      Byte  ,        ,
      。      :
//              
import java.io.*;
public class Text24 {
public static void main(String[] args) throws Exception{
String s=new String("");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("        :");
s=stdin.readLine();
byte[]bytes=s.getBytes();
System.out.println("  "+bytes.length+"  ");
for(int i=bytes.length-1;i>=0;i--)
{
System.out.print(bytes[i]-'0');
}
}
}
//output:
        :123456
  6  
654321
【  25】
  :  5  ,         。 12321    ,       ,    
   。
  :
     24          ,      :
//       5  
import java.io.*;
public class Text25 {
public static void main(String[] args) throws Exception{
String s=new String("");
int flag=0;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("        :");
s=stdin.readLine();
byte[]bytes=s.getBytes();
System.out.println("  "+bytes.length+"  ");
for(int i=0;i=2)
d=(char)bytes[1];
switch(c)
{
case 'M':
System.out.print("   ");
break;
case 'm':
System.out.print("   ");
break;
case 'T':
if(d=='u'||d=='U')
System.out.print("   ");
if(d=='h'||d=='H')
System.out.print("   ");
break;
case 't':
if(d=='u'||d=='U')
System.out.print("   ");
if(d=='h'||d=='H')
System.out.print("   ");
break;
case 'W':
System.out.print("   ");
break;
case 'w':
System.out.print("   ");break;
case 'F':
System.out.print("   ");
break;
case 'f':
System.out.print("   ");
break;
case 'S':
if(d=='a'||d=='A')
System.out.print("   ");
if(d=='u'||d=='U')
System.out.print("   ");
break;
case 's':
if(d=='a'||d=='A')
System.out.print("   ");
if(d=='u'||d=='U')
System.out.print("   ");
break;
}
}
}
【  27】
  : 100     
  :   2  
public class Text27 {
public static void main(String[] args) {
int sum=0;
System.out.print("100      : ");
System.out.print(2+" ");
for(int i=3;i<100;i++)
{
int flag=0;
for(int j=2;j<=Math.sqrt(i);j++)
{
float k=(float)i;
if(k%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.print(i+" ");
sum++;
}
}
System.out.println("");
System.out.print("     : ");
System.out.print(sum+1);
}}
//output
100      : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 
71 73 79 83 89 97 
     : 25
【  28】
  : 10      
1、  Array.sort()      ,     15,        ,    。
2、               。
    
public class MaoPao {
public static void main(String[] args) {
int[] a={3,6,4,2,9,5,8,1,7,0};
int temp=0;int flag=0;
for(int i=0;i p=new ArrayList();for(int i=0;scan.hasNext();i++)
{
p.add(scan.nextLong());
}
Collections.sort(p); //   JDK       
System.out.print("       :");
for(int i=0;i=p.get(i)) //      ,           
continue;
p.add(i,m);
break;
}
System.out.print("       :");
for(int i=0;i>>1;}
System.out.println("");
}
public static void main(String[] args) throws Exception {
int x=0;
String s=new String("");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("      :");
s=stdin.readLine();
x=new Integer(s);
toBinary(x);
}
}
//output
      :12
1100
      :2047
11111111111
【  32】
  :     a      5~8 。
  :
(1)  a  4 。
(2)     4   1,    0  。  ~(~0<<4) 
(3)       &  。
import java.io.*;
public class Text32 {
public static void main(String[] args) throws Exception{
int n=5;int m=8;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("        :");
int a=new Integer(stdin.readLine());
int b=~(~0<>>(m-n+1);
int d=c&b;
System.out.print("          :");
System.out.println(Integer.toBinaryString(a));
System.out.print("          5~8  :");
int k=Integer.toBinaryString(d).length();
if(k vector1=new Vector();;
for(int i=1;i<=n;i++)
{
Vector vector=new Vector();
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");//       
}
vector.addElement(1);
if(i>=3)
{
for(int k=2;k<=i-1;k++)
{
int m=vector1.elementAt(k-2)+vector1.elementAt(k-1);
vector.addElement(m);
}
}
if(i>=2)
vector.addElement(1);
vector1=vector;
for(int h=0;h p=new ArrayList();
for(int i=0;scan.hasNext();i++)
{
p.add(scan.nextLong());
}
int flag=0;
int flag1=p.size()-1;
long temp=0;
for(int i=1;i=0;i--)
{
if(p.get(flag1)>p.get(i))
flag1=i;
}
if(flag1!=p.size()-1)
{
temp=p.get(p.size()-1);
p.set(p.size()-1,p.get(flag1));
p.set(flag1,temp);
}
System.out.print("       :");
for(int i=0;i p=new ArrayList();
for(int i=0;scan.hasNext();i++)
{
p.add(scan.nextLong());
}
System.out.print("            :");
long m=new Long(stdin.readLine());
if(m<=p.size())
{
int n=p.size();
for(int i=1;i<=m;i++)
p.add(0,p.get(n-1));//    ,      ,           
System.out.print("       :");
for(int i=0;i p=new ArrayList();int flag=1;
int flag1=0;
for(int i=0;ig)
k=g; //               
for(int i=0;it)
return 1; //     1
if(ug)
return 1;
return -1;
}
public static void main(String[] args) throws Exception{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("         :");
String n=stdin.readLine();
System.out.print("         :");
String m=stdin.readLine();
int daxiao=compare(n,m);
if(daxiao==0)
System.out.print(n+"="+m);
if(daxiao==1)
System.out.print(n+">"+m);
if(daxiao==-1)
System.out.print(n+"abcd【  41】
  :        ,      。                ,    ,
             ,     。                  , 
    ,            ,     ,  、  、          
 ,              ?
  :
            ,          m   ,           
         m*5/4+1   ,         。   5      n   ,
  m=5*n+1     。                  ,        
    。(      ,    m   4  )。
public class Text41 {
public static void main(String[] args) {
int n=1;
int m=0;
int flag=1;
int monkeyNum=5;
while(true)
{
flag=1;
m=monkeyNum*n+1;
for(int i=monkeyNum;i>=1;i--)
{
if(m%(monkeyNum-1)==0)
{
m=m/(monkeyNum-1)*monkeyNum+1;
flag++; //           4  
}
else
break;
}
if(flag==monkeyNum) //          4  
break;
n++;
}
System.out.println("         :"+m);
System.out.print("              :"+n);
}
}
//output
         :3121
              :255
【  42】
  :809*??=800*??+9*??+1   ??      ,8*??       ,9*??    
3  。 ??      , 809*??    。
  :       ,    。
【  43】
  : 0—7         。
          。
【  44】
  :               。
  ,               。
【  45】  :          9  
          。
【  46】
  :         
  :
1、    “+”     。
2、 public String concat(String str)   ,"cares".concat("s") returns "caress" ,
"to".concat("get").concat("her") returns "together"。
【  47】
  :  7  (1—50)    ,      ,          *。
  :
      ,      。(     29     )
【  48】
  :              ,        ,          ,  
    :       5,      10        ,          
 ,         。
  :
      :
import java.io.*;
public class Text48 {
public static void main(String[] args) throws Exception {
System.out.print("             :");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s=stdin.readLine();
char[] a=s.toCharArray();
int[] b=new int[a.length];
for(int i=0;i=0;i--)
{
System.out.print(b[i]);
}
}
}
//output
             :1234
       :9876
             :4567
       :2109
【  49】
  :             
  :
import java.io.*;
public class Text49 {
public static void main(String[] args) throws Exception {
System.out.print("      :");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s=stdin.readLine();
System.out.print("       :");String s1=stdin.readLine();
char[] a=s.toCharArray();
char[] b=s1.toCharArray();
int num=0;
int flag=0;
for(int i=0;i vector=new Vector();
byte[] a=s.getBytes();
char[] mass = s.toCharArray();
for(int j=0;j