最后3题答案
第8题 参考答案
import java.util.Scanner;
public class strpro {
/**
* @param args
* 在实际的开发工作中,对字符串的处理是最常见的编程任务。本题目即是要求程序对用户输入的串进行处理。具体规则如下:
1. 把每个单词的首字母变为大写。
2. 把数字与字母之间用下划线字符(_)分开,使得更清晰
3. 把单词中间有多个空格的调整为1个空格。
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String str=scan.nextLine();
if(str.trim().equals(""))
return;
Integer len=str.length();
char c;
String str1="",result="";
for(int i=0;i<len;i++)
{
c=str.charAt(i);
if(c!=' ' || i==(len-1))
{
str1=str1+c;
}
else
{
if(str1=="" && c==' ')
continue;
str1=str1.toUpperCase().charAt(0)+str1.substring(1);
str1=processNumChar(str1);
str1=str1+" ";
result=result+str1;
str1="";
}
}
str1=str1.toUpperCase().charAt(0)+str1.substring(1);
str1=processNumChar(str1);
result=result+str1;
System.out.println(result);
}
static String processNumChar(String str)
{
char c;
boolean hasnum=false,hasalf=false;
String strnum="",stralf="",strresult="";
for(int i=0;i<str.length();i++)
{
c=str.charAt(i);
if(c>='0' && c<='9')
{
if(hasalf==true)
{
strresult=strresult+stralf;
strresult = strresult + "_";
stralf="";
hasalf=false;
}
hasnum=true;
strnum=strnum+c;
continue;
}
if((c>='a' && c<='z') || (c>='A' && c<='Z'))
{
if(hasnum==true)
{
strresult=strresult+strnum;
strresult = strresult + "_";
strnum="";
hasnum=false;
}
hasalf=true;
stralf=stralf+c;
continue;
}
}
if(hasnum==true)
strresult = strresult + strnum;
if(hasalf==true)
strresult = strresult + stralf;
return strresult;
}
}
第9题 参考答案
import java.util.Scanner;
public class exe9 {
/**
* @param args
*/
public int a[][];
public int d;
public int num=1;
public void init()
{
Scanner scan = new Scanner(System.in);
d=scan.nextInt();
a=new int[d][d];
for(int i=0;i<d;i++)
for(int j=0;j<d;j++)
a[i][j]=-1;
}
public int pjie(int n)
{
int r=0;
while(n>0)
{
r=r+n;
n--;
}
return r;
}
public void setnum()
{
int max=pjie(d);
int col=0;
int row=0;
int[] point ={row,col};
while(num<=max)
{
setrow(point);
row=point[0];col=point[1];
col=col-1;row=row+1;
point[0]=row;
point[1]=col;
setarrow(point);
row=point[0];
col=point[1];
row=row-1;
point[0]=row;
setcol(point);
col=point[1];
col=col+1;
point[1]=col;
}
}
public void setrow(int[] point)
{
int row=point[0];
int col=point[1];
while(col<d && a[row][col]==-1)
{
a[row][col]=num;
col=col+1;
num++;
}
point[1]=col-1;
}
public void setarrow(int[] point)
{
int row=point[0];int col=point[1];
while(col>=0 && row<d && a[row][col]==-1 )
{
a[row][col]=num;
row++;
col--;
num++;
}
point[0]=row-1;
point[1]=col+1;
}
public void setcol(int[] point)
{
int row=point[0];
int col=point[1];
while(row>=0 && a[row][col]==-1 )
{
a[row][col]=num;
row--;
num++;
}
point[0]=row+1;
}
public void printit()
{
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
//if(a[i][j]!=-1)
System.out.printf("%4d",a[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
exe9 e=new exe9();
e.init();
e.setnum();
e.printit();
}
}
第10题 参考答案
public class exe101 {
static int a[]=new int[10];
public static void print(){
System.out.println("可能的组合是:");
String str="";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
for(int l=0;l<2;l++){
for(int m=0;m<2;m++){
for(int t=0;t<2;t++){
for(int y=0;y<2;y++){
for(int u=0;u<2;u++){
for(int q=0;q<2;q++){
for(int w=0;w<2;w++){
a[0]=i;a[1]=j;a[2]=k;a[3]=l;a[4]=m;
a[5]=t;a[6]=y;a[7]=u;a[8]=q;a[9]=w;
if(check(a)){
if(a[0] == 1){
str+="A ";
}
if(a[1] == 1){
str+="B ";
}
if(a[2] == 1){
str+="C ";
}
if(a[3] == 1){
str+="D ";
}
if(a[4] == 1){
str+="E ";
}
if(a[5] == 1){
str+="F ";
}
if(a[6] == 1){
str+="G ";
}
if(a[7] == 1){
str+="H ";
}
if(a[8] == 1){
str+="I ";
}
if(a[9] == 1){
str+="J ";
}
System.out.println(str);
}
str="";
}
}
}
}
}
}
}
}
}
}
}
public static boolean check(int[] a){
if(a[0]==1&&a[1]==0) return false;
if((a[2]==0&&a[3]==1)) return false; //C不参加,D也不参加
if(a[0]==1&&a[2]==1) return false; //A和C中只能有一个人参加
if((a[1]==1&&a[3]==1)||(a[1]==0&&a[3]==0)) return false; //B和D中有且仅有一个人参加
if(a[3]+a[4]+a[5]+a[6]+a[7]<2) return false;//D、E、F、G、H 中至少有2人参加;
if((a[2]==1&&a[6]!=1)||(a[2]!=1&&a[6]==1)) return false;// C和G或者都参加,或者都不参加;
if(a[2]+a[4]+a[6]+a[8]>2) return false;//C、E、G、I中至多只能2人参加
if((a[4]==1&&a[5]!=1&&a[6]==1)||(a[4]==1&&a[5]==1&&a[6]!=1)||(a[4]==1&&a[5]!=1&&a[6]!=1))
return false;//如果E参加,那么F和G也都参加。
if((a[5]==1&&a[6]!=0&&a[7]==0)||(a[5]==1&&a[6]==0&&a[7]!=0)||(a[5]==1&&a[6]!=0&&a[7]!=0))
return false;//如果F参加,G、H就不能参加
if((a[8]==0&&a[9]==0&&a[7]==0)) return false;//如果I、J都不参加,H必须参加
return true;
}
public static void main(String[] args) {
print();
}
}