UVA - 264 - Count on Cantor (Cantor的数表!)

UVA - 264

Count on Cantor

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

 Count on Cantor 

One of the famous proofs of modern mathematics is Georg Cantor‘s demonstration that the set of rational numbers is enumerable. The proof works by using an explicit enumeration of rational numbers as shown in
the diagram below.

In the above diagram, the first term is 1/1, the second term is 1/2, the third term is 2/1, the fourth term is 3/1, the fifth term is 2/2, and so on.

Input and Output

You are to write a program that will read a list of numbers in the range from 1 to  and
will print for each number the corresponding term in Cantor‘s enumeration as given below. No blank line should appear after the last number.

The input list contains a single number per line and will be terminated by end-of-file.

Sample input

3
14
7

Sample output

TERM 3 IS 2/1
TERM 14 IS 2/4
TERM 7 IS 1/4

有木有很熟悉的感觉,LRJ在UVA上做的第一个题,\(^o^)/~

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	while(scanf("%d", &n) != EOF)
	{
		printf("TERM %d IS ", n);
		int x = 1;
		while(n>(x*(x+1)/2))
		{
			x++;
		}
		n -= (x*(x-1)/2);
		if(x&1)
		{
			printf("%d/%d\n", (x+1)-n, n);
		}
		else
		{
			printf("%d/%d\n", n, (x+1)-n);
		}
	}
	return 0;
} 
时间: 2024-10-14 04:53:31

UVA - 264 - Count on Cantor (Cantor的数表!)的相关文章

uva 10712 - Count the Numbers(数位dp)

题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a,b:问说在a到b之间有多少个n. 解题思路:数位dp,dp[i][j][x][y]表示第i位为j的时候,x是否前面是相等的,y是否已经出现过n.对于n=0的情况要特殊处理前导0,写的非常乱,搓死. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using na

UVa 11408 - Count DePrimes

题目:一个数的素因子的和如果也是素数就叫做DePrimes,统计给定区间内的DePrimes. 分析:数论.本题使用用一种素数的筛法,欧拉筛法,也加线性筛法. 这种方法,每次删选分两种情况:1.素因子不重复.2.素因子重复: 利用这个性质,统计DePrimes,如果素因子不同就加和,否则就去相应的最小不同集合: 打表计算,做差输出即可. 说明:线性筛法,比传统的筛法在数据量大时会快(⊙_⊙). #include <iostream> #include <cstdlib> #incl

UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)

题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的形状个数为:14 5个节点形成的二叉树的形状个数为:42 把n个节点对号入座有n!种情况 所以有n个节点的形成的二叉树的总数是:卡特兰数F[n]*n! 程序: 1 import java.math.BigInteger; 2 import java.util.Scanner; 3 public cl

UVA 1645 Count

https://vjudge.net/problem/UVA-1645 题意:有多少个n个节点的有根树,每个深度中所有节点的子节点数相同 dp[i] 节点数为i时的答案 除去根节点还有i-1个点,如果j是i-1的约数,说明能平均分成j棵子树 每棵子树有(i-1)/j个节点,所以可以递推 递推:子问题 #include<cstdio> using namespace std; const int mod=1e9+7; int dp[1001]; int main() { dp[1]=1; for

UVA - 1645 - Count(思路)

题意:输入n(1 <= n <= 1000),输出有n个结点且每个深度中所有结点的子节点数相同的树有多少种. 根据题意,其实要求每个子树都相同. 一个结点当作根节点,还剩下n - 1个结点,枚举n - 1的因子(因子当作紧邻根结点的子树中的结点数),然后将所有因子的答案相加即可. 代码如下: #include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include

UVa 1645 Count (递推,数论)

题意:给定一棵 n 个结点的有根树,使得每个深度中所有结点的子结点数相同.求多棵这样的树. 析:首先这棵树是有根的,那么肯定有一个根结点,然后剩下的再看能不能再分成深度相同的子树,也就是说是不是它的约数.那么答案就有了, 我们只要去计算n-1的约数有多少棵不同的树,然后就有递推式了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <str

UVa 10007 - Count the Trees

题目:统计n个节点的二叉树的个数. 分析:组合,计数,卡特兰数,大整数. n个节点的二叉树的形状有Cn个,求不同的树的个数,用卡特兰数乘以全排列n! 说明:打表计算,查询输出,提高效率. #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int C[305][2005] = {0}; int main() { C[1][0] = 1; for (int i = 2

UVa 1645 Count(**)

题目大意:输入n,统计有多少个n个结点的有根树,使得每个深度中所有结点的子结点数相同.结果模1000000007. 思路:根据题意,每个结点的每个子树都是相同的.所以n结果为n-1的所有约数的结果加起来. 示意图: 代码如下: 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #inclu

一个Android实数转化二进制的计算器

导言: 这是我自己写的第一个小应用,没有什么优化,没有追求图标好看,就是搜索来的代码堆砌,等将来学完设计模式还有重构的话可能还会来改. 我觉得这个里面最考验我的就是小数转化为二进制的问题了,当然整数转二进制很容易,不过因为浮点数在计算机中的储存方式并不是正常人类思维的方式,因此我费了很大的力气才尽可能的实现了这样的模块.希望看到的读者有好的方法或是我的代码逻辑有错误,请不吝赐教,先谢过. 开始,新建项目-B2D(起反了)....一路默认就是了 构建界面 步骤1-声明应用的字符串在strings.