1. 程式人生 > >「題目代碼」P1060~P1065(Java)

「題目代碼」P1060~P1065(Java)

\n boolean tostring string rime class scanner -- 譚浩強

P1060 譚浩強C語言(第三版)習題7.5

註意行末空格。

import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.Character;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        int[] arr=new int[10];
        for(int i=0;i!=10;++i)
            arr[i]=cin.nextInt();
        for(int i=9;i>=0;--i)
        {
            System.out.print(arr[i]);
            if(i!=0) System.out.print(" ");
            else System.out.print("\n");
        }
    }
}

P1061 譚浩強C語言(第三版)習題8.1

import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.Character;

public class Main
{
    static int gcd(int x,int y)
    {
        if(y==0) return x;
        else return gcd(y, x%y);
    }
    static int lcm(int x,int y)
    {
        return x/gcd(x,y)*y;
    }
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        int x=cin.nextInt(),y=cin.nextInt();
        System.out.println(String.valueOf(gcd(x,y))+" "+String.valueOf(lcm(x,y)));
    }
}

P1062 譚浩強C語言(第三版)習題8.2

import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.Character;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        double a=cin.nextDouble(),b=cin.nextDouble(), c=cin.nextDouble();
        double delta=b*b-4*a*c;
        if(Math.abs(delta)>1e-6 && delta<0)
        {
            System.out.printf("x1=%.3f+%.3fi x2=%.3f-%.3fi\n", -b/2/a,
                              Math.sqrt(-delta)/2/a, -b/2/a, Math.sqrt(-delta)/2/a);
        }
        else
        {
            System.out.printf("x1=%.3f x2=%.3f\n", -b/2/a-Math.sqrt(-delta)/2/a,
                              -b/2/a-Math.sqrt(delta)/2/a);
        }
    }
}

P1063 譚浩強C語言(第三版)習題8.3

import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.Character;

public class Main
{
    static void judge_prime(int x)
    {
        boolean ok=true;
        if(x==1) ok=false;
        else
        {
            for(int i=2; i*i<=x; ++i)
            {
                if(x%i==0)
                {
                    ok=false;
                    break;
                }
            }
        }
        if(ok)
            System.out.println("prime");
        else
            System.out.println("not prime");
    }
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt();
        judge_prime(a);
    }
}

P1064 譚浩強C語言(第三版)習題8.4

import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.Character;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        int[][] arr=new int[3][3];
        for(int i=0;i!=3;++i)
            for(int j=0;j!=3;++j)
                arr[i][j]=cin.nextInt();
        for(int j=0;j!=3;++j)
        {
            for(int i=0;i!=3;++i)
            {
                System.out.print(arr[i][j]);
                System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
}

P1065 譚浩強C語言(第三版)習題8.5

import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.Character;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        String a=cin.next();
        String reverse=new StringBuffer(a).reverse().toString();
        System.out.println(reverse);
    }
}

「題目代碼」P1060~P1065(Java)