| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2667 人关注过本帖
标题:c语言写的完整程序看不懂请老师逐行解释一下,谢谢!
只看楼主 加入收藏
ysr2857
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
回复 9楼 wmf2014
又在网上搜索了一个程序,请您再看看是否完整?分两种,递归型:(105行,黏贴就乱了,整理一下)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <conio.h>
#define N 150010const double pi = 3.141592653;
char s1[N>>1], s2[N>>1];
double rea[N], ina[N], reb[N], inb[N], Retmp[N], Intmp[N];
int ans[N>>1];
void FFT(double *reA, double *inA, int n, int flag)
{
    if(n == 1) return;    int k, u, i;
    double reWm = cos(2*pi/n), inWm = sin(2*pi/n);
    if(flag) inWm = -inWm;
    double reW = 1.0, inW = 0.0;
    /* 把下标为偶数的值按顺序放前面,下标为奇数的值按顺序放后面 */
    for(k = 1,u = 0; k < n; k += 2,u++)
{
        Retmp[u] = reA[k];
        Intmp[u] = inA[k];
    }
    for(k = 2; k < n; k += 2)
{
        reA[k/2] = reA[k];
        inA[k/2] = inA[k];
    }
    for(k = u,i = 0;
 k < n && i < u; k++, i++)
{
        reA[k] = Retmp[i];
        inA[k] = Intmp[i];
    }
    /* 递归处理 */
    FFT(reA, inA, n/2, flag);
    FFT(reA + n/2, inA + n/2, n/2, flag);
    for(k = 0; k < n/2; k++)
{
        int tag = k+n/2;
        double reT = reW * reA[tag] - inW * inA[tag];
        double inT = reW * inA[tag] + inW * reA[tag];
        double reU = reA[k], inU = inA[k];
        reA[k] = reU + reT;
        inA[k] = inU + inT;
        reA[tag] = reU - reT;
        inA[tag] = inU - inT;
        double rew_t = reW * reWm - inW * inWm;
         double inw_t = reW * inWm + inW * reWm;
         reW = rew_t;
        inW = inw_t;
    }
}
 int main()
{
#if 0
    freopen("in.txt","r",stdin);
#endif
    while(~scanf("%s%s", s1, s2))
{
        memset(ans, 0 , sizeof(ans));
        memset(rea, 0 , sizeof(rea));
        memset(ina, 0 , sizeof(ina));
        memset(reb, 0 , sizeof(reb));
        memset(inb, 0 , sizeof(inb));
        /* 计算长度为 2 的幂的长度len */
        int i, lent, len = 1, len1, len2;
        len1 = strlen(s1);
        len2 = strlen(s2);
        lent = (len1 > len2 ? len1 : len2);
        while(len < lent) len <<= 1;
        len <<= 1;
        /* 系数反转并添加 0 使长度凑成 2 的幂 */
        for(i = 0; i < len; i++)
{
            if(i < len1) rea[i] = (double)s1[len1-i-1] - '0';
            if(i < len2) reb[i] = (double)s2[len2-i-1] - '0';
            ina[i] = inb[i] = 0.0;
        }
        /* 分别把向量 a, 和向量 b 的系数表示转化为点值表示 */
        FFT(rea, ina, len, 0);
        FFT(reb, inb, len, 0);
        /* 点值相乘得到向量 c 的点值表示 */
        for(i = 0; i < len; i++)
{
            double rec = rea[i] * reb[i] - ina[i] * inb[i];
            double inc = rea[i] * inb[i] + ina[i] * reb[i];
           rea[i] = rec; ina[i] = inc;
        }
        /* 向量 c 的点值表示转化为系数表示 */
        FFT(rea, ina, len, 1);
        for(i = 0; i < len; i++)
{
            rea[i] /= len;
            ina[i] /= len;
        }
        /* 进位 */
        for(i = 0; i < len; i++)
            ans[i] = (int)(rea[i] + 0.5);
        for(i = 0; i < len; i++)
{
            ans[i+1] += ans[i] / 10;
            ans[i] %= 10;
       }
        int len_ans = len1 + len2 + 2;
        while(ans[len_ans] == 0 && len_ans > 0) len_ans--;
        for(i = len_ans; i >= 0; i--)
            printf("%d", ans[i]);
        printf("\n");
    }
   return 0;
}
————————————————
版权声明:本文为CSDN博主「o-pqy-o」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.
2020-03-31 09:03
ysr2857
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
迭代型:(共119行,乱了,再整理一下)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <conio.h>
#define N 150010const double pi = 3.141592653;
char s1[N>>1], s2[N>>1];
double rea[N], ina[N], reb[N], inb[N];
int ans[N>>1];
 void Swap(double *x, double *y)
{
    double t = *x;
    *x = *y;
    *y = t;
}
 int Rev(int x, int len)
{
    int ans = 0;
    int i;
   for(i = 0;
 i < len; i++)
{
       ans <<= 1;
        ans |= (x & 1);
        x >>= 1;
    }
    return ans;
}
 void FFT(double *reA, double *inA, int n, bool flag)
{
   int s;
    double lgn = log((double)n) / log((double)2);
    int i;
    for(i = 0; i < n; i++)
{  
      int j = Rev(i, lgn);
        if(j > i)
{
            Swap(&reA[i], &reA[j]);
            Swap(&inA[i], &inA[j]);
        }
    }
   for(s = 1; s <= lgn; s++)
{   
     int m = (1<<s);
        double reWm = cos(2*pi/m), inWm = sin(2*pi/m);
        if(flag) inWm = -inWm;
        int k;
        for(k = 0; k < n; k += m)
{  
          double reW = 1.0, inW = 0.0;
            int j;
           for(j = 0; j < m / 2; j++)
{  
              int tag = k+j+m/2;
                double reT = reW * reA[tag] - inW * inA[tag];
                double inT = reW * inA[tag] + inW * reA[tag];
               double reU = reA[k+j], inU = inA[k+j];
                reA[k+j] = reU + reT;
                inA[k+j] = inU + inT;
                reA[tag] = reU - reT;
               inA[tag] = inU - inT;
               double rew_t = reW * reWm - inW * inWm;
                 double inw_t = reW * inWm + inW * reWm;
                reW = rew_t;
               inW = inw_t;
           }
        }
    }
    if(flag)
{
        for(i = 0;
 i < n; i++)
{
            reA[i] /= n;
            inA[i] /= n;
        }
   }
}
 int main()
{
#if 0
   freopen("in.txt","r",stdin);
#endif
   while(~scanf("%s%s", s1, s2))
{
       memset(ans, 0 , sizeof(ans));
        memset(rea, 0 , sizeof(rea));
        memset(ina, 0 , sizeof(ina));
        memset(reb, 0 , sizeof(reb));
        memset(inb, 0 , sizeof(inb));
        int i, lent, len = 1, len1, len2;
        len1 = strlen(s1);
        len2 = strlen(s2);
        lent = (len1 > len2 ? len1 : len2);
        while(len < lent) len <<= 1;
        len <<= 1;
        for(i = 0;
 i < len; i++)
{
           if(i < len1) rea[i] = (double)s1[len1-i-1] - '0';
            if(i < len2) reb[i] = (double)s2[len2-i-1] - '0';
            ina[i] = inb[i] = 0.0;
        }
        FFT(rea, ina, len, 0);
        FFT(reb, inb, len, 0);
        for(i = 0; i < len; i++)
{
           double rec = rea[i] * reb[i] - ina[i] * inb[i];
            double inc = rea[i] * inb[i] + ina[i] * reb[i];
            rea[i] = rec; ina[i] = inc;
        }
        FFT(rea, ina, len, 1);
        for(i = 0; i < len; i++)
           ans[i] = (int)(rea[i] + 0.4);
        for(i = 0; i < len; i++)
{
           ans[i+1] += ans[i] / 10;
            ans[i] %= 10;
        }
        int len_ans = len1 + len2 + 2;
        while(ans[len_ans] == 0 && len_ans > 0)
 len_ans--;
        for(i = len_ans; i >= 0; i--)
            printf("%d", ans[i]);
       printf("\n");
    }
    return 0;
}
————————————————
迭代型比递归型稍快一点。
2020-03-31 09:18
ysr2857
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:34
帖 子:809
专家分:77
注 册:2020-2-10
收藏
得分:0 
回复 9楼 wmf2014
接通知结贴,把积分送给你!
2020-04-01 08:12
快速回复:c语言写的完整程序看不懂请老师逐行解释一下,谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025299 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved