A - Count on Canton

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

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.

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

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

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

Output

You are to write a program that will read a list of numbers in the
range from 1 to 10^7 and will print for each number the corresponding
term in Cantor's enumeration as given below.

Sample Input

3
14
7

Sample Output

TERM 3 IS 2/1
TERM 14 IS 2/4
TERM 7 IS 1/4 代码:
#include <stdio.h>
#include <string.h> int main()
{
    int n;
    int i, j;
    int dd, ff;
    int d, c;     while(scanf("%d", &n)!=EOF )
    {
        d = 1;
        while(( (1+d)*d)/2 < n)
        {
            d++;
        }
        c = d-1;
        if(d%2==1)//奇数列
        {
            dd = d-( n-(1+c)*c/2 )+1 ;
            ff = n-(1+c)*c/2;
        }
        else if(d%2==0)//偶数列
        {
            dd = n-(1+c)*c/2 ;
            ff = d-( n-(1+c)*c/2 ) +1;
        }
        printf("TERM %d IS %d/%d\n", n, dd, ff );
    }
    return 0;
}

算法思考: poj 1969 Count on Canton的更多相关文章

  1. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

  2. 多维算法思考(三):AB组合问题

    多维算法思考(三):AB组合问题 题目:x个A,y个B可以组合成多少个不同排列的问题. 首先,我们用数学的方式思考,这个问题属于<组合数学>的问题,我们的第一种方法可以用组合思路来求解. ...

  3. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

  4. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  5. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

  6. poj 2777 Count Color

    题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...

  7. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  8. 分布式唯一id:snowflake算法思考

    匠心零度 转载请注明原创出处,谢谢! 缘起 为什么会突然谈到分布式唯一id呢?原因是最近在准备使用RocketMQ,看看官网介绍: 一句话,消息可能会重复,所以消费端需要做幂等.为什么消息会重复后续R ...

  9. LeetCode算法题-Subdomain Visit Count(Java实现)

    这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...

随机推荐

  1. PHP面试题及答案解析(8)—PHP综合应用题

    1.写出下列服务的用途和默认端口. ftp.ssh.http.telnet.https ftp:File Transfer Protocol,文件传输协议,是应用层的协议,它基于传输层,为用户服务,它 ...

  2. js项目第一课:获取节点的方法有三个

    第一种方法: demo.html代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  3. 匿名函数块级作用域以及在JQuery中的应用

    最近经常回在群里面看到有些朋友问如下这样的js写法该如何理解的的问题,虽然可能对匿名函数有些理解,但是有时候看到JQuery源码或者其他JS库中同样的写法时,就有点不理解了,今天再次分享下这方面的知识 ...

  4. Active Directory的基本概念

    前言 本文是面对准备加入Active Directory编程的初学者的一份文章,主要是讲解Active Directory(活动目录)的一些概念和相关知识.这篇文章本来是不想写下来的,因为概念性内容的 ...

  5. MyBatis_传入参数的问题

    一.单个参数 1.基本数据类型 (1)直接使用 List<ChargeRuleDO> tests(long id); <select id="tests" res ...

  6. centos中git使用

    先用root用户登录, yum install git 进行安装,然后退出用普通用户登录. ssh-keygen -t rsa -C "tuhooo@163.com" 登录GitH ...

  7. yaffs2在am335x上实施

    一.yaffs2文件烧写(通过网口) uboot下输入如下命令: setenv ipaddr 192.168.2.110;setenv serverip 192.168.2.222;saveenv n ...

  8. python入门课程 第二章 安装Python

    2-1 选择python版本首先python2.7和python3是不可以通用的目前丰富的类库都支持python2.7,所以选用Python2.7    选择python2.7版本2-2 window ...

  9. MySql存储过程及MySql常用流程控制语法

    /* 该代码是创建了一个名叫"p4"的存储过程并设置了s1,s2,s3两个int型一个varchar型参数,还可以是其他数据类型,内部创建了x1,x2两个变量 DELIMITER是 ...

  10. nginx中使用waf防火墙

    1.安装依赖 yum install -y readline-devel ncurses-devel 2.安装Lua # .tar.gz # cd lua- # make linux # make i ...