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. Java编程手冊-Collection框架(上)

    该文章所讲内容基本涵盖了Collection里面的全部东西,尽管基于jdk 1.5的.可是思路非常清晰 1.引言 1.1 Collection框架的介绍 尽管我们能够使用数组去存储具有同样类型的元素集 ...

  2. HDU-1165-Eddy&#39;s research II

    这个事实上是一个递归题.题目非常easy.m的数非常小.分三种情况.算一下.就能够直接把公式算出来. 当然,也能够用dp做: #include<iostream> #include< ...

  3. GDBus

    1. https://en.wikipedia.org/wiki/D-Bus In computing, D-Bus (for "Desktop Bus"[4]), a softw ...

  4. Android服务类Service具体解析

    Service有什么作用? 很多人不明确service是用来干嘛的.事实上Service作为Android四大组件之中的一个,能够理解为一个执行在后台的Activity.它适用于处理一些不干扰用户的长 ...

  5. css 控制文本显示两行 超过省略号显示

    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; word-break: break-all; ov ...

  6. Android使用ImageView显示网络图片

    本案例使用ImageView 简单的实现了网络图片的调用.当中注意事项.由于用到了网络,这里採用了HttpClient方法訪问网络联接,关于怎样使用,可參照文章 Android中使用HttpClien ...

  7. jfreechart时序图 demo

    import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.io.FileNotFoundExce ...

  8. 每天学习30分钟新知识之html教程1

    版本 年份 HTML 1991 HTML+ 1993 HTML 2.0 1995 HTML 3.2 1997 HTML 4.01 1999 XHTML 1.0 2000 HTML5 2012 XHTM ...

  9. linux 上拷贝文件到windows 上 文件出现锁的文件

    要在linux上拷贝出文件到windows上,那么文件必须是777的最高权限. chmod wb_redis -R

  10. wireshark:Couldn't run /usr/bin/dumpcap in child process: Permission denied

    When start wireshark, I met an error like: 引用 Couldn't run /usr/bin/dumpcap in child process: Permis ...