找规律题
现代数学的著名证明之一是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; }