java语言程序设计基础篇第六章编程练习题

时间:2023-02-22 21:49:00

1

import java.util.Scanner;
public class Main{
public static void main(String[] args){
final int NUMBER = 100;
int count = 0;
Scanner input = new Scanner(System.in);
while(count < NUMBER){
System.out.print(getPentagonalNumber(count+1) + " ");
if((count+1) % 10 == 0)
System.out.println();
++count;
}
}

public static int getPentagonalNumber(int n){
return n*(3*n-1)/2;
}
}

2

import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
System.out.println(sumDigits(n));
}

public static int sumDigits(int n){
int sum = 0;
while(n != 0){
sum += n % 10;
n = n / 10;
}
return sum;
}
}
3import java.util.Scanner;    public class Main{      public static void main(String[] args){    Scanner input = new Scanner(System.in);    int num = input.nextInt();    if(isPalindrome(num))    System.out.println(num + " is palindrome.");    else    System.out.println(num + " is not palindrome.");        }        public static int reverse(int number){    int num = 0;    while(number != 0){    num = num*10 + number%10;    number /= 10;    }    return num;    }        public static boolean isPalindrome(int number){    if(number == reverse(number))    return true;    return false;    }    }4import java.util.Scanner;    public class Main{      public static void main(String[] args){    Scanner input = new Scanner(System.in);    int num = input.nextInt();    System.out.println(reverse(num));    }        public static int reverse(int number){    int num = 0;    while(number != 0){    num = num*10 + number%10;    number /= 10;    }    return num;    }}5import java.util.Scanner;    public class Main{      public static void main(String[] args){    Scanner input = new Scanner(System.in);    double num1 = input.nextDouble();    double num2 = input.nextDouble();    double num3 = input.nextDouble();    displaySortedNumber(num1,num2,num3);    }        public static void displaySortedNumber(double num1, double num2, double num3){    double temp = 0;    if(num1 > num2){    temp = num1;    num1 = num2;    num2 = temp;    }    if(num1 > num3){    temp = num1;    num1 = num3;    num3 = temp;    }        if(num2 > num3){    temp = num2;    num2 = num3;    num3 = temp;    }    System.out.println(num1 + " " + num2 + " " + num3);    }}6import java.util.Scanner;    public class Main{      public static void main(String[] args){    Scanner input = new Scanner(System.in);    int num = input.nextInt();    displayPattern(num);    }        public static void displayPattern(int n){    for(int i = 1; i <= n; ++i){    for(int j = 1; j <= n-i; ++j)    System.out.print(" ");    for(int j = i; j > 0; --j)    System.out.print(j);    System.out.println();    }    }}7,8,9套公式10import java.util.Scanner;    public class Main{      public static void main(String[] args){    int count = 0;    for(int i = 1; i <= 10000; ++i){    if(isPrime(i))    ++count;    }    System.out.println(count);    }        public static boolean isPrime(int n){    if(n == 1)    return false;    for(int i = 2; i*i <= n; ++i){    if(n%i == 0)    return false;    }    return true;    }}11。。。。12import java.util.Scanner;    public class Main{      public static void main(String[] args){    printChars('1','Z',10);    }        public static void printChars(char ch1, char ch2, int numberPerLine){    int count = 0;    for(int i = ch1; i <= ch2; ++i){    System.out.print((char)i);    ++count;    if(count%10 == 0)    System.out.println();    }    }}13import java.util.Scanner;    public class Main{      public static void main(String[] args){    for(int i = 1; i <= 20; ++i){    System.out.println(i + "        " + m(i));    }    }        public static double m(int n){    double sum = 0.0;    for(int i = 1; i <= n; ++i)    sum += (double)i/(double)(i+1);    return sum;    }}14import java.util.Scanner;    public class Main{      public static void main(String[] args){    for(int i = 1; i <= 901; i += 100){    System.out.println(i + "        " + m(i));    }    }        public static double m(int n){    double sum = 0.0;    int one = 1;    for(int i = 1; i <= n; ++i){    sum += one/(double)(2*i-1);    one = -one;    }    return 4*sum;    }}15,16。。。。。。。17import java.util.Scanner;    public class Main{      public static void main(String[] args){    Scanner input = new Scanner(System.in);    int n = input.nextInt();    printMatrix(n);    }        public static void printMatrix(int n){    for(int i = 0; i < n; ++i){    for(int j = 0; j < n; ++j){    System.out.print((System.currentTimeMillis()/(j+i+1))%2 + " ");    }    System.out.println();    }    }}18import java.util.Scanner;    public class Main{      public static void main(String[] args){    Scanner input = new Scanner(System.in);    System.out.print("Enter password:");    String password = input.next();    if(isValidPassWord(password)){    System.out.println("Valid Password");    }    else    System.out.println("Invalid Password");    }        public static boolean isValidPassWord(String str){    if(str.length() < 8)    return false;    int count = 0;    for(int i = 0; i < str.length(); ++i){    if(!Character.isDigit(str.charAt(i)) && !Character.isLetter(str.charAt(i)))    return false;    if(Character.isDigit(str.charAt(i)))    ++count;    }    if(count < 2)    return false;    return true;    }}19,20,21。。。。。。22import java.util.Scanner;    public class Main{      public static void main(String[] args){    Scanner input = new Scanner(System.in);    double n = input.nextDouble();    System.out.println(sqrt(n));    }        public static double sqrt(double n){    double nextGuess = 0;    double lastGuess = 1.0;    while(true){    nextGuess = (lastGuess + n/lastGuess) / 2;    if(Math.abs(nextGuess - lastGuess) < 0.0001)    return nextGuess;    lastGuess = nextGuess;    }    }}23。。。。。24参考第五章29题25貌似和程序清单2-7差不多26import java.util.Scanner;    public class Main{      public static void main(String[] args){    int count = 0;    int i = 1;    while(count < 100){    if(isPrime(i) && isPalindrome(i)){    System.out.print(i + " ");    ++count;    if(count % 10 == 0)    System.out.println();    }    ++i;    }    }        public static boolean isPalindrome(int number){    if(number == reverse(number))    return true;    return false;    }        public static int reverse(int number){    int num = 0;    while(number != 0){    num = num*10 + number%10;    number /= 10;    }    return num;    }        public static boolean isPrime(int n){    if(n == 1)    return false;    for(int i = 2; i*i <= n; ++i){    if(n%i == 0)    return false;    }    return true;    }}27import java.util.Scanner;    public class Main{      public static void main(String[] args){    int count = 0;    int i = 1;    while(count < 100){    if(isPrime(i) && isPrime(reverse(i))){    System.out.print(i + " ");    ++count;    if(count % 10 == 0)    System.out.println();    }    ++i;    }    }        public static int reverse(int number){    int num = 0;    while(number != 0){    num = num*10 + number%10;    number /= 10;    }    return num;    }        public static boolean isPrime(int n){    if(n == 1)    return false;    for(int i = 2; i*i <= n; ++i){    if(n%i == 0)    return false;    }    return true;    }}28import java.util.Scanner;    public class Main{      public static void main(String[] args){    for(int p = 1; p <= 31; ++p){    int n = (int)Math.pow(2, p)-1;    if(isPrime(n))    System.out.println(p + "        " + n);    }    }    public static boolean isPrime(int n){    if(n == 1)    return false;    for(int i = 2; i*i <= n; ++i){    if(n%i == 0)    return false;    }    return true;    }}29import java.util.Scanner;    public class Main{      public static void main(String[] args){    int record = 2;    for(int i = 3; i <= 1000; ++i){    if(isPrime(i)){    if(i - record == 2){    System.out.println("(" + record + ", " + i + ")");    record = i;    }    else if(i - record < 2 && !isPrime(i+1))    record = i;    else if(i - record > 2)    record = i;    }    }    }    public static boolean isPrime(int n){    if(n == 1)    return false;    for(int i = 2; i*i <= n; ++i){    if(n%i == 0)    return false;    }    return true;    }}30。。。。。。31import java.util.Scanner;public class Main {public static void main(String[] args){Scanner cin = new Scanner(System.in);long num = cin.nextLong();int size = getSize(num);boolean flag = false;if(prefixMatched(num,4) || prefixMatched(num,5) || prefixMatched(num,37) || prefixMatched(num,6))flag = true;if(size >= 13 && size <= 16 && flag && isValid(num)){System.out.println(num + " is valid");}else{System.out.println(num + " is invalid");}}public static boolean isValid(long number){int sum1 = sumOfDoubleEvenPlace(number);int sum2 = sumOfOddPlace(number);int sum = sum1+sum2;if(sum%10 == 0) return true;else return false;}public static int sumOfDoubleEvenPlace(long number){int len = 0;int sum = 0;int[] numarr = new int[20];int index = 0;while(number != 0){numarr[index++] = (int)(number%10);number /= 10;}for(int i = index-1; i >= 0; --i){++len;if(len%2 == 1){sum += getDigit(numarr[i]*2);}}return sum;}public static int getDigit(int number){if(number < 10)return number;return (number%10 + number/10);}public static int sumOfOddPlace(long number){int len = 0;int sum = 0;int[] numarr = new int[20];int index = 0;while(number != 0){numarr[index++] = (int)(number%10);number /= 10;}for(int i = index-1; i >= 0; --i){++len;if(len%2 == 0){sum += numarr[i];}}return sum;}public static boolean prefixMatched(long number, int d){String str = String.valueOf(number);String substr = String.valueOf(d);if(str.startsWith(substr))return true;return false;}public static int getSize(long d){int len = 0;while(d != 0){d /= 10;++len;}return len;}//感觉题目用不到这个方法啊public static long getPrefix(long number, int k){return 0;}}

待续。。。。。