/* * 分析,对于这道题目,把每一个要求细化 */ import java.util.*; public class T6 { public static void main( String[] args ) { Scanner sc = new Scanner( System.in ); System.out.print( "请输入行数:" ); int rows = sc.nextInt(); int score[] = new int[rows]; for ( int i = 0; i < rows; i++ ) { String str = sc.next(); int a = Integer.parseInt( str.charAt( 0 ) + "" ); int b = Integer.parseInt( str.charAt( 1 ) + "" ); int c = Integer.parseInt( str.charAt( 2 ) + "" ); int d = Integer.parseInt( str.charAt( 3 ) + "" ); score[i] = test6( a, b, c, d ); } for ( int i : score ) System.out.println( i ); } public static int test6( int a, int b, int c, int d ) { int score = 0; /*不管升序还是降序,都加5分。例如:5678,4321都满足加分标准 */ if ( (a == b + 1 && b == c + 1 && c == d + 1) || (a == b - 1 && b == c - 1 && c == d - 1) ) score += 5; /* * 前三个数字相同,或后三个数字相同,都加3分。 * 例如:4888,6665,7777都满足加分的标准。 * 7777因为满足这条标准两次,所以这条规则给它加了6分。 */ if ( a == b && b == c ) score += 3; if ( b == c && c == d ) score += 3; /* * 符合AABB或者ABAB模式的加1分。例如:2255,3939, * 7777因为满足这条标准两次,所以这条标准给它加了2分。 */ if ( a == b && c == d ) score += 1; if ( a == c && b == d ) score += 1; /* * 含有:6,8,9中任何一个数字,每出现一次加1分。 * 例如4326,6875,9918都符合加分标准。 * 其中,6875被加2分;9918被加3分。 */ if ( a == 6 ) score += 1; else if ( a == 8 ) score += 1; else if ( a == 9 ) score += 1; if ( b == 6 ) score += 1; else if ( b == 8 ) score += 1; else if ( b == 9 ) score += 1; if ( c == 6 ) score += 1; else if ( c == 8 ) score += 1; else if ( c == 9 ) score += 1; if ( d == 6 ) score += 1; else if ( d == 8 ) score += 1; else if ( d == 9 ) score += 1; return(score); } }
/* * 分析:这个题目比较简单,不过注意数值越界 */ public class T13 { public static void main( String[] args ) { double sum = test(); System.out.println( "sum=" + sum ); } public static double test() { double sum = 0; double a = 1; for ( int i = 2; i <= 64; i++ ) { a = a * 2; sum += a; } return(sum + 1); } }
/* * 分析:用坐标的方法,解析到得数字然后进行进行位移x,y 然后direction 换方向 */ import java.util.*; import java.util.regex.*; public class T14_1 { public static void main( String[] args ) { Scanner sc = new Scanner( System.in ); System.out.print( "请输入指令条数(指令条数<100): " ); int x = sc.nextInt(); if ( x < 100 ) { System.out.println( "请输入指令(指令的长度不超过256个字符): " ); String commands[] = new String[x]; Double result[] = new Double[x]; for ( int i = 0; i < x; i++ ) { commands[i] = sc.next(); if ( commands[i].length() < 256 && Pattern.matches( "^[LR0-9]+$", commands[i] ) ) { result[i] = test( commands[i] ); }else { System.out.println( "输入的指令长度超过了256 或 指令不正确(每条指令只由L、R和数字组成(数字是0~100之间的整数))!!!" ); System.exit( 0 ); } } System.out.println( "输出: " ); for ( double d : result ) { System.out.printf( "%.2f\n", d ); } }else { System.out.println( "输入的指令超过了100条!!!" ); } } public static double test( String command ) { int direction = 1; /* 0 x负半轴 1 y正半轴 2 x正半轴 3 y负半轴 */ double x = 0; double y = 0; String str = "0"; int i = 0; char temp; while ( i < command.length() ) { temp = command.charAt( i ); if ( temp >= '0' && temp <= '9' ) { str += temp; } else if ( 'L' == temp ) { int length = Integer.parseInt( str ); switch ( direction ) { case 0: x = x - length; break; case 1: y = y + length; break; case 2: x = x + length; break; case 3: y = y - length; break; } str = "0"; direction = (direction - 1); /* 调整当前位置的方向 */ if ( direction == -1 ) { direction = 3; } } else if ( 'R' == temp ) { int length = Integer.parseInt( str ); switch ( direction ) { case 0: x = x - length; break; case 1: y = y + length; break; case 2: x = x + length; break; case 3: y = y - length; break; } str = "0"; direction = (direction + 1); if ( direction == 4 ) { direction = 0; } } i++; } int length = Integer.parseInt( str ); switch ( direction ) { case 0: x = x - length; break; case 1: y = y + length; break; case 2: x = x + length; break; case 3: y = y - length; break; } return(Math.sqrt( x * x + y * y ) ); } }
A C 一周中必须至少有4天能见面(即同时上班)。 你的任务是:编写程序,列出ABCDE所有可能的一周排班情况。 工作日记为1,休息日记为0
A B C D E 每人占用1行记录,从星期一开始。 【输入、输出格式要求】 程序没有输入,要求输出所有可能的方案。 每个方案是7×5的矩阵。只有1和0组成。 矩阵中的列表示星期几,从星期一开始。 矩阵的行分别表示A,B,C,D,E的作息时间表。 多个矩阵间用空行分隔开。 例如,如下的矩阵就是一个合格的解。请编程输出所有解(多个解的前后顺序不重要)。 0110111 1101110 0110111 1101110 1110110