POJ.grids.2980

题目链接:http://bailian.openjudge.cn/practice/2980

解题思路:先将对应位相乘的积累加,最后再来处理进位问题;如 835*49;

先做 835*9;

得到 i  2            1           0

72           27          45

再做 835*4

得到 i   3                   2                  1               0

32                  12                20             0

再把对应位上的累加起来

得到 i   3                   2                    1               0

3 2                84                   47            45

最后再考虑进位的问题

得到 i 4        3                   2                    1               0

4        0                  9                     1             5

这样就得到了835*49;

注意:一个数的第i位和另一个数的第j位相乘所得的数,一定要放在积的第i+j位上;

两个长度都为len的数相乘所得的积的位数不会超过2*len;

反思:还是不够熟练,好好学,改了好多次。

#include<stdio.h>
#include<string.h>
#define max 500
int main()
{
    int i,j;
    int len1,len2,len;
    int a[max],b[max],c[max];
    char str1[max],str2[max];

    while(~scanf("%s %s",&str1,&str2))
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        len1=strlen(str1);
        len2=strlen(str2);
        for(i=0;i<len1;i++)
        {
            a[i]=str1[len1-i-1]-‘0‘;
        }
        for(i=0;i<len2;i++)
        {
            b[i]=str2[len2-i-1]-‘0‘;
        }

        for(i=0;i<len2;i++)
        {
            for(j=0;j<len1;j++)
            c[i+j]+=b[i]*a[j];
        }
        len=i+j;

        for(i=0;i<len;i++)
        {
            if(c[i]>=10)
            {
                c[i+1]+=c[i]/10;
                c[i]=c[i]%10;
            }
        }

        for(i=len;(c[i]==0)&&(i>=0);i--);
            for(j=i;j>=0;j--)
            printf("%d",c[j]);
            printf("\n");

    }

}

  

时间: 2024-08-28 02:43:18

POJ.grids.2980的相关文章

Poj.Grids 2951 浮点数求高精度幂

2951:浮点数求高精度幂 总时间限制: 1000ms 内存限制: 65536kB 描述 有一个实数 R ( 0.0 < R < 99.999 ) ,要求写程序精确计算 R 的 n 次方.n 是整数并且 0 < n <= 25. 输入 T输入包括多组 R 和 n. R 的值占第 1 到 第 6 列,  n 的值占第 8 和第 9 列. 输出 对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方.输出需要去掉前导的 0 后后面不不要的 0 .如果输出是整数,不要输出小数点.

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

UVA 322 ships (POJ 1138)

题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=258 http://poj.org/problem?id=1138 题目描写叙述:  Ships  Probably everyone who ever attended school knows the game where two opposing players place

poj 2389.Bull Math 解题报告

题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次写 = =.......大整数加法写得比较多.百练也有一条是大整数乘法,链接如下:http://bailian.openjudge.cn/practice/2980/ 一步一步模拟即可,代码就是按这个来写的. 以 835 * 49 为例(亲爱的读者,允许我截图吧) 简直就是神奇呀----- 1 #i

poj 2446 Chessboard (二分图利用奇偶性匹配)

Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13176   Accepted: 4118 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2

poj 1704 Georgia and Bob(阶梯博弈)

Georgia and Bob Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8656   Accepted: 2751 Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2, 3, ...

Oil Deposits(poj 1526 DFS入门题)

http://poj.org/problem?id=1562 Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12595   Accepted: 6868 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp wor

POJ 3076 数独DLX

Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 4203   Accepted: 2051 Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital l