티스토리 뷰

알고리즘/홍정모 알고리즘

초등학교 뺄셈

박완희버서커 2025. 8. 27. 20:20
반응형

https://www.acmicpc.net/problem/15740

 

 

이 문제에서 지금 덧셈과 뺄셈은 구현했다.

지금 뺄셈의 문제점

  1. A>=B일 때만 작동한다
  2. 둘 다 양수일 때만 작동한다.

 

코드를 보면,

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

void Substraction(char*A,char*B)
{
    char Sum_arr[10002];
    int A_start=(int)strlen(A)-1;
    int B_start=(int)strlen(B)-1;
    int start=0,temp=0,cal=0;
    int end=max(A_start,B_start);
    
    while((A_start>=0 || B_start>=0) && start<=end)
    {
        
        if(B_start<0)
        {
            cal=(A[A_start]-'0'-temp);
            A_start--;
        }
        else if(A_start<0)
        {
            cal=(B[B_start]-'0'-temp);
            B_start--;
        }
        else
        {
            cal=(A[A_start]-'0')-(B[B_start]-'0')-temp;
            B_start--;
            A_start--;
        }
        if(cal<0)
        {
            temp=1;
            Sum_arr[start]=(10+cal)+'0';
        }
        else
        {
            temp=0;
            Sum_arr[start]=(cal)+'0';
        }
        
        
        start++;
    }
    if(temp==1)
    {
        Sum_arr[start]=temp+'0';
        start++;
    }
    bool check=false;
    for(int i=start-1;i>=0;i--)
    {
        if(Sum_arr[i]!='0')
            check=true;
        if(check || i==0)
            cout<<Sum_arr[i];
    }
    cout<<endl;
}


int main(void)
{
    char A[10001];
    char B[10001];
    
    cin>>A>>B;

    
    Substraction(A,B);
    
    
    return 0;
}

 

앞서 구현했던 덧셈의 코드를 이용했다.

 

2025.08.27 - [알고리즘/홍정모 알고리즘] - 초등학교 덧셈

 

초등학교 덧셈

백준 문제 A+B를 기준으로 실험하였다.https://www.acmicpc.net/problem/1000우선 이건 성공했다. 정모형이 홍랩에 있는 코드는 올리지말라고 해서 힌트 안 보고 나혼자 한 코드를 우선 올리겠다. #include #inc

eunjin123123-programming.tistory.com

 

고친점이 있다면,

 

1. '+' 를 '-'로 바꿨다.

   if(B_start<0)
        {
            cal=(A[A_start]-'0'-temp);
            A_start--;
        }
        else if(A_start<0)
        {
            cal=(B[B_start]-'0'-temp);
            B_start--;
        }
        else
        {
            cal=(A[A_start]-'0')-(B[B_start]-'0')-temp;
            B_start--;
            A_start--;
        }
  • A[A_start]+ temp; 이 부분을 A[A_start]- temp;로 바꾸었다(else if, else 마찬가지) .
  • cal=(A[A_start]-'0')-(B[B_start]-'0')-temp; 에서 중간을 -로 바꾸었다.

 

2. 업데이트 규칙을 >10에서 <0으로 바꾸었다.

 if(cal<0)
        {
            temp=1;
            Sum_arr[start]=(10+cal)+'0';
        }
        else
        {
            temp=0;
            Sum_arr[start]=(cal)+'0';
        }
  • 5-9면 4는 남기고 -1을 상부로 넘긴다.
  • 6-5처럼 양수가 남는 경우는 그냥 더한다.
  • 그래서 Sum_arr을 업데이트 하는 방법이 두가지로 바뀌었다.

 

3. 출력형식을 바꾸었다.

 bool check=false;
    for(int i=start-1;i>=0;i--)
    {
        if(Sum_arr[i]!='0')
            check=true;
        if(check || i==0)
            cout<<Sum_arr[i];
    }
    cout<<endl;

 

  • 처음에 0이 아닌 숫자가 나오면 true로 바뀐다.
  • true인 경우에만 숫자를 리턴한다. 예를 들어 00001이면 0000인 경우는 false여서 출력을 안한다.
  • 답이 0일수도 있으니 맨 마지막 경우에는 출력을 한다.

 

 

반응형

'알고리즘 > 홍정모 알고리즘' 카테고리의 다른 글

초등학교 덧셈  (1) 2025.08.27
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/11   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
글 보관함
반응형