zoukankan      html  css  js  c++  java
  • java小算法复习

    package com.bshinfo.bm.util;

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Scanner;

    public class Test {

    //菱形
    public void test1(){
    for (int i = 0; i < 5; i++) {//行数
    for (int j = 0; j <5-i-1; j++) {//空格
    System.out.print(" ");
    }
    for (int k = 0; k < 2*(i+1)-1; k++) {//星号
    System.out.print("*");
    }
    System.out.println("");
    }
    for (int i = 5; i > 0; i--) {//行数
    for (int j =5-i+1; j >0; j--) {//空格
    System.out.print(" ");
    }
    for (int k = 2*(i-1)-1; k >0; k--) {//星号
    System.out.print("*");
    }
    System.out.println("");
    }
    }
    //菱形
    public void test2(int n){
    for(int i = 0; i < n - 1; i++){
    for(int x = i + 1; x < n; x++){
    System.out.print(" ");
    }
    for(int y = 0; y < (i + 1) * 2 - 1; y++){
    System.out.print("*");
    }
    System.out.println();
    }
    for(int i = 0; i < n; i++){
    for(int x = 0; x < i; x++){
    System.out.print(" ");
    }
    for(int y = i; y < 2 * n - i - 1; y++){
    System.out.print("*");
    }
    System.out.println();
    }
    }

    //三角形
    public void test3(){
    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5-i-1; j++) {
    System.out.print(" ");
    }
    for (int j = 0; j < i+1; j++) {
    System.out.print("*");
    }
    System.out.println("");
    }
    }

    //九九乘法表
    public void test4(){

    for (int i = 1; i < 10; i++) {
    for (int j = 1; j < i+1; j++) {
    System.out.print(j+"*"+i+"="+(i*j)+" ");
    }
    System.out.println("");
    }
    }
    //统计字符串中字符出现的个数
    public void test5(){
    String str = "asdfsadfewrtewfxzfagres";
    Map<String,Integer> map =new HashMap<String,Integer>();
    int count = str.length();
    for (int i = 0; i < count; i++) {
    if(map.containsKey(str.substring(i, i+1))){
    int temp = map.get(str.substring(i, i+1));
    temp++;
    map.put(str.substring(i, i+1), temp);
    }else{
    map.put(str.substring(i, i+1),1);
    }
    }
    for (String key:map.keySet()) {
    int value = map.get(key);
    System.out.println("字符 "+key+" 出现的次数是"+value);
    }
    }
    //判断是否输入的是水仙花数
    public void test6(){
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    int count = str.length();
    if(count%2!=0){
    int temp = 0;
    for (int i = 0; i < count/2; i++) {
    int temp1 = Integer.parseInt(str.substring(i, i+1));
    int temp2 = Integer.parseInt(str.substring(count-i-1, count-i));
    if(temp1!=temp2){
    System.out.println(str +" 不是水仙花数!");
    return;
    }else{
    temp++;
    }
    }
    if(temp == (count/2)){
    System.out.println(str +" 是水仙花数!");
    }
    }

    }
    public static void main(String[] args) {

    Test test = new Test();
    test.test1();
    test.test2(5);
    test.test3();
    test.test4();
    test.test5();
    test.test6();
    }

    }

  • 相关阅读:
    2-sat问题,输出方案,几种方法(赵爽的论文染色解法+其完全改进版)浅析 / POJ3683
    hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。
    最小费用最大流粗解 poj2516
    hdu3078 建层次树+在线LCA算法+排序
    hdu 3594 Cactus /uva 10510 仙人掌图判定
    有向图最小路径覆盖方法浅析、证明 //hdu 3861
    hdu 1827 有向图缩点看度数
    条件转化,2-sat BZOJ 1997
    2-sat基础题 BZOJ 1823
    2-sat 分类讨论 UVALIVE 3713
  • 原文地址:https://www.cnblogs.com/mybug/p/7053562.html
Copyright © 2011-2022 走看看