1,由A-Z、a-z、0-9组成3位的字符密码,设计一个算法,列出并打印所有可能的密码组合。
由于控制台容纳能力有限,故将其写到文本文件中。
没人会耐心等你把这61个字母输完(程序执行ms,输入程序一分钟,自己手动输入四五分钟),这时用程序把内容生成,然后复制,,,
/*为什么输入程序这么多比手动输入'A','B','C',…'a','b','c'...'1','2,'3'...'8','9'快
*因为需要输入不同的字母,数字,引号括住,还得逗号隔开。
*但是程序都是生成的,输入程序只要半分钟,最多一分钟,执行时间几乎能忽略
*/
public class ZiFuChuanShengCheng{//这代码程序生成,为方便,可简化为public class A
public static void main(String[] args){//自动生成
for(int a='A';a<='Z';a++){//for循环自动生成轮廓,填充即可
System.out.print("'"+(char)a+"',");
}
/*下面两个循环由第一个按住Ctrl+ALT+↓下箭头得到*/
for(int a='a';a<='z';a++){
System.out.print("'"+(char)a+"',");
}
for(int a='1';a<='9';a++){
System.out.print("'"+(char)a+"',");
}
System.out.println();
}
}
得到:'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y',
'z','1','2','3','4','5','6','7','8','9',
java实现:
package com.starain.mimabianli;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class BianLiMiMa {
public static void main(String[] args) {
try {
long time1 = System.currentTimeMillis();
int count = 1;
char mima[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'1','2','3','4','5','6','7','8','9'};
File file1 = new File("D:/mima.txt");
BufferedWriter bfile1 = new BufferedWriter(new FileWriter(file1));
for(int i = 0;i <61;i++){
for(int j=0;j<61;j++){
for(int k = 0; k <61;k++){
count++;
bfile1.write(mima[i]);
bfile1.write(mima[j]);
bfile1.write(mima[k]);
bfile1.newLine();
}
}
}
long time2 = System.currentTimeMillis();
System.out.println("用时:"+(time2-time1)+"ms");
bfile1.close();
} catch (IOException e) {
System.out.println("error");
}
}
}
C实现
1 #include<stdio.h> 2 #include <time.h> 3 void main(){ 4 clock_t t1 , t2; 5 t1 = clock(); 6 FILE *file1 =fopen("D:/mima2.txt","w"); 7 char mima[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 8 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 9 'a','b','c','d','e','f','g','h','i','j','k','l','m', 10 'n','o','p','q','r','s','t','u','v','w','x','y','z', 11 '1','2','3','4','5','6','7','8','9'}; 12 for(int i = 0;i <61;i++){ 13 for(int j=0;j<61;j++){ 14 for(int k = 0; k <61;k++){ 15 fputc(mima[i],file1); 16 fputc(mima[j],file1); 17 fputc(mima[k],file1); 18 fputs("\n",file1); 19 } 20 } 21 } 22 t2 = clock(); 23 printf("用时%dms\n",(t2-t1)); 24 }
用时真的能可以忽略了。。。
2、实现字符串反转函数
String s = "abcdefghijklm";
C++
#include <iostream>
#include <string>
using namespace std;
void main(){
String s = "abcdefghijklm";
cout << s << endl;
int len = s.length();
char temp = 'a';
for(int i = 0; i < len/2; i++){
temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
cout << s;
}
java
package com.starain.mianshi;
public class FanZhuan {
public static void main(String[] args) {
String str1 = "abcdefghijklm";
String str2 = "";
for(int i=str1.length()-1;i>=0;i--){
str2+=str1.charAt(i);
}
System.out.println(str2);
}
}