zoukankan      html  css  js  c++  java
  • uva oj 10106 Product

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=1047

    花了一上午,做了这一题,悲催啊.......主要是一些小问题没有注意到,一直WA........

    就是大数处理,给两个相当长的大数,求乘积,算法很简单,用数组保存,然后模拟手算的方式,int x[],y[],用y的每一位乘以x,最终得结果.

    一开始,没有注意到0的处理,WA了好几次,然后相加的时候忘记了处理进位.....相乘的过程倒是没有搞错进位......其实之前的晚上才做了两个大数相加......

    都想放弃这一题了,反正自己知道怎么做,以为是什么规则错了或是其他什么地方错了而不是自己的结果错了,然后试试再看看,发现了问题!

    看来知道做和做出来是两回事啊!

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 char chx[251],chy[251];      //读输入的字符串
     5 int intx[251],inty[251],ans[501],anstemp[501];
     6 
     7 int ch_to_int(char arrch[],int arrin[])    //返回这个数字的长度
     8 {
     9     int len=strlen(arrch);
    10     for (int i=0;i<len;i++)
    11     {
    12         arrin[i]=arrch[len-1-i]-48;
    13     }
    14     return len;
    15 }
    16 
    17 int main(void)
    18 {
    19     while (gets(chx)&&gets(chy))
    20     {
    21         if (chx[0]=='0'||chy[0]=='0')
    22         {
    23             printf("0");
    24         } 
    25         else
    26         {
    27             memset(intx,0,sizeof(intx));
    28             memset(inty,0,sizeof(inty));
    29             memset(ans,0,sizeof(ans));
    30 
    31             /*原始长度----可能只是一个0,但是长度不为0*/
    32             int lenx=ch_to_int(chx,intx);  
    33             int leny=ch_to_int(chy,inty);
    34 
    35             /*把y分解,分别乘x*/
    36             for (int i=0;i<leny;i++)
    37             {
    38                 memset(anstemp,0,sizeof(anstemp));
    39 
    40                 /*先乘,放到anstemp里,再在anstemp的前面加0*/
    41                 int m,yushu=0,thislen=lenx;   //余数,这次相乘后anstemp的长度
    42                 for (m=0;m<lenx;m++)
    43                 {
    44                     int temp=intx[m]*inty[i]+yushu;
    45                     anstemp[m]=temp%10;
    46                     yushu=temp/10;
    47                 }
    48                 if (yushu)   //最后还有一个进位
    49                 {
    50                     anstemp[m]=yushu;
    51                     thislen++;
    52                 }
    53 
    54                 /*再移动,在数组开始加0*/
    55                 /*把x先右移动i位,在空出的地方加0*/
    56                 for (int n=0;n<i;n++)   //移动i次
    57                 {
    58                     for (int j=thislen-1;j>=0;j--)   //这只移动了一次
    59                     {
    60                         anstemp[j+1]=anstemp[j];
    61                     }
    62                     anstemp[0]=0;   //在后面加一个0
    63                     thislen++;    //移动一次长度加一
    64                 }
    65 
    66                 /*再把anstemp和ans相加,模拟手算----------------注意这里的进位!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
    67                 int jinweiyicuo=0;      //这里的进位反倒没有考虑..........
    68                 for (int n=0;n<=500;n++)
    69                 {
    70                     int tempagain=ans[n]+anstemp[n]+jinweiyicuo;
    71                     ans[n]=tempagain%10;
    72                     jinweiyicuo=tempagain/10;
    73                 }
    74             }
    75 
    76             /*输出*/
    77             int m;
    78             for (m=500;m>=0;m--)
    79             {
    80                 if (ans[m])
    81                 {
    82                     break;
    83                 }
    84             }
    85             for (int k=m;k>=0;k--)
    86             {
    87                 printf("%d",ans[k]);
    88             }
    89         }
    90         
    91         putchar('\n');
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    编程之道——高内聚低耦合
    虚拟机的安装
    Tomcat的安装与配置
    Spring(十)--Advisor顾问
    Spring(九)--通知
    Spring(八)-- 代理设计模式
    Spring(七)--Spring JDBC
    Spring(六)--Spring配置文件之间的关系
    Spring(五)--autowire自动装配和spel
    Spring(四)--bean的属性赋值
  • 原文地址:https://www.cnblogs.com/jiayith/p/3059337.html
Copyright © 2011-2022 走看看