产生不重复的随机数:1
public
class
Test {
public
static
void
main(String[] args) {
//产生10个15以内不重复的数字。
Test t =
new
Test();
t.test2();
}
public
void
test1(){
Random r =
new
Random();
int
[] arr =
new
int
[10];
for
(
int
i = 0; i < arr.
length
; i++) {
boolean
flag=
false
;
int
num = r.nextInt(15);
for
(
int
j = 0; j < arr.
length
; j++) {
if
(num==arr[j]){
i--;
flag=
true
;
break
;
}
}
if
(!flag)
arr[i] = num;
}
for
(
int
c : arr) {
System.
out
.print(
" "
+c);
}
}
public
void
test2(){
Random r =
new
Random();
long
[] arr =
new
long
[935];
boolean
flag[]=
new
boolean
[1000];
flag[0]=
true
;
for
(
int
k=1;k<flag.
length
;k++) {
flag[k]=
false
;
}
for
(
int
i = 0; i < arr.
length
; i++) {
int
num = r.nextInt(1000);
if
(flag[num]==
false
){
arr[i] = num;
flag[num]=
true
;
}
else
{
i--;
}
}
System.
out
.println(
"--------------"
);
for
(
long
c : arr) {
System.
out
.print(
" "
+c);
}
}
}
打印以下图像:(次方)2
public
class
Test1 {
/*
* 1 0
* 2 1
* 3 4 2
* 5 6 7 8 3
* 9 10 11 12 13 14 15 16 4
*/
public
static
void
main(String[] args) {
System.
out
.println();
Test1 t=
new
Test1();
t.printData(135);
}
//解法
public
void
printData(
int
n) {
int
c=0;
for
(
int
i = 1; i <= n; i++) {
if
( i<=Math. pow(2, c)){
System.
out
.print( i+
" "
);
}
else
{
++c;
-- i;
System.
out
.println();
}
}
}
}
/**
* 这是用java反射来实现对对象的属性,方法,类名的取值。再组成一条万能的insert 语句。3
*
@author
小高
*/
public
class
Test2 {
public
static
void
main(String[] args)
throws
Exception {
Test2 t=
new
Test2();
Student stu=
new
Student();
//可以对任意对象操作
stu.setName(
"小高"
);
stu.setAge(20);
stu.setId(11);
String sql=t.createInsertSql(stu);
System.
out
.println(sql);
}
private
String createInsertSql(Object obj)
throws
Exception {
Class cls=obj.getClass();
String sql=
"insert into "
+cls.getSimpleName()+
" ("
;
//类名
Field fields[]=cls.getDeclaredFields();
String columnName=
""
;
//列名
String columnValue=
""
;
//列值
Object objs[]=
new
Object[fields.
length
];
//多少个字段就多少个值
for
(
int
i = 0; i < fields.
length
; i++) {
String fieldName=fields[i].getName();
//获得字段
String fieldMethodOfget=fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1, fieldName.length());
Method methodofget = cls.getMethod(
"get"
+fieldMethodOfget,
null
);
Object value= methodofget.invoke(obj,
null
);
//字段的值
if
(value!=
null
){
//判断是否设置值
columnName+=
","
+fieldName;
if
(value.getClass().getSimpleName().equals(
"String"
)){
columnValue+=
",'"
+value+
"'"
;
}
else
{
columnValue+=
","
+value;
}
}
}
columnName=columnName.substring(1, columnName.length());
sql+=columnName+
") values ("
;
columnValue=columnValue.substring(1, columnValue.length());
sql+=columnValue +
")"
;
return
sql;
}
}
package
com.one1;
public
class
Test3 {
/* 蛇形矩阵4
【样例输出】
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
*/
public
static
void
main(String[] args) {
Test3 t=
new
Test3();
t.test(8);
}
public
void
test(
int
n) {
int
arr[][]=
new
int
[n][n];
arr[0][0]=1;
int
column=n;
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < column; j++) {
if
(j==0){
//第一列
if
(i>0)
arr[i][j]=arr[i-1][j]+i;
}
else
{
//一行中某列与前一列的关系
arr[i][j]=arr[i][j-1]+(i+1+j);
}
}
column-=1;
}
//输出
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < n; j++) {
if
(arr[i][j]!=0)
System.
out
.print(arr[i][j]+
" "
);
}
System.
out
.println();
//换行
}
}
}
public
class
Test4 {
/*5
01--02 06--07 15--16 28--29
/ / / / / / /
03 05 08 14 17 27 30 43
| / / / / / / / |
04 09 13 18 26 31 42 44
/ / / / / / /
10 12 19 25 32 41 45 54
| / / / / / / / |
11 20 24 33 40 46 53 55
/ / / / / / /
21 23 34 39 47 52 56 61
| / / / / / / / |
22 35 38 48 51 57 60 62
/ / / / / / /
36--37 49--50 58--59 63--64
*/
public
static
void
main(String[] args) {
Test4 t=
new
Test4();
t.printData(t.test2(8));
}
//核心算法
public
int
[][] test2(
int
n){
int
arr[][]=
new
int
[n][n];
for
(
int
i = 0; i < n; i++) {
for
(
int
j=0,c=i;j<=i&&c>=0;j++,c--) {
if
(i%2==0){
//偶数行
if
(c==i){
//建立与上次循环的关系
if
(c==0)
arr[c][j]=1;
else
arr[c][j]=arr[c-1][j]+1;
}
else
arr[c][j]=arr[c+1][j-1]+1;
}
else
{
//奇数行
if
(c==i)
arr[j][c]=arr[j][c-1]+1;
//建立与上次循环的关系
else
arr[j][c]=arr[j-1][c+1]+1;
}
}
}
return
arr;
}
//打印数据
public
void
printData(
int
arr[][]){
int
n=arr.
length
;
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < n; j++) {
if
(arr[i][j]>=10)
System.
out
.print(arr[i][j]+
" "
);
else
if
(arr[i][j]!=0)
System.
out
.print(
"0"
+arr[i][j]+
" "
);
}
System.
out
.println();
}
}
}
public
class
Test5 {
/*6
1
5 2
8 6 3
10 9 7 4
分析:
arr[1][0] arr[3][3]
arr[2][0] arr[3][2]
*/
public
static
void
main(String[] args) {
Test5 t=
new
Test5();
Test4 t2=
new
Test4();
//输出
t2.printData(t.test(10));
}
public
int
[][] test(
int
n){
int
arr[][]=
new
int
[n][n];
for
(
int
i = 0; i < n; i++) {
//控制次数
for
(
int
j = 0,c=i;j<n&&c<n; j++,c++) {
//一次循环打印的列数
if
(c==i){
if
(c==0)
arr[c][j]=1;
else
arr[c][0]=arr[n-1][n-c]+1;
}
else
{
arr[c][j]=arr[c-1][j-1]+1;
}
}
}
return
arr;
}
}
//冒泡排序,选择排序,插入排序。7
package
com.mysort;
public
class
One {
public
static
void
main(String[] args) {
int
arr[]={1,4,9,7,3,8,2,6};
/*冒泡排序
for ( int i = 0; i < arr.length-1; i++) {
for ( int j = i+1; j < arr.length; j++) {
if( arr[i]>arr [j]){
int t=arr [i];
arr[i]=arr [j];
arr[j]=t;
}
}
}*/
// 选择排序:每次从剩下的找到最大值放在已经排好中的后面
int
index=0;
//改变索引
int
max;
//默认最大
for
(
int
i = 0; i < arr.
length
; i++) {
max=arr[index];
//最大值
int
maxIndex=index;
//最大下标
for
(
int
j=index; j < arr.
length
; j++) {
//找比较每一轮的最大值,并且记录下标位置
if
(max<arr[j]){
max=arr[j];
maxIndex=j;
}
}
//把找到的最大值放到第一个位置,第一个位置的数放到这一轮找到最大值的位置
int
t=arr[i];
arr[i]=max;
arr[maxIndex]=t;
//从下一个开始找
index++;
}
/* 插入排序: 每次和最后一个比较,若比前面下则再换位置,直到不比前面小。
int count=0;
int pos ;
for ( int i = 1; i < arr.length; i++) {
pos=count; //一开始进来就是和最后一个数比
int index=i;
while( arr[index]<arr[pos ]&&pos>=0){ //排序数 如果比已经排好的最后一个数小则,和前一个比较
int t=arr [pos];
arr[pos ]=arr[index];
arr[index]=t;
index-=1; //注意:这是个换了位置的数
pos--; //准备和前一个比较
}
count++;
}*/
/* 输出 */
for
(
int
j = 0; j < arr.
length
; j++) {
System.
out
.print(arr[j]+
" "
);
}
}
}
//快速排序8
public
class
Two{
public
static
void
main(String[] args) {
long
start=System. currentTimeMillis();
int
arr[] ={12,34,11,99,54,21,9,5,101};
Two t=
new
Two();
t.quicksort(arr,0,arr.
length
-1);
t.print(arr);
System.
out
.println(
"所花费时间:"
+(System.currentTimeMillis()-start));
}
public
void
quicksort(
int
arr[],
int
left,
int
right){
if
(left < right){
int
key = arr[left];
int
low = left;
int
high = right;
while
(low < high){
while
(low < high && arr[high] > key){
high--;
}
arr[low] = arr[high];
while
(low < high && arr[low] < key){
low++;
}
arr[high] = arr[low];
}
arr[low] = key;
quicksort(arr,left,low-1);
quicksort(arr,low+1,right);
}
}
//打印
public
void
print(
int
arr[]) {
for
(
int
i = 0; i < arr.
length
; i++) {
System.
out
.print(arr[i]+
" "
);
}
}
}
//模仿遍历树节点,递归遍历。9
package
com.bean;
import
java.util.*;
public
class
LookWord {
private
ArrayList<TreeBean>
treeList
=
new
ArrayList<TreeBean>();
//初始
public
static
void
main(String[] args) {
LookWord lw =
new
LookWord();
lw.init();
// 初始化数据
//lw.findAll(lw.treeList); //查找指定的集合
lw.findWho(
"11"
,lw.
treeList
);
//找到指定的id 的集合
}
// 查找id的父节点
public
void
findWho(String id,ArrayList<TreeBean> ts) {
for
(TreeBean t : ts) {
if
(t.getId().equals(id)&&t.getChildren()!=
null
){
//找到了那个对象就不断查找出来
findAll(t.getChildren());
}
else
{
if
(t.getChildren()!=
null
){
findWho(id,t.getChildren());
}
}
}
}
// 根据查找的集合查找
public
void
findAll(ArrayList<TreeBean> findList) {
// 根据集合的大小找子集合作为查找集合的目标
for
(
int
i = 0; i < findList.size(); i++) {
TreeBean tb = findList.get(i);
System.
out
.println(tb.toString());
// 不管有没有子节点都要进行输出
if
(tb.getChildren() !=
null
) {
// 如果有子节点则继续输出
findAll(tb.getChildren());
// 如果子节点不为空则继续查找
}
}
}
// 初始化
private
void
init() {
ArrayList<TreeBean> children111 =
new
ArrayList<TreeBean>();
TreeBean tb111 =
new
TreeBean(
"1111"
,
"one1111"
,
"111"
);
TreeBean tb112 =
new
TreeBean(
"1112"
,
"one1112"
,
"111"
);
children111.add(tb111);
children111.add(tb112);
TreeBean tb11 =
new
TreeBean(
"111"
,
"one111"
,
"11"
);
tb11.setChildren(children111);
TreeBean tb12 =
new
TreeBean(
"112"
,
"one112"
,
"11"
);
TreeBean tb13 =
new
TreeBean(
"113"
,
"one113"
,
"11"
);
ArrayList<TreeBean> children11 =
new
ArrayList<TreeBean>();
children11.add(tb11);
children11.add(tb12);
children11.add(tb13);
// 为11 设置子节点
TreeBean tp11 =
new
TreeBean(
"11"
,
"one1"
,
"1"
);
tp11.setChildren(children11);
TreeBean tp12 =
new
TreeBean(
"12"
,
"one2"
,
"1"
);
ArrayList<TreeBean> children1 =
new
ArrayList<TreeBean>();
children1.add(tp11);
children1.add(tp12);
// 为1设置12,11子节点
TreeBean tb1 =
new
TreeBean(
"1"
,
"one"
,
"0"
);
tb1.setChildren(children1);
ArrayList<TreeBean> children2 =
new
ArrayList<TreeBean>();
children2.add(
new
TreeBean(
"21"
,
"two1"
,
"2"
));
children2.add(
new
TreeBean(
"22"
,
"two2"
,
"2"
));
// 为2设置子节点
TreeBean tb2 =
new
TreeBean(
"2"
,
"two"
,
"0"
);
tb2.setChildren(children2);
treeList
.add(tb1);
treeList
.add(tb2);
}
}
对象实体:
package
com.bean;
import
java.util.ArrayList;
public
class
TreeBean {
private
String
id
;
private
String
text
;
private
String
pid
;
private
ArrayList<TreeBean>
children
=
new
ArrayList<TreeBean>();
public
TreeBean(String id,String text ,String pid){
this
.
id
=id;
this
.
text
=text;
this
.
pid
=pid;
}
public
String getId() {
return
id
;
}
public
void
setId(String id) {
this
.
id
= id;
}
public
String getText() {
return
text
;
}
public
void
setText(String text) {
this
.
text
= text;
}
public
String getPid() {
return
pid
;
}
public
void
setPid(String pid) {
this
.
pid
= pid;
}
public
ArrayList<TreeBean> getChildren() {
return
children
;
}
public
void
setChildren(ArrayList<TreeBean> children) {
this
.
children
= children;
}
@Override
public
String toString() {
return
"TreeBean [id="
+
id
+
", text="
+
text
+
", pid="
+
pid
+
"]"
;
}
}
//排名前三名的同学成绩:10
public
class
Three {
String []
name
;
int
[]
score
;
public
String[] getName() {
return
name
;
}
public
void
setName (String[] name) {
this
.
name
= name;
}
public
int
[] getScore() {
return
score
;
}
public
void
setScore(
int
[] score) {
this
.
score
= score;
}
public
Three(
int
len){
String[] name=
new
String[len];
int
[] score=
new
int
[len];
}
public
Three(){
}
public
void
paiXu(){
int
temps ;
String tempn;
for
(
int
i=0;i<
score
.
length
;i++){
for
(
int
j=0;j<
score
.
length
;j++){
if
(
score
[i]<
score
[j]){
temps=
score
[i];
score
[i]=
score
[j];
score
[j]=temps;
tempn=
name
[i];
name
[i]=
name
[j];
name
[j]=tempn;
}
}
}
}
@Override
public
String toString() {
return
"Three [name="
+ Arrays. toString(
name
) +
", score="
+ Arrays. toString(
score
) +
"]"
;
}
public
void
sd(){
int
one=0,count1=0,two=0,count2=0,three=0,count3=0;
one=
score
[
score
.
length
-1];
//取第一名
for
(
int
i=0;i<
score
.
length
;i++){
if
(
score
[i]==one)
count1++;
}
two=
score
[
score
.
length
-1-count1];
// 取第二名 arr9 arr8 arr7
for
(
int
i=0;i<
score
.
length
;i++){
if
(
score
[i]==two)
count2++;
}
three=
score
[
score
.
length
-1-count1-count2];
// 取第二名 arr9 arr8 arr7 arr6 arr5
for
(
int
i=0;i<
score
.
length
;i++){
if
(
score
[i]==three)
count3++;
}
System.
out
.print(
"\n第一名"
+count1+
"个:"
);
for
(
int
i=
name
.
length
-1;i>
name
.
length
-1-count1;i--)
System.
out
.print(
name
[i]+
":"
+
score
[i]);
System.
out
.println(
"\n"
);
System.
out
.print(
"第二名"
+count2+
"个:"
);
for
(
int
i=
name
.
length
-1-count1;i>
name
.
length
-1-count1-count2;i--)
System.
out
.print(
name
[i]+
":"
+
score
[i]);
System.
out
.println(
"\n"
);
System.
out
.print(
"第三名"
+count3+
"个:"
);
for
(
int
i=
name
.
length
-1-count1-count2;i>
name
.
length
-1-count1-count2-count3;i--)
System.
out
.print(
name
[i]+
":"
+
score
[i]);
}
}
测试
public
class
ThreeTest {
public
static
void
main(String[] args) {
// 取前三名的成绩和名字
String name[]={
"张三1"
,
"张三2"
,
"张三3"
,
"张三4"
,
"张三5"
,
"张三6"
,
"张三7"
,
"张三8"
,
"张三9"
,
"张三10"
,};
int
score[]={98,98,97,96,96,56,77,65,22,87};
Three ts=
new
Three(name.
length
);
ts. setName(name);
ts.setScore(score);
ts.paiXu();
String result=ts.toString();
System.
out
.println(result);
//输出已经过排序的成绩和对应的名字
ts.sd();
}
}
/**
* 二分法查找11
*
*
@param
arr
* 数组
*
@param
num
* 要查找的数字
*
@return
返回位置
*/
package
mysort;
import
java.util.Scanner;
public
class
HalfSelect {
/**
* 二分法查找
*
*
@param
arr
* 数组
*
@param
num
* 要查找的数字
*
@return
返回位置
*/
private
static
int
halfLook(
int
[] arr,
int
num) {
// 二分法查找
int
middle;
int
start = 0, last = arr.
length
- 1;
if
(num < arr[start] || num > arr[last])
return
-1;
while
(
true
) {
middle = (last + start) / 2;
// 求中间位置
if
(start > last) {
break
;
}
if
(num > arr[middle]) {
start = middle + 1;
}
else
if
(num < arr[middle]) {
last = middle - 1;
}
else
return
middle;
// 相等则返回
}
return
-1;
}
public
static
void
halfShow(
int
arr[]) {
Scanner input =
new
Scanner(System.
in
);
System.
out
.println(
"请输入你要查找的数:"
);
int
n = input.nextInt();
int
w = halfLook(arr, n);
if
(w >= 0)
System.
out
.println((
new
StringBuilder(
"位置:"
)).append(w).toString());
else
System.
out
.println(
"没有该数!"
);
}
}
/**
* 拷贝文件(把D盘java1中的java文件拷贝到 jad文件夹改成 txt文件后缀名)12
*
@param
args
*
@throws
Exception
*/
package
mylove;
import
java.io.*;
public
class
CopyFile
{
/**
* 拷贝文件(把D盘java1中的java文件拷贝到 jad文件夹改成 txt文件后缀名)
*
@param
args
*
@throws
Exception
*/
public
static
void
main(String args[])
throws
Exception{
long
lasting = System. currentTimeMillis();
File f =
new
File(
"D:\\java1"
);
File md =
new
File(
"D:\\jad"
);
if
(!md.exists())
md.mkdir();
if
(f.exists() && f.isDirectory()){
File file[] = f.listFiles(
new
FilenameFilter() {
public
boolean
accept(File dir, String name)
{
return
name.endsWith(
".java"
);
}
});
for
(
int
i = 0; i < file.
length
; i++)
{
BufferedReader bf =
null
;
BufferedWriter bw =
null
;
bf =
new
BufferedReader(
new
InputStreamReader(
new
FileInputStream(file[i])));
String fname = file[i].getName().replaceAll(
"\\.java$"
,
".txt"
);
bw =
new
BufferedWriter(
new
OutputStreamWriter(
new
FileOutputStream(
new
File(md, fname))));
char
ch[] =
new
char
[1024];
for
(
int
len = 0; (len = bf.read(ch)) != -1;)
{
bw.write(ch, 0, len);
bw.newLine();
bw.flush();
}
bf.close();
bw.close();
}
}
else
{
System.
out
.println(
"无此文件夹"
);
}
System.
out
.println(System. currentTimeMillis() - lasting);
}
}
are you looking for a relativation ship to comple yourself ?
come on, don't give me that look.
who cares that your think .
why you should do that !
/**
* 递归某个文件夹下的所有文件13
*
@author
Administrator
*
*/
package
mylove;
import
java.io.*;
/**
* 递归某个文件夹下的所有文件
*
@author
Administrator
*
*/
public
class
DiguiFile
{
public
static
void
main(String args[])
{
File my =
new
File(
"E:\\smole"
);
show(my);
}
public
static
void
show(File my)
{
if
(my !=
null
)
if
(my.isDirectory())
{
File f[] = my.listFiles();
if
(f !=
null
)
{
for
(
int
i = 0; i < f.
length
; i++)
show(f[i]);
}
}
else
{
System.
out
. println(my);
}
}
}
I get pocket for the sunshine .
/**
* 数123,数3的学生退出,最后剩下的是谁14
*
@param
args
*/
import
java.util.*;
public
class
TheLastPerson
{
/**
* 数123,数3的学生退出,最后剩下的是谁
*
@param
args
*/
public
static
void
main(String args[])
{
System.
out
.println(
"数123,数3的学生退出,最后剩下的是谁?"
);
System.
out
.println(
"请输入学生个数:"
);
Scanner input =
new
Scanner(System.
in
);
int
num = input.nextInt();
//13个人都没报数
boolean
bl[] =
new
boolean
[num];
for
(
int
i = 0; i < bl.
length
; i++){
bl[i] =
true
;
}
int
index = 0;
//剩下的人
int
lastnum = num;
//剩下的人
int
countNum = 0;
//报数
while
(lastnum > 1)
{
if
(bl[index] && ++countNum == 3)
//如果留下来,且
{
countNum = 0;
//数到3归零从新报数
bl[index] =
false
;
//标记那个位置为假的,报过数退出的
lastnum--;
//退出一个数量减1
}
if
(++index == num)
//索引报数到最后这个人的 时候就从头报数,索引回到0
index = 0;
}
for
(
int
i = 0; i < num; i++){
if
(bl[i])
System.
out
.println((
new
StringBuilder(
"留下来的是第"
)).append(i + 1).append(
"个同学"
).toString());
}
}
}
//删除重复的单词。15
public
class
removeWord {
/**
* 删除字符串中出现的字符
*
@param
args
*/
public
static
void
main(String[] args) {
System.
out
.println(
"请输入一串字符"
);
Scanner input =
new
Scanner(System.
in
);
String str=input.nextLine();
System.
out
.println(
"请输入要删除的字符"
);
String delStr=input.nextLine();
StringBuffer sb=
new
StringBuffer();
sb.append(str);
for
(
int
i=0;i<sb.length();i++){
String s=
""
+sb.charAt(i);
if
(s.trim().equals(delStr)){
sb=sb. deleteCharAt(i);
// 删除字符
i--;
}
}
System.
out
.println(
"最后输出结果为:"
+sb);
}
}
//颠倒字符串数组16
public
class
reverseString {
/**
* 旋转字符串 如: I am a student
* 结果为: student a am I
*
@param
arr
*/
public
static
void
reverse(String arr[])
{
int
start = 0;
for
(
int
end = arr.
length
- 1; start < end ; end--)
{
String temp = arr[start];
arr[start] = arr[ end];
arr[ end] = temp;
start++;
}
System.
out
.println(
"\n颠倒后的结果为:"
);
for
(
int
i = 0; i < arr.
length
; i++)
System.
out
.print((
new
StringBuilder(String.valueOf(arr[i]))).append(
" "
).toString());
}
public
static
void
main(String[] args) {
System.
out
.println(
"请输入一串字符"
);
Scanner input=
new
Scanner(System.
in
);
String str=input.nextLine();
String arr[]=str.split(
" "
);
reverse(arr);
}
}
/**
* 输入一个日期 ,判断是星期几17
*
@author
小高
*
@date
2013
-
08
-
24
*
*/
public
class
date2 {
public
static
void
main(String[] args) {
System.
out
.println(
"要想知道是星期几?那请输入一个日期:"
);
Scanner input=
new
Scanner(System.
in
);
String strDate=input.nextLine();
String strArray[]=strDate.split(
"-"
);
int
year=Integer.parseInt(strArray[0]);
//年
int
month=Integer.parseInt(strArray[1]);
//月
int
day=Integer.parseInt(strArray[2]);
//日
//思路: 1900年1月1日是星期一
//到现在的总年数天数
int
sumYearDays=0;
for
(
int
y=1900;y<year;y++){
if
(y%400==0||(y%4==0&&y%100!=0)){
//闰年
sumYearDays+=366;
}
else
sumYearDays+=365;
}
//当前年份的 月份、日期 的 总天数
int
monthArr[]={31,28,31,30,31,30,31,31,30,31,30,31};
//12个月的天数
if
(year%400==0||(year%4==0&&year%100!=0)){
//闰年
monthArr[1]=29;
}
//如输入1月3号 或 2月3号 3月3号
int
sumMonthAndDays=day;
if
(month!=1){
for
(
int
i=0;i<month-1;i++){
sumMonthAndDays+=monthArr[i];
}
}
else
{
sumMonthAndDays=day;
}
int
sumDays= sumYearDays+sumMonthAndDays;
//总天数
//求星期几 余数
int
week=sumDays%7;
String strWeek=
"日一二三四五六"
;
System.
out
.println(
"你好,"
+strDate+
"是: 星期"
+strWeek.charAt(week));
}
}
/**
* 统计同成绩的人数18
*/
public
class
SameSore {
/**
* 统计同成绩的人数
*
*
@param
args
*/
public
static
void
main(String[] args) {
int
arr[] = { 65, 70, 80, 50, 70, 80 };
Arrays. sort(arr);
// 升序排列
int
len = arr.
length
- 1;
System.
out
.println(
"请输入前多少名?"
);
Scanner input=
new
Scanner(System.
in
);
int
num=input.nextInt();
int
count[] =
new
int
[num+1];
//由于第1个不取,所以长度比实际的加1 ,存储第多少名的 数据
int
score[]=
new
int
[len+1];
//成绩
//第几名 多少个同学
for
(
int
k = 1; k <= count.
length
-1; k++) {
int
hasCount=0;
for
(
int
j=1;j<=k;j++){
hasCount+=count[j];
//已经统计的个数
}
for
(
int
i = len - hasCount; i >= 0; i--) {
//统计第几名重复了几个
if
(arr[i] == arr[len - hasCount]) {
score[k]=arr[len - hasCount];
//第几名的分数
count[k]++;
//第几名的个数
}
}
}
// 结果:
for
(
int
i = 1; i <count.
length
; i++) {
try
{
System.
out
.println(
"第"
+ i +
"名,成绩为"
+score[i]+
":"
+ count[i]);
}
catch
(ArrayIndexOutOfBoundsException e){
System.
out
.println(
"第"
+i+
"名超出范围哦!"
);
}
}
}
//推断过程: // 第一名找出后 ,统计和第一名相等的分数有几个
/**
* 最大公约数算法:给两个数,如果两个数相等,最大公约数是其本身;如果不等,取两个数相减的绝对值和两个数中最小的数比较,
* 相等则为最大公约,不等则继续上面的算法,直到相等 19
*
@author
Administrator
*/
public class BigYueShu {
public
static
void
main(String[] args) {
yueshu(16,12);
}
public
static
void
yueshu(
int
num1,
int
num2){
if(num1==num2){
System. out.println(num2);
}
else
yueshu(Math.abs (num1-num2),Math.min(num1,num2));
}
}
20
package AC;
import java.util.ArrayList;
import java.util.List;
public class Abc {
public static void main(String[] args) {
String word="abc";
Abc one=new Abc();
List<String> list=new ArrayList<String>();
one.method1(word, "", list);
System.out.println(list.toString());
}
//先删除第0,1,2个 ,有 bc ac ab 三种组合方法
public void method1(String base,String buff,List<String> result) {
if(base.length()<=0)
{result.add(buff); }
for (int i = 0; i < base.length(); i++) {
method1(new StringBuilder(base).deleteCharAt(i).toString(),buff+base.charAt(i),result);
}
//第一次删除a,留下bc, a先存起来,接着把bc删除第一个 , 留下c,ab存起来,接着删除c,留下空。然后 abc存起来,放到集合中。
}
}
import java.util.ArrayList;
import java.util.List;
public class Abc {
public static void main(String[] args) {
String word="abc";
Abc one=new Abc();
List<String> list=new ArrayList<String>();
one.method1(word, "", list);
System.out.println(list.toString());
}
//先删除第0,1,2个 ,有 bc ac ab 三种组合方法
public void method1(String base,String buff,List<String> result) {
if(base.length()<=0)
{result.add(buff); }
for (int i = 0; i < base.length(); i++) {
method1(new StringBuilder(base).deleteCharAt(i).toString(),buff+base.charAt(i),result);
}
//第一次删除a,留下bc, a先存起来,接着把bc删除第一个 , 留下c,ab存起来,接着删除c,留下空。然后 abc存起来,放到集合中。
}
}