zoukankan      html  css  js  c++  java
  • wikioi 1083 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,…

    从图中我们可以看出,如果一个数是第i行的第j个数(从右上到左下)那么该数可以表示为j/i-j+1

    因此求出第n个数的i和j就可以得出这个数

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main(){
        int n;
        cin>>n;
        int lineCount,index;
        if(n==1)cout<<"1/1"<<endl;
        else{
            lineCount=sqrt(n*2);
            if(lineCount*(lineCount+1)/2<n)lineCount++;
            if(lineCount%2==0){
                index=n-lineCount*(lineCount-1)/2-1;
            }
            else{
                index=lineCount-n+lineCount*(lineCount-1)/2;
            }
            int a=index+1,b=lineCount-index;
            cout<<a<<"/"<<b<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    RPC-Thrift(三)
    RPC-Thrift(二)
    RPC-Thrift(一)
    RPC-整体概念
    Java并发编程--ThreadPoolExecutor
    Java并发编程--Exchanger
    编译libjpeg库
    树莓派3B+ wifi 5G连接
    手动安装 pygame
    摘记 pyinstaller 使用自定义 spec
  • 原文地址:https://www.cnblogs.com/superzrx/p/3510887.html
Copyright © 2011-2022 走看看