码迷,mamicode.com
首页 > 其他好文 > 详细

有趣的数

时间:2014-09-07 17:19:55      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:c++

http://blog.csdn.net/pipisorry/article/details/39120489

有趣的数

时间限制:3000 ms  |           内存限制:65535 KB
 
问题描述:

把分数按下面的办法排成一个数表。

1/1 1/2 1/3 1/4.....

2/1 2/2 2/3....

3/1 3/2 ....

4/1..... .........

我们以z字型方法给上表的每项编号。

方法:第一项是1/1,然后是1/2、2/1、3/1、2/2、1/3、1/4、2/3……。

编程输入项号N(1<=N<=100000),输出表中第N项。

 
输入
第一行有一个整数m(0<m<=10),表示有m组测试数据; 随后有m行,每行有一个整数N;
输出
输出表中第N项
样例输入
4
3
14
7
12345
样例输出
2/1
2/4
1/4
59/99

code:
//***************************************************/
//*	有趣的数    nyoj 85	皮皮 2014-9-7	    */
//***************************************************/
#include <assert.h>
#include <stdio.h>
#include <math.h>

void funnyNum(){
	int n;
	scanf("%d", &n);

	int den = sqrt(float(n * 2));
	while(den*(den + 1) < n * 2)
		den++;
	int inc = n - den * (den - 1) / 2;
	if(den%2)
		printf("%d/%d\n", den + 1 - inc, inc);
	else
		printf("%d/%d\n", inc, den + 1 - inc);
}

int main(){
	assert( freopen("mathematic\\FunnyNum.in", "r", stdin) );
	int cases;
	scanf("%d", &cases);

	while(cases--){
		funnyNum();
	}

	fclose(stdin);
	return 0;
}
#include <iostream>
using namespace std;
int main()
{
    int m,d,s,g,i;cin>>m;
    while(m--)
    {
    cin>>d;s=g=0;
    for(i=1;i<d;i++)
    {
    s+=i;
    if(s>=d) break;
    g+=i;
    }
    if(i%2==0) cout<<d-g<<"/"<<i-(d-g-1)<<endl;
    else cout<<i-(d-g-1)<<"/"<<d-g<<endl;
    }
    return 0;
}
#include <stdio.h>
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int i,s,t;
        int n;
        scanf("%d",&n);
        for(i=1;i<=100000;i++)
        {
            if(i*(i+1)/2>=n)
            {t=i;break;}
        }
        s=(t*t-t)/2;
        if(t&1)
        printf("%d/%d\n",t+s+1-n,n-s);
        else
        printf("%d/%d\n",n-s,t+s+1-n);
    }
    return 0;
}

from:
ref:

有趣的数

标签:c++

原文地址:http://blog.csdn.net/pipisorry/article/details/39120489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!