zoukankan      html  css  js  c++  java
  • hdu 4002 Find the maximum

    Problem - 4002

      不知道思路了。。居然是大一上学期的时候做的题,今天找回来才发现是区域赛的题。为什么那时候就写这么恶心的代码?_(:з」∠)_ 好像很厉害的样子。。

      写一下那时候我的记录下来的思路,不知道对不对的了。。囧:

    1.筛素数70个左右

    2.求素数的乘积,要用到高精度乘法

    3.把70个积都存下来

    4.读入字符串,然后比较大数,超过已存素数就输出

      大概就这样子。

    代码如下:

      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<stdlib.h>
      4 #include<math.h>
      5 
      6 int prime[101];
      7 char *product[101];
      8 
      9 void produce_primes(void)
     10 {
     11     int i, j;
     12     prime[1]=2;
     13     prime[0]=1;
     14     i=3;
     15     while(prime[0] < 70)
     16     {
     17         for(j=1; j <= prime[0]; j++)
     18             if(!( i%prime[j] ))break;
     19         if( j > prime[0] )
     20         {
     21             prime[0]++;
     22             prime[prime[0]]=i;
     23         }
     24         i++;
     25     }
     26 }
     27 
     28 char *multiply( char *a, char *b )
     29 {
     30     int i, j, k;
     31     int len_a=strlen(a);
     32     int len_b=strlen(b);
     33     int *i_product=(int *)malloc((len_a+len_b)*sizeof(int));
     34     char *c_product=(char *)malloc((len_a+len_b+1)*sizeof(char));
     35 
     36     memset(i_product, 0, (len_a+len_b)*sizeof(int));
     37     memset(c_product, 0, (len_a+len_b)*sizeof(char));
     38     c_product[len_a+len_b]='';
     39 
     40     for( i=len_a-1; i >= 0; i-- )
     41         for( j=len_b-1; j >= 0; j-- )
     42         i_product[i+j+1]+=(a[i]-'0')*(b[j]-'0');
     43     for( i=len_a+len_b-1; i >= 1; i-- )
     44     {
     45         i_product[i-1]+=i_product[i]/10;
     46         c_product[i]=i_product[i]%10+'0';
     47     }
     48 
     49     c_product[0]=i_product[0]+'0';
     50     i=0;
     51     while(c_product[i] == '0' && i<len_a+len_b-1)i++;
     52 
     53     return &c_product[i];
     54 }
     55 
     56 char *num_to_ch(int a)
     57 {
     58     char *res=(char *)malloc(11*sizeof(char));
     59     int i;
     60 
     61     memset(res, 0, 11*sizeof(char));
     62     res[10]='';
     63     res[9]='0';
     64     i=9;
     65     while(a)
     66     {
     67         res[i]= a%10 + '0';
     68         i--;
     69         a/=10;
     70     }
     71     i=0;
     72     while(res[i] == 0)i++;
     73 
     74     return &res[i];
     75 }
     76 
     77 int cmp(char *a,char *b)
     78 {
     79     if(strlen(a) != strlen(b))
     80         if(strlen(a) > strlen(b))return 1;
     81         else return -1;
     82     else return strcmp(a,b);
     83 }
     84 
     85 int main()
     86 {
     87     int i, n;
     88     char in[105];
     89     produce_primes();
     90     i=0;
     91     product[0]=num_to_ch(prime[1]);
     92 
     93     while(strlen(product[i]) <= 100)
     94     {
     95         i++;
     96         product[i]=multiply(product[i-1], num_to_ch(prime[i+1]));
     97     }
     98 
     99     scanf("%d", &n);
    100     while(n--)
    101     {
    102         i=0;
    103 
    104         scanf( "%s", in );
    105         while(cmp(product[i],in) <= 0)i++;
    106 
    107         printf("%s
    ", product[i-1]);
    108     }
    109 
    110     return 0;
    111 }
    View Code

    ——written by Lyon

  • 相关阅读:
    广搜 BFS()
    最短路-A
    DFS-C
    codeforces contest
    小技巧
    将博客搬至CSDN
    建树
    codeforces gym102411 Equidistant(图论+乱搞)
    codeforces 1250N wires(简单图论)
    Splay 树
  • 原文地址:https://www.cnblogs.com/LyonLys/p/hdu_4002_Lyon.html
Copyright © 2011-2022 走看看