import java.util.Scanner;
public class Palindrome{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str;
while( sc.hasNext() ){
str = sc.nextLine();
String tmp = "";
int length = str.length() - 1;
for(int i = length; i >= 0; i--){
tmp += str.charAt(i);
}
if( str.equals(tmp) ){
System.out.println("yes");
}
else{
System.out.println("no");
}
}
}
}
題目
參考程式
2012年4月13日 星期五
Java 迴文
2012年4月12日 星期四
Java 阿姆斯壯數
import java.util.Scanner;
public class ArmstrongNumber{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a,b;
while( sc.hasNext() ){
a = sc.nextInt();
b = sc.nextInt();
int count = 0;
for(int i = a; i <= b; i++){
boolean ans = false;
int length = String.valueOf(i).length();
int sum = 0;
switch( length ){
case 7:
sum += Math.pow(i/1000000,length);
case 6:
sum += Math.pow((i%1000000)/100000,length);
case 5:
sum += Math.pow((i%100000)/10000,length);
case 4:
sum += Math.pow((i%10000)/1000,length);
case 3:
sum += Math.pow((i%1000)/100,length);
case 2:
sum += Math.pow((i%100)/10,length);
case 1:
sum += Math.pow(i%10,length);
break;
}
if( sum == i ){
System.out.print( i + " ");
count++;
}
}
if( count != 0 ){
System.out.println();
}
else{
System.out.println("none");
}
}
}
}
題目
Java 數字翻轉
import java.util.Scanner;
public class NumberTurn{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str;
while( sc.hasNext() ){
str = sc.nextLine();
char c [] = str.toCharArray();
String tmp = "";
for( int i = c.length - 1; i >= 0; i-- ){
tmp += c[i];
}
System.out.println( Integer.parseInt(tmp) );
}
}
}
題目
參考程式
Java 二進位轉換
import java.util.Scanner;
public class CarryTransform{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int num;
while( sc.hasNext() ){
num = sc.nextInt();
System.out.println( Integer.toBinaryString(num) );
}
}
}
題目
Java 最大公因數
import java.util.Scanner;
public class GCD{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a,b;
while( sc.hasNext() ){
a = sc.nextInt();
b = sc.nextInt();
int i = 0;
while( b != 0 ){
i = a % b;
a = b;
b = i;
}
System.out.println( a );
}
}
}
題目
2012年4月9日 星期一
Java 身分證檢驗
import java.util.Scanner;
public class IdAuthentication{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str;
while( sc.hasNext() ){
str = sc.nextLine();
char c [] = str.toCharArray();
if( c[0] == 88 || c[0] == 89 ){
c[0] -= 58;
}
else if( c[0] >= 65 && c[0] <= 72 ){
c[0] -= 55;
}else if( c[0] >= 74 && c[0] <= 78){
c[0] -= 56;
}else if( c[0] >= 80 && c[0] <= 86){
c[0] -= 57;
}else{
c[0] = (c[0] == 73) ? 34 : (c[0] == 79) ? 35 :
(c[0] == 87) ? 32 : (c[0] == 90) ? 33 : c[0];
}
int sum = c[0]/10 + (c[0]%10)*9 + (c[9]-48);
for(int i = 1; i < c.length-1; i++){
sum += (c[i]-48)*(9-i);
}
if( sum % 10 == 0 ){
System.out.println("real");
}
else{
System.out.println("fake");
}
}
}
}
題目
2012年4月6日 星期五
Java 數獨判斷
import java.util.Scanner;
public class Sudoku{
public static int array [][] = new int [9][9];
public static boolean checkRow(int row){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[row][i];
}
if( sum < 45 || sum > 45 ){
return false;
}
return true;
}
public static boolean checkColumn(int column){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[i][column];
}
if( sum < 45 || sum > 45 ){
return false;
}
return true;
}
public static boolean checkLittlePalaceGrid(int box){
int sum = 0;
int row = (box / 3) * 3;
int column = (box < 3) ? box*3 : (box < 6) ? (box - 3)*3 : (box - 6)*3;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
sum += array[i + row][j + column];
}
}
if( sum < 45 || sum > 45 ){
return false;
}
return true;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
boolean row=true,column=true,lpg=true;
while( sc.hasNext() ){
for(int j = 0; j < 9; j++ ){
int a [] = {0,0,0,0,0,0,0,0,0};
for(int k = 0; k < 9; k++){
a[k] = sc.nextInt();
}
array[j] = a;
}
int i = 0;
while(i < array.length){
row = checkRow(i);
column = checkColumn(i);
lpg = checkLittlePalaceGrid(i);
if(row == false || column == false || lpg == false){
System.out.println("no");
break;
}
if( i == array.length - 1 ){
System.out.println("yes");
}
i++;
}
}
}
}
題目
2012年4月4日 星期三
Java 矩陣翻轉
import java.util.Scanner;
public class Matrix{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int row,colum;
int matrix [][];
while(sc.hasNext()){
row = sc.nextInt();
colum = sc.nextInt();
matrix = new int [row][colum];
for( int i = 0; i < row; i++ ){
for( int j = 0; j < colum; j++){
matrix[i][j] = sc.nextInt();
}
}
for( int i = 0; i < colum; i++ ){
for( int j = 0; j < row; j++){
System.out.print(matrix[j][i] + " ");
}
System.out.println();
}
}
}
}
題目
訂閱:
文章 (Atom)