CSP历年复赛题-P1014 [NOIP1999 普及组] Cantor 表

原题链接:https://www.luogu.com.cn/problem/P1014

题意解读:根据z字形遍历,求第n个数。

解题思路:

根据题意,遍历顺序如下图所示

观察得知,第i层的x/y的x+y = i + 1,并且

如果i是偶数,x从1开始枚举;如果i是奇数,x从i开始枚举

100分代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int i = 1;
    int cnt = 0;
    while(true)
    {
        if(i % 2 == 0)  //偶数从1/i开始枚举
        {
            for(int x = 1; x <= i; x++)
            {
                if(++cnt == n)
                {
                    cout << x << "/" << i + 1 - x;
                    return 0;
                }
            }
        }
        else //奇数从i/1开始枚举
        {
            for(int x = i; x >= 1; x--)
            {
                if(++cnt == n)
                {
                    cout << x << "/" << i + 1 - x;
                    return 0;
                }
            }
        }
        i++;
    }
}   

 

posted @   五月江城  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示