零基础学Java语言(浙江大学mooc)

Posted on 2021-10-09 22:38  金色的省略号  阅读(89)  评论(0编辑  收藏  举报

  mooc使用编程软件为eclipseeclipse下载地址Java jdk或jre下载编程题代码,仅供参考

  第一周编程题  

  温度转换

import java.util.Scanner;

public class Main 
{
    public static void main (String[] args) 
    {
        Scanner in = new Scanner(System.in);
        int F,C;
        F = in.nextInt();
        C = (int)((5.0/9)*(F - 32));
        System.out.println(C);
        in.close();    
    }
}
View Code

   第二周编程题

  时间换算

import java.util.Scanner;

public class Main 
{
    public static void main (String[] args) 
    {
        Scanner in = new Scanner(System.in);
        int BJT,UTF;
        BJT = in.nextInt();
        UTF = BJT-800;
        if(UTF<0)UTF += 2400;
        System.out.println(UTF);
        in.close();
    }
}
View Code

  信号报告

import java.util.Scanner;

public class Main 
{
    public static void main (String[] args) 
    {
        Scanner in = new Scanner(System.in);
         String []str1={
                 "",
                 "unreadable.",
                 "barely readable, occasional words distinguishable.",
                 "readable with considerable difficulty.",
                 "readable with practically no difficulty.",
                 "perfectly readable.",
        };
        String []str2={
                 "",
                 "Faint signals, barely perceptible,",
                 "Very weak signals,",
                 "Weak signals,",
                 "Fair signals,",
                 "Fairly good signals,",
                 "Good signals,",
                 "Moderately strong signals,",
                 "Strong signals,",
                 "Extremely strong signals,",
        };
        int n = in.nextInt();
        int i,j;
        i=n%10;
        j=n/10;
      System.out.println(str2[i]+" "+str1[j]);
        in.close();
    }
}
View Code

  第三周编程题

  奇偶个数

import java.util.Scanner;

public class Main 
{        
     public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
         int x,even =0,odd=0;
         do
         {
             x=in.nextInt();
             if(x%2==0) even++;
             else if(x!=-1)odd++;
         }while (x!=-1);
         System.out.println(odd+" "+even);
         in.close();

     }
}
View Code

  数字特征值

import java.util.Scanner;

public class Main 
{        
     public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
         int number=in.nextInt();
         int bit=1,sum=0,b=1;
         do
         {
              if(number%10%2==bit%2) 
              {
                  sum += b;    
              }
              number/=10;
              bit++;
              b*=2;    
         }while (number!=0);
         System.out.println(sum);
         in.close();

     }
}
View Code

  第四周编程题

  素数和

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x,n,m,sum=0;
        int cnt = 0;
        n = in.nextInt();
        m = in.nextInt();
        for (x=2 ;cnt <m; x++ ) 
        {    
            boolean isPrime = true; 
            for ( int i=2; i<x; i++ ) {
                if ( x % i == 0 ) 
                {
                    isPrime = false;
                    break;
                }
            }
            if ( isPrime ) 
            {
                cnt ++;
                if (cnt>=n)sum+=x;
            }    
        }
        System.out.println(sum);
        in.close();
    }
}
View Code

  念整数

import java.util.Scanner;

public class Main{
 
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String []str1 = {
            "ling","yi","er","san","si","wu","liu","qi","ba","jiu"
        };
        String str = in.next();
        for(int i=0;i<str.length();++i)
        {
            if(i!=0)System.out.print(" ");
            String sub = str.substring(i,i+1);
            if(sub.equals("-"))
            {
                System.out.print("fu");
            }
            for(int j=0;j<10;++j)
            {
                if(sub.equals(j+""))
                    {
                        System.out.print(str1[j]);
                        break;
                    }
                }
            }
            in.close();
        }
}
View Code

  第五周编程题

  多项式加法

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {    
        Scanner in = new Scanner(System.in);
        int x=0,y=0,count=0;
        int[] numb = new int [101];
        while (count<2){
            x = in.nextInt(); 
            y = in.nextInt();
            numb[x]+=y;
            if (x ==0)  count++;
        }
        
        boolean flag = true;
        for (int i=numb.length-1;i>=0;i--){
            if(numb[i]==0)continue;
            if(!flag&&numb[i]>0)  System.out.print("+");
            if(numb[i]==1&&i>0); 
            else System.out.print(numb[i]);    
            if(i>0)  System.out.print("x");     
            if(i>1)  System.out.print(i);      
            flag =false;  
        }
        if(flag) System.out.print(0);
    }
}
View Code

  第六周编程题

  单词长度 

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        
        while(true)
        {
            String s = in.next();
            if(s.lastIndexOf('.')>=0)
            {
                if(s.length()>1)System.out.print(s.length()-1);
                break;
            }
            else
            {
                System.out.print(s.length()+" ");
            }
        }

    }

}
View Code

  GPS数据处理

import java.util.Scanner;
public class Main {    
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        String s;  
        int hh=0,mm=0,ss=0; 
        do{
            s = in.nextLine();
           // if (s.indexOf("$GPRMC")!=-1&&s.indexOf(",A,")!=-1)
             if (s.contains("$GPRMC")&&s.contains(",A,"))
            {
                int i,sum = s.charAt(1);
                for ( i=2;s.charAt(i)!='*' ;i++)
                {
                    sum^=s.charAt(i);
                }

                if (sum%65536==Integer.parseInt(s.substring(i+1) ,16))
                {         
                    int j=0;
                    //while (!Character.isDigit(s.charAt(j)))j++;
                    j=s.indexOf(",");j++;
                    hh=Integer.parseInt(s.substring(j,j+2))+8;
                    mm=Integer.parseInt(s.substring(j+2,j+4));
                    ss=Integer.parseInt(s.substring(j+4,j+6));
                    if (hh>=24)hh-= 24; 
                }
            }
        }while (!s.equals("END")); 
        System.out.printf("%02d:%02d:%02d\n", hh,mm,ss);
        in.close();
    }
}
View Code

  第七周编程题

  分解质因数

import java.util.Scanner;
public class Main {   
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        System.out.print(number+"=");
        for(int i=2;i<=number/2;++i){        
            if(number%i==0){        
                number/=i;
                System.out.print(i+"x");
                --i;
            }                      
        }
        System.out.println(number);
        in.close();
    }
}
View Code

  完数

import java.util.Scanner;

public class Main {
    public static boolean isPNumber(int n)
    {
        int sum = 0;
        for(int i =1;i<n;i++)
        {
            if (n%i==0)
            {
                sum +=i;
            }
        }
        if (sum==n)
            return true;
        return false;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        
        int[] x = new int[50];
        int cnt = 0;
        for (int i=a;i<=b;i++)
        {
            if(isPNumber(i))
            {
                x[cnt++] = i;
            }
        }
        for(int i=0;i<cnt-1;i++)
        {
            System.out.print(x[i]+" ");
        }
        System.out.println(x[cnt-1]);

    }

}
View Code

  考试题

  二进制的前导的零

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int number = 0;
        int bit = 32;
        String result = null;

        number = in.nextInt();

        result = Integer.toBinaryString(number); 

        if (number != 0) 
        {
            System.out.println(bit - result.length());
        } 
        else 
        {
            System.out.println(bit);
        }
        in.close();
    }
}
View Code