「題目程式碼」P1007~P1012(Java)

SamHX發表於2018-05-11

 

1007 C基礎-計負均正

import java.util.*;
import java.io.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int x;
        int negativeCnt=0,positiveSum=0,zeroCnt=0;
        for(x=1;x<=20;++x)
        {
            int n=sc.nextInt();
            if(n<0) negativeCnt++;
            else if(n==0) zeroCnt++;
            else positiveSum+=n; 
        }
        System.out.printf("%d\n%.2f",negativeCnt,positiveSum/(double)(20-negativeCnt-zeroCnt));
    }
}

1008 C基礎-公約公倍

注意一下題幹有問題,是空行不是空格。

 1 import java.util.*;
 2 import java.io.*;
 3 import java.math.BigInteger;
 4 
 5 public class Main
 6 {
 7     static int gcd(int x,int y)
 8     {
 9         if(y==0) return x;
10         else return gcd(y,x%y);
11     }
12     static int lcm(int x, int y)
13     {
14         return x/gcd(x,y)*y;
15     }
16     public static void main(String args[])
17     {
18         Scanner cin=new Scanner(System.in);
19         int n=cin.nextInt(),m=cin.nextInt();
20         System.out.println(String.valueOf(gcd(n,m))+"\n"+String.valueOf(lcm(n,m)));
21     }
22 }

 

1009 C基礎-統計字元

import java.util.*;
import java.io.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        String inputStr=sc.nextLine();
        int ch=0,sp=0,num=0,other=0;
        for(int i=0;i!=inputStr.length();++i)
        {
            if(Character.isDigit(inputStr.charAt(i)))
                num++;
            else if(Character.isLetter(inputStr.charAt(i)))
                ch++;
            else if(inputStr.charAt(i)==' ') sp++;
            else other++;
        }
        System.out.printf("%d\n%d\n%d\n%d\n",ch,sp,num,other);
    }
}

1010 C基礎-階乘數列

import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        BigInteger x=BigInteger.valueOf(0);
        for(int i=1;i<=30;++i)
        {
            BigInteger tmp=BigInteger.valueOf(1);
            for(int j=1;j<=i;++j)
            {
                tmp=tmp.multiply(BigInteger.valueOf(j));
                //System.out.println(tmp.toString()+" "+String.valueOf(j));
            }
            //System.out.println(tmp);
            x=x.add(tmp);
        }
        System.out.printf("%.2e",x.doubleValue());
    }
}

1011 C基礎-進位制轉換

import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        System.out.println(Integer.toOctalString(x));
    }
}

1012 SZ斐波拉契數列

儲存計算結果以加快運算速度。注意每次清空。

import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    static long[] arr=new long[35];
    static int a,b;
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        while(T--!=0)
        {
            for(int i=0;i<=30;++i) arr[i]=0;
            a=sc.nextInt();
            b=sc.nextInt();
            int n=sc.nextInt();
            System.out.println(f(n));
        }
    }

    public static long f(int x)
    {
        //System.out.println(x);
        if(arr[x]!=0) return arr[x];
        else
        {
            if(x==1) arr[x]=a;
            else if(x==2) arr[x]=b;
            else if(x%2==1) arr[x]=f(x-1)+f(x-2);
            else arr[x]=f(x-1)+f(x-2)+f(x-3);
            return arr[x];
        }
    }
}

相關文章