메뉴 건너뛰기

Programing

Code Up

3022

관리자 2020.04.06 19:41 조회 수 : 51

C++

#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

int first[110],second[110];

char str[110];

void reverseString(char* s) {

  size_t size_ = strlen(s);

  char temp;

 

  for (size_t i = 0; i < size_ / 2; i++) {

    temp = s[i];

    s[i] = s[(size_ - 1) - i];

    s[(size_ - 1) - i] = temp;

  }

}

int ab(int cnt1,int cnt2){

    if(cnt1>cnt2)

        return 0;

    if(cnt2>cnt1)

        return 1;

    for(int i=cnt1;i>=0;i--){

        if(first[i]>second[i])//first is bigger than second

            return 0;

        if(first[i]<second[i])//second is bigger than first

            return 1;

    }

    return 0;//case of all of them are same

}

int main()

{

    scanf("%s",str);

    reverseString(str);

    int i=0,cnt=0,cnt2=0;

    while(str[i]!='\0'){

        first[i]=(int)str[i]-48;

        cnt++;

        i++;

    }

    scanf("%s",str);

    //gets(str);

    reverseString(str);

    i=0;

    while(str[i]!='\0'){

        second[i]=(int)str[i]-48;

        cnt2++;

        i++;

    }

    int a_b=ab(cnt,cnt2);

    if(a_b==1){

        printf("-");

    }

    for(int j=0;j<cnt2;j++){

        if(a_b==0)

            first[j]-=second[j];

        else{

            first[j]-=first[j]*2;

            first[j]+=second[j];

        }

    }

    cnt=max(cnt,cnt2);

    for(int j=1;j<=cnt+3;j++)

    {

        if(first[j-1]<0){

            first[j]-=1;

            first[j-1]=10+first[j-1];

        }

    }

    int a=1,b=0;

    for(int j=cnt+3;j>=0;j--)

    {

        if(a==1&&first[j]!=0){

            a=0;

        }

        if(a==1&&first[j]==0)

            continue;

        if(b==0&&a==1&&first[j]==-1){

            b=1;

            //printf("-");

        }

        printf("%d",first[j]);

    }

    if(a==1){

        printf("0");

    }

 

    return 0;

}

 

번호 제목 글쓴이 날짜 조회 수
공지 안내사항 관리자 2019.12.02 175
172 3703 관리자 2019.12.20 8
171 3701 관리자 2019.12.20 9
170 3520 관리자 2019.12.20 8
169 3120 관리자 2019.12.20 8
168 3023 관리자 2020.04.06 60
» 3022 관리자 2020.04.06 51
166 3021 관리자 2020.04.06 63
165 3009 관리자 2019.12.20 8
164 3008 관리자 2019.12.20 6
163 3007 관리자 2019.12.20 7
162 3006 관리자 2019.12.20 14
161 2748 관리자 2019.12.20 9
160 2657 관리자 2019.12.20 21
159 2652 관리자 2019.12.20 7
158 2641 관리자 2019.12.20 7
157 2634 관리자 2019.12.20 6
156 1953 관리자 2019.12.20 7
155 1936 관리자 2019.12.20 7
154 1925 관리자 2019.12.20 7
153 1525 관리자 2019.12.20 9
152 1505 관리자 2019.12.20 7
151 1476 관리자 2019.12.20 3
150 1162 관리자 2019.12.20 7
149 1161 관리자 2019.12.20 7
148 1160 관리자 2019.12.20 7
147 1159 관리자 2019.12.20 5
146 1158 관리자 2019.12.20 5
145 1157 관리자 2019.12.20 10
144 1156 관리자 2019.12.20 7
143 1155 관리자 2019.12.20 5
위로