郑非吧 关注:17贴子:1,465
  • 7回复贴,共1
记录编程时的种种问题


IP属地:浙江1楼2016-03-16 12:21回复
    openjudge #10001 “exponentiation” (百练)
    这道高精度求幂也编了好久,主要是下标错误导致的error。
    一定要在编程的时候把特定的数组【下标】想清楚,在程序中统一规定(如最大位数,小数点位置这样的int变量,如果统一用后一位的下标表示,就不能出现用前一位的下标表示的算法,否则很容易出错)
    还有执行循环的时候一定要考察元素在一次循环结束后是否应该【清零】。
    一开始我的output数组在一次乘法结束后忘记清零了,导致一些奇怪的现象。
    还有,记得各种值、数组【初始化】。
    检查的时候,注意 0 1;11.111 2;1.2345 5;这样的输入。有可能问题就出现在0这样的点。
    这里编的程序并不好,通用性不良。
    ++++++++代码++++++++++++++++++++++++++++++++++
    #include <iostream>
    #include <string>
    using namespace std;
    const int maxLen=5,maxPower = 25;
    int myPower(int input[maxLen],int power,int output[maxLen*maxPower],int &dotIndex,int &end,int &zeroes)
    {
    int last = end;
    int temp[maxLen*maxPower];int iz;int begin = zeroes;
    for(int k=0;k<=last;k++)
    {
    output[k] = input[k];
    }
    for(iz=0;iz<=dotIndex;iz++)
    {
    if(input[iz]!=0)
    {
    zeroes = iz;
    break;
    }
    }
    if(iz>dotIndex)
    {
    zeroes = dotIndex;
    }
    for(int i=0;i<power-1;i++)
    {
    for(int k=0;k<=last;k++)
    {
    temp[k] = output[k];
    output[k] =0 ;
    }
    for(int j=0;j<=last;j++)
    {
    for(int k=0;k<=end;k++)
    {
    output[j+k]+=temp[j]*input[k];
    }
    }
    for(int j=0;j<=end+last+1;j++)
    {
    output[j+1]+=(output[j]/10);
    output[j]%=10;
    }
    if(output[end+last+1]==0)
    {
    last = end + last;
    }
    else
    {
    last = end + last+1;
    }
    }
    zeroes = zeroes*power;
    dotIndex = dotIndex*power;
    end = last;
    return 0;
    }
    int main()
    {
    char in[maxLen+1]="";
    int power;int count=0;
    int outputF[10][maxLen*maxPower+1]={0};
    while(cin>>in>>power)
    {
    int input[maxLen]={0};
    int output[maxLen*maxPower]={0};
    int dotIndex=-1,end=maxLen-1,zeroes=0;
    int j=0;
    for(int i=0;i<maxLen+1&&in[i];i++)
    {
    if(in[i]>='0'&&in[i]<='9')
    {
    input[maxLen-1-j] = in[i]-'0';
    j++;
    }
    else
    if(in[i]=='.')
    {
    dotIndex = maxLen-j;
    }
    else
    {
    break;
    }
    }
    if(dotIndex == -1)
    dotIndex = maxLen -j;
    if(power==0)
    {
    cout<<1<<endl;
    continue;
    }
    myPower(input,power,output,dotIndex,end,zeroes);
    int jF=0;
    for(int i = end;i>=dotIndex&&(output[end]||zeroes==dotIndex);i--)
    {
    cout<<output[i];
    //outputF[count][jF++]=output[i];
    }
    for(int i=dotIndex-1;i>=zeroes;i--)
    {
    if(i==dotIndex-1)
    {
    cout<<".";
    //outputF[count][jF++]=-1;continue;
    }
    cout<<output[i];
    //outputF[count][jF++]=-1;
    }
    cout<<endl;
    //outputF[count][jF]=-2;
    count++;
    }
    /* for(int i=0;i<count;i++)
    {
    for(int j=0;outputF[count][j]!=-2;j++)
    {
    cout<<(outputF[count][j]==-1)?'.':outputF[count][j];
    }
    cout<<endl;
    }*/
    return 0;
    }


    IP属地:浙江4楼2016-03-23 13:36
    回复
      2026-01-23 04:45:27
      广告
      不感兴趣
      开通SVIP免广告
      今天编openjudge #1006 Biorhythms 居然花了好长时间纠错。看来编写的时候一定要把【关键数学公式】搞清楚,注意特殊情形特别是0和负数的时候
      还有 openjudge 的批改方式
      一个输入后伴随一个输出还是全部输入结束后一起输出并没有关系。
      ++++++++++++++++++代码++++++++++++++++++++++
      #include <iostream>
      const int Max_List_Length = 10000;
      const int physicalInterval = 23;
      const int emotionalInterval = 28;
      const int intellectualInterval = 33;
      using namespace std;
      int TriplePeakDay(int physicalInit,int emotionalInit,int intellectualInit,int initialDay)
      {
      int currentDay = ((initialDay-intellectualInit)/intellectualInterval )*intellectualInterval+intellectualInit;
      //这里如果initialDay 在 大于等于 intellectualInit时, currentDay 就会取在initialDay之前的intellectual peak上
      // 导致结果输出【负数】
      if(currentDay <= initialDay) currentDay += intellectualInterval;/这个判断语句非常重要
      for(;;currentDay+=intellectualInterval)
      {
      if((currentDay-physicalInit)%physicalInterval == 0&&(currentDay - emotionalInit)%emotionalInterval ==0)
      {
      return currentDay;
      }
      }
      }
      int main()
      {
      int DaysList[Max_List_Length];
      int physicalInit,emotionalInit,intellectualInit,initDay;
      int listCount=0;
      while(cin>>physicalInit>>emotionalInit>>intellectualInit>>initDay)
      {
      if(physicalInit ==-1)
      {
      break;
      }
      DaysList[listCount] = TriplePeakDay(physicalInit,emotionalInit,intellectualInit,initDay) - initDay;
      listCount ++;
      }
      for(int i=0;i<listCount;i++)
      {
      cout<<"Case "<<i+1<<": the next triple peak occurs in "<<DaysList[i]<<" days."<<endl;
      }
      return 0;
      }


      IP属地:浙江5楼2016-03-25 23:21
      回复
        写了个合并排序,总是出现奇怪的东西,什么“堆已经损坏”,输出结果中出现奇怪的大整数。检查了半个小时,才发现原来是 new int[] 写成了 new int()
        注: int* a = new int[10] 表示动态分配出含有 10个 new int 的数组。
        int a = new int(10)表示 动态分配出一个 new int 并赋值为10。
        误写成new int()后,a[index]指向的是非法的内存,所以会被改变从而读出奇怪的的东西。
        代码:
        —————————————————————————————————
        ————————————————————-—————————————
        #include "stdafx.h"
        #include <iostream>
        using namespace std;
        int Merge(int *HalfSeq1,int len1,int *HalfSeq2,int len2,int *MergedSequence)
        {
        int seq1Index=0,seq2Index=0;int MergedIndex=0;
        while(seq1Index!=len1||seq2Index!=len2)
        {
        if(seq2Index==len2||(seq1Index <len1&&HalfSeq1[seq1Index]<HalfSeq2[seq2Index]))
        {
        MergedSequence[MergedIndex] = HalfSeq1[seq1Index];MergedIndex++;
        seq1Index++;
        }
        else
        {
        MergedSequence[MergedIndex] = HalfSeq2[seq2Index];MergedIndex++;
        seq2Index++;
        }
        }
        return 0;
        }
        int MergeSort(int *Sequence,int begin,int end,int* MergedSequence)
        {
        /* for(int i=0;i<10;i++)
        {
        cout<<Sequence[i]<<" ";
        }
        cout<<endl;*/
        int len1= (end -begin)/2;int len2= (end - begin)-len1;
        int *HalfSeq1 = new int[len1];int *HalfSeq2 = new int[len2];
        if(begin+1 == end)
        {
        MergedSequence[0] = Sequence[begin];
        return 0;
        }
        if(len1!=1)
        {
        MergeSort(Sequence,begin,begin+len1,HalfSeq1);
        }
        else
        {
        HalfSeq1[0] = Sequence[begin];
        }
        if(len2!=1)
        {
        MergeSort(Sequence,begin+len1,end,HalfSeq2);
        }
        else
        {
        HalfSeq2[0] = Sequence[end -1];
        }
        //---------
        Merge(HalfSeq1,len1,HalfSeq2,len2,MergedSequence);
        return 0;
        }
        int _tmain(int argc, _TCHAR* argv[])
        {
        int length;
        cout<<"Programmed by Zhengfei 20160512"<<endl;
        for(;;)
        {
        cout<<"Input the length of the sequence: "<<endl;
        cin>>length;
        int *Sequence = new int[length]
        int *SortedSequence = new int[length];
        for(int i=0;i<length;i++)
        {
        cin>>Sequence[i];
        }
        MergeSort(Sequence,0,length,SortedSequence);
        for(int i=0;i<length;i++)
        {
        cout<<SortedSequence[i]<<" ";
        }
        cout<<endl;
        }
        return 0;
        }


        IP属地:浙江7楼2016-05-13 22:55
        回复
          这几天编写C++加密程序,用VS2015, fstream来读写文件。发现一旦遇到字符0x1A fstream.eof()就变成了true。研究了半天,发现我fstream.open的参数 open(filename,ios::in(或 ios_base::in),ios::binary)是错误的。第三个参数是nProt, 只有第二个参数是nMode表示文件打开方式,如果有多种打开方式,应该用|,绝不是逗号。正确的应该是open(filename,ios::in|ios::binary).
          之所以遇到0x1A就判定fstream.eof()为true,导致文件只能读取一部分,是因为没有用binary打开,在windows系统下某些符号和linux不同,会识别为结束符。


          IP属地:浙江来自Android客户端8楼2016-09-25 21:15
          回复
            x86 assembly languege
            in Linux Ubuntu 16.04
            Write a bubble sort program:
            Pay attention: While compiling a 32-bit asm in 64-bit machine, you should use extra instruction
            if not, there will be some strange problems.
            When I just use:
            as -g sort.asm -o sort.o
            ld sort.o -o sort
            and use gdb to debug sort
            the instruction after "incl" will be skipped
            So the right should be
            as --32 -g sort.asm -o sort.o
            ld -m -elf_i386 sort.o -o sort
            source code:
            .code32
            .section .data
            data_items:
            .long 1,5,4,6,3,2,7,8,0
            .section .text
            .globl _start
            _start:
            movl $8,%esi
            loop_1:
            movl data_items(,%edi,4),%eax
            cmpl %esi,%edi
            je loop_2
            incl %edi
            movl data_items(,%edi,4),%ebx
            cmpl %eax,%ebx
            jg ex1
            jmp loop_1
            ex1:
            movl %eax,data_items(,%edi,4)
            decl %edi
            movl %ebx,data_items(,%edi,4)
            incl %edi
            jmp loop_1
            loop_2:
            decl %esi
            cmpl $0,%esi
            je end
            movl $0,%edi
            jmp loop_1
            end:
            movl $1,%eax
            int $0x80


            IP属地:浙江9楼2017-03-07 16:59
            回复
              .code32
              .section .data
              data_items:
              .long 1,5,4,6,3,2,7,8,0
              .section .text
              .globl _start
              _start:
              movl $0,%edi
              movl data_items(,%edi,4),%eax
              addl $1,%edi
              movl data_items(,%edi,4),%ebx
              cmpl %eax,%ebx
              movl $1,%eax
              int $0x80


              IP属地:浙江10楼2017-03-08 19:25
              回复