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. [分享]windows下编译squid的经验(转)

    squid是什么我这里就不说了,这不是本文的重点,总之它是一个集:代理.加速.缓存.负载均衡.防盗链.访问控制等多功能的一个超牛X开源软件,如今已经广泛应用于很多领域.对于缓存和加速这一领域,如今各大 ...

  2. Lua学习十一----------Lua迭代器

    © 版权声明:本文为博主原创文章,转载请注明出处 Lua迭代器 - 迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址 - Lu ...

  3. tomcat 使用log4j进行日志切割

    因为tomcat catalina.out日志不会自己主动切割, 一.日志切割所需包在附近中 1. 压缩包中有三个jar包:     log4j-1.2.16.jar      tomcat-juli ...

  4. Linux下redis安装与使用 (转)

    尊重原创:https://www.cnblogs.com/codersay/p/4301677.html,并更正如下红字 redis官网地址:http://www.redis.io/ 最新版本:2.8 ...

  5. 用Android Studio 执行ndk 程序

    近期准备研究一下android双进程守护,因为此前用eclipse 写jni习惯了.如今主要用as 工具.在此也试着写个demo 然后在对双进程守护进行研究 1.所需工具 android studio ...

  6. O(n)求素数,求欧拉函数,求莫比乌斯函数,求对mod的逆元,各种求

    筛素数 void shai() { no[1]=true;no[0]=true; for(int i=2;i<=r;i++) { if(!no[i]) p[++p[0]]=i; int j=1, ...

  7. Codeforces Round #392 (Div. 2) F. Geometrical Progression

    原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 se ...

  8. python函数式编程-------python2.7教程学习【廖雪峰版】(五)

    2017年6月13日19:08:13 任务: 看完函数式编程 笔记: 该看:函数式编程1.函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解 ...

  9. [学英语]vocabulary.com你都可以怎么用

    vocabulary.com你都可以怎么用? 核心就是背单词. 1.利用纯英文环境熟悉单词,看看他们是如何解释单词的. 2.对于大段的文章,可以提炼出你需要背的单词. 3.以游戏的方式来背单词,app ...

  10. Spring MVC 返回Json IE出现下载

    今天在做一个利用IFrame提交进行form提交表单的时候发现返回的json在ie下竟然弹出了下载的提示, 于是就查看了返回的Content-type:appliation/json;charset= ...