HDU 2522 A simple problem

A simple problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3368    Accepted Submission(s): 1249

Problem Description

Zty很痴迷数学问题.。一天,yifenfei出了个数学题想难倒他,让他回答1 / n。但Zty却回答不了^_^.  请大家编程帮助他.

Input

第一行整数T,表示测试组数。后面T行,每行一个整数 n (1<=|n|<=10^5).

Output

输出1/n. (是循环小数的,只输出第一个循环节).

Sample Input

4
2
3
7
168

Sample Output

0.5
0.3
0.142857
0.005952380

Author

yifenfei

Source

HDU 2008-10 Programming Contest

模拟除法的运算。

上代码

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
    int a[100005],t;  //a数组是用来判断循环小数的。
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        int n;
        int flag=0;
        scanf("%d",&n);
        if(n==1)
        {
            printf("1\n");  //n为一就是一
            continue;
        }
        if(n<0)
        {
            n=-n;   //小于0的话,先变正,做个标记
            flag=1;
        }
        int l=1;
        a[l]=1;
        if(flag)
            printf("-0.");  //输出符号
        else
            printf("0.");
        while(l)  //l除不尽时,即不为0
        {
            l*=10;   //开始模拟,先乘10
            printf("%d",l/n);  //输出第一位小数
            l%=n;  //取余,不断重复以上操作
            if(a[l])
                break;
            a[l]=l;  //让a[l]=l  ,出现相同的循环时,a[l]为真直接跳出了。保证只有一个循环节。
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-26 15:04:52

HDU 2522 A simple problem的相关文章

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

hdu 4143 A Simple Problem (变形)

题目 题意:给n,求x; 直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n; 令y-x = b, y+x = a; 枚举b, b 的范围肯定是sqrt(n),  y = (a+b)/2;  x = (a-b)/2; b越大, x越小, 所以倒着枚举b 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 using namesp

HDU 4143 A Simple Problem(数论-水题)

A Simple Problem Problem Description For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2. Input The first line is an integer T, which is the the number of cases. Then T line

hdu 4267 A Simple Problem with Integers(树形结构-线段树)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3708    Accepted Submission(s): 1139 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

HDU 4267 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5402    Accepted Submission(s): 1710 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

HDU 4267 A Simple Problem with Integers (树状数组)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given

HDU 4267 A Simple Problem with Integers(树状数组)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4191    Accepted Submission(s): 1309 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

HDU ACM 2522 A simple problem 模拟除法

分析:在除的过程中,当出现相同余数时即出现循环节. #include<iostream> using namespace std; bool h[100002]; void div(int x) { int t; memset(h,false,x*sizeof(h[0])+1); h[1]=true; t=1; while(t) { t=t*10; cout<<t/x; t=t%x; if(h[t]) //再次出现相同余数,表示出现循环节 break; h[t]=true; } }

【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状数组的单点查询:求某点a的值就是求数组中1~a的和. (i-a)%k==0把区间分隔开了,不能直接套用树状数组的区间修改单点查询 这道题的K很小,所以可以枚举k,对于每个k,建立k个树状数组,所以一共建立55棵树 所以就可以多建几棵树..然后就可以转换为成段更新了~~ [AC] 1 #include