1.顺时针打印矩阵
package com.tulun.src;
import java.util.Scanner;
public class Arr4 {
public static void main(String[] args){
/**
* 顺时针打印矩阵:
*/
int[][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int circle = arr[0].length/2;
int count = 0;
while(count<circle){
//上
for(int colum = count;colum < arr[0].length-count;colum++){
System.out.print(arr[count][colum]+" ");
}
//右
for(int row = count+1;row < arr.length-count;row++){
System.out.print(arr[row][arr[0].length-1-count]+" ");
}
//下
for(int colum = arr[0].length-1-1-count;colum > count;colum--){//三行四列
System.out.print(arr[arr.length-1-count][colum]+" ");
}
//左
for(int row = arr.length-1-1-count+1;row >= count+1.;row--){//三行四列
System.out.print(arr[row][count]+" ");
}
count++;
}
}
}
2.统计1~n中数字出现1的个数
package com.tulun.src;
import java.util.Scanner;
public class Arr4 {
/**
* 输入整数n,求1~n整数中数字1出现的次数
*/
public static int time(int n){
int count = 0;
for(int i=1;i<=n;i++){
int src = i;
while(src!=0) {
if (src % 10 == 1) {
count++;
}
src/=10;
}
}
return count;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = time(n);
System.out.println(count);
}
}
3.字符串转换整型 例如 123转换成123,sjdh— 2345dj5转换成23455
package com.tulun.src;
import java.util.Scanner;
public class Arr4 {
/**
* 字符串转换整型 例如 123转换成123,sjdh--- 2345dj5转换成23455
*/
public static int getCount(char[] ch){
int count = 0;
for(int i=0;i< ch.length;i++){
if(ch[i] >= '0' && ch[i]<='9'){
count++;
}
}
return count;
}
public static int trans(String str){
char[] ch = str.toCharArray();//'-' '1''2''3'
int count = getCount(ch);
int size = 0;
int[] arr = new int[count];//1 2 3
int flag = 1;//统计正负号
for(int i=0;i<ch.length;i++){
if(ch[i]>='0'&& ch[i]<='9') {
arr[size++] = ch[i] - '0';
}
if(ch[i] == '-'){
flag *=-1;
}
}//1 2 3 4
int result = 0;
int exp = count;
for(int i=0;i<count;i++){
result+=arr[i]*Math.pow(10,--exp);
}
result*=flag;
return result;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();//"123 456"
int result =trans(str);
System.out.println(result);
}
}