首页 > 其他分享 >poj-1930

poj-1930

时间:2023-05-23 16:04:18浏览次数:45  
标签:b% return gcd int 1930 poj str include


//144K  0MS C++
#include <cstdio>
#include <cstring>
#include <cmath>

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else if (a > b) {
        return gcd(b, a%b);
    } else {
        return gcd(a, b%a);
    }
}

// int gcd(int a,int b)  
// {  
//     if(!a)  
//         return b;  
//     return gcd(b%a,a);  
// }  

char str[100];

int main() {
    while(scanf("%s", str) != EOF) {
        if(!strcmp(str, "0")) {
            return 0;
        }

        int num,k,all,a,b,i,j,mina,minb,l;
        mina = minb = 1000000000;

        // get the number before ......
        for(i=2, all=0, l=0; str[i]!='.'; i++)  
        {  
            all = all*10 + str[i]-'0';  
            l++;  
        }

        for(num=all,k=1,i=1;i<=l;i++)  
        {  
            // printf("K\n");
            num/=10;  
            k*=10;  
            a=all-num;  
            b=(int)pow(10.0,l-i)*(k-1);  
            j=gcd(b,a);  
            if(b/j<minb)  
            {  
                mina=a/j;   minb=b/j;  
            }  
        }

        printf("%d/%d\n", mina, minb);
    }
}


比较trick的题,原理很简单, 要记住题目要求遍历所有可能的无限循环, 从中找到最小的除数:

比如 0.34.... , 无限循环的部分可能是 34 也可能是 3. 

要将这些情况都考虑一下,找到除数最小的,

记得求gcd(妈的 一开始还写错了。。。 太水了)

标签:b%,return,gcd,int,1930,poj,str,include
From: https://blog.51cto.com/u_9420214/6332968

相关文章

  • poj-1023
    //184K0MSC++#include<cstdio>#include<cstring>usingnamespacestd;charNP[65];//-1:n,1:pcharstr[80];chardigitUsed[80];charbinaryExpression[80];intcaseNum;intlength;longlongval;voidsolve(longlongval){l......
  • poj-1401
    //408K375MSG++#include<cstdio>#include<cstring>longlongget2FactorNum(longlongN){longlongres=0;while(N){res+=N/2;N/=2;}returnres;}longlongget5FactorNum(longlongN){long......
  • poj-2231
    //264K 47MS C++#include<cstdio>#include<cstring>#include<cstdlib>constintMAX=10005;longlongcowLocation[10005];intcmp(constvoid*a,constvoid*b){ return*((longlong*)a)-*((longlong*)b);}longlongcowNum;......
  • poj-1308
    //392K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=10000;intUF_set[MAX];voidUF_get_setId(intcurId){intparentId=UF_set[curId];if(parentId==0){return;}while(UF......
  • poj-1120
    //408K16MSG++#include<cstdio>#include<cstring>usingnamespacestd;intdish[20][20];intK[20][20];intD[16];intdays;intgetDensitySum(introwId,intcolumnId){intK=0;K+=dish[rowId][columnId];if(rowId......
  • poj-3641
    //712K0MSG++#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>usingnamespacestd;longlonga,p;//longlongpower2(longlonga,longlongn)//{//longlongret=1;//for(longlongm......
  • poj-1026
    //188K110MSC++#include<cstring>#include<cstdio>#include<iostream>usingnamespacestd;charstr1[205];charstr2[205];intkey[205];intcycleLength[205];//voidreplace(char*str,intkeyLength,intstrLength){//......
  • poj-2707
    //408K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;intoX;intoY;intdX;intdY;inlinedoubleMIN(doublea,doubleb){returna<b?a:b;}inlinedoubleMAX(doublea,doubleb){returna>b?......
  • poj-2635
    //1652K875MSG++1000//1648K1313MSG++10000#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>constintMAX=1000100;charnotPrime[MAX+1];intPrimeNum;intPrimes[MAX];voidcheckPrim......
  • poj-3286
    //stupidmethod!!!!!!!!!!!!!!!//388K360MSG++#include<stdio.h>#include<string.h>#include<math.h>intC[33][33];//C[n][m],choosemfromn;voidgetCombination(){for(intn=0;n<=32;n++){for(intm=......