1. 程式人生 > >牛客網刷的程式設計題((1——5)/69)

牛客網刷的程式設計題((1——5)/69)

1、小明同學學習了不同的進位制之後,拿起了一些數字做起了遊戲。小明同學知道,在日常生活中我們最常用的是十進位制數,而在計算機中,二進位制數也很常用。現在對於一個數字x,小明同學定義出了兩個函式f(x)和g(x)。
f(x)表示把x這個數用十進位制寫出後各個數位上的數字之和。如f(123)=1+2+3=6。
g(x)表示把x這個數用二進位制寫出後各個數位上的數字之和。如123的二進位制表示為1111011,那麼,g(123)=1+1+1+1+0+1+1=6。
小明同學發現對於一些正整數x滿足f(x)=g(x),他把這種數稱為幸運數,現在他想知道,大於0且小於等於n的幸運數有多少個?
輸入描述:
每組資料輸入一個數n(n<=100000)
輸出描述:
每組資料輸出一行,小於等於n的幸運數個數。
示例1
輸入
21
輸出
3

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        fun(n);
    }
    
    public static int fx(int n) {
        int count = 0;
        do {
            count = count+(n % 10);
            n = n/10;
        }while(n > 0);
       return count;
    }
    
    public static int gx(int n) {
        int count = 0;
        while(n > 0) {
            n &= (n-1);
            count++;
        }
        return count;
    }
    
    public static void fun(int n) {
        int count = 0;
        for(int i = 1;i <= n;i++) {
            if(fx(i) == gx(i)) {
                count++;
            }
        }
        System.out.println(count);
    }
}

2、題目描述:春天是鮮花的季節,水仙花就是其中最迷人的代表,數學上有個水仙花數,他是這樣定義的: “水仙花數”是指一個三位數,它的各位數字的立方和等於其本身,比如:153=13+53+3^3。 現在要求輸出所有在m和n範圍內的水仙花數。
輸入描述:
輸入資料有多組,每組佔一行,包括兩個整數m和n(100 ≤ m ≤ n ≤ 999)。

輸出描述:
對於每個測試例項,要求輸出所有在給定範圍內的水仙花數,就是說,輸出的水仙花數必須大於等於m,並且小於等於n,如果有多個,則要求從小到大排列在一行內輸出,之間用一個空格隔開;
如果給定的範圍內不存在水仙花數,則輸出no; 每個測試例項的輸出佔一行。
示例1 輸入

100 120
300 380
輸出

no
370 371

import java.util.Scanner;
import static java.lang.StrictMath.pow;
public class Main {
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        int m = scan.nextInt();
        int n = scan.nextInt();
        fun(m, n);
        
    }
    
   

    public static void fun(int m,int n) {
        int count = 0;
        for(int i = m;i <= n;i++) {
            if(show(i)) {
                if(count == 0) {
                     count++;
                     System.out.print(i);
                }else {
                    count++;
                    System.out.print(" "+i);
                }
               
            }
        }
  
        if(count == 0) {
            System.out.print("no");
        }
        System.out.println();
       
    }

    public static boolean show(int x) {
        int tmp = 0;
        int cot = x;
        while(x != 0) {
            tmp =  tmp + (int)pow(x%10,3);
            x = x/10;
        }
        if(cot == tmp) {
            return true;
        }
        return false;
    }
}

3、題目描述
數列的第一項為n,以後各項為前一項的平方根,求數列的前m項的和。
輸入描述:
輸入資料有多組,每組佔一行,由兩個整數n(n < 10000)和m(m < 1000)組成,n和m的含義如前所述。
輸出描述:
對於每組輸入資料,輸出該數列的和,每個測試例項佔一行,要求精度保留2位小數。
示例1 輸入
81 4
2 2
輸出
94.73
3.41

import java.text.DecimalFormat;
import java.util.Scanner;

import static java.lang.StrictMath.sqrt;
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int m = s.nextInt();
        double tmp = n;
        double count = n;
        while(m != 1) {
            tmp = sqrt(tmp);
            count = count+tmp;
            m--;
        }
        System.out.println(new DecimalFormat("#.00").format(count));
        
        }
 }

4、題目描述
一隻袋鼠要從河這邊跳到河對岸,河很寬,但是河中間打了很多樁子,每隔一米就有一個,每個樁子上都有一個彈簧,袋鼠跳到彈簧上就可以跳的更遠。每個彈簧力量不同,用一個數字代表它的力量,如果彈簧力量為5,就代表袋鼠下一跳最多能夠跳5米,如果為0,就會陷進去無法繼續跳躍。河流一共N米寬,袋鼠初始位置就在第一個彈簧上面,要跳到最後一個彈簧之後就算過河了,給定每個彈簧的力量,求袋鼠最少需要多少跳能夠到達對岸。如果無法到達輸出-1
輸入描述:
輸入分兩行,第一行是陣列長度N (1 ≤ N ≤ 10000),第二行是每一項的值,用空格分隔。
輸出描述:
輸出最少的跳數,無法到達輸出-1
示例1
輸入
5
2 0 1 1 1
輸出
4

這題恐怕就我解得最麻煩,動態規劃沒搞出來,所以拿類和物件寫的,回頭學了演算法再來優化它吧;

import java.util.Scanner;
import java.util.Arrays;
class Stack {
    int[] elem;
    int top;

    public Stack(int n) {
        this.elem = new int[n];
        this.top = 0;
    }

    public void push(int val) {
        if (top == elem.length) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
        this.elem[this.top++] = val;
    }

    public int getTop() {
        if(top == 0) {
            return -1;
        }
        this.top--;
        return elem[this.top];
    }

}

class ArrayNode {
    int value;
    boolean state;

    public ArrayNode(int value) {
        this.value = value;
        this.state = true;
    }
}


public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();

        ArrayNode[] array = new ArrayNode[n];
        for (int i = 0; i < n; i++) {
            int value = s.nextInt();
            array[i] = new ArrayNode(value);
        }
        System.out.println(show(array));
    }

    public static int show(ArrayNode[] array) {
        if(array[0].value == 0) {
            return -1;
        }
        Stack stack = new Stack(array.length);
        int count = 0;
        for(int i = 0;i < array.length;) {
            if(array[i].value == 0) {
                count--;
                i = stack.getTop();
                if(i == -1) {
                    return -1;
                }
                while(array[i].value == 1 && i != 0) {
                    count--;
                    i = stack.getTop();
                    if(i == -1) {
                        return -1;
                    }
                }
                if(array[i].value == 1) {
                    return -1;
                }else {
                    i = max(array,i,array[i].value+i);
                    count++;
                    continue;
                }
            }

            if(array[i].value >= array.length-i) {
                count++;
                return count;
            }else {
                count++;
                stack.push(i);
                i = max(array,i,i+array[i].value);
                //i = i+array[i].value;
                continue;
            }
        }
        return count;
    }



    public static int max(ArrayNode[] array,int start,int end){
        int max = array[end].value+end;
        int tmp = end;
        for(int i = start+1;i <= end;i++) {
            if((array[i].value+i) >= max && array[i].state == true) {
                max = array[i].value+i;
                tmp = i;
            }
        }
        array[tmp].state = false;
        return tmp;
    }
}

5、待續