• java实现 洛谷 P1014 Cantor表


    题目描述
    现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的:

    1/1 1/2 1/3 1/4 1/5 …

    2/1 2/2 2/3 2/4 …

    3/1 3/2 3/3 …

    4/1 4/2 …

    5/1 …

    … 我们以Z字形给上表的每一项编号。第一项是1/1,然后是1/2,2/1,3/1,2/2,…

    输入输出格式
    输入格式:
    整数N(1≤N≤10000000)

    输出格式:
    表中的第N项

    输入输出样例
    输入样例#1:
    7
    输出样例#1:
    1/4

    
    做树状数组WA到飞起,做一道找规律的水题调解心情……
    
    
    ```
    import java.util.Scanner;
     
    public class Main {
    	private static Scanner cin;
    	
    	public static void main(String args[]) throws Exception {
    		cin = new Scanner(System.in);
    		int n = cin.nextInt();
    		int count = 0;
    		for(int i=1;i<=n;i++) {
    			if(count<n &&(count+i)>=n) {
    				for(int j=1;j<=i;j++) {
    				 if((count+j) == n) {
    					 if(i%2==0) {
    						 System.out.println(String.format("%d/%d", j,i-j+1));
    						 return;
    					 }else {
    						 System.out.println(String.format("%d/%d",i-j+1, j));
    						 return;
    					 }
    				 }
    				}
    			}else {
    				count = count + i;
    			}
    		}
    	}
    }
    ```
    
    
  • 相关阅读:
    手动渗透测试漏洞
    博弈论一 [ 巴什游戏 ]
    [Error]EOL while scanning string literal
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947138.html
Copyright © 2020-2023  润新知