메뉴 건너뛰기

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 176
201 4893 관리자 2019.12.20 146
200 4878 관리자 2019.12.20 146
199 4848 관리자 2019.12.20 149
198 4698 관리자 2019.12.20 145
197 4685 관리자 2019.12.20 203
196 4073 관리자 2020.04.06 157
195 4068 관리자 2020.04.06 214
194 4065 관리자 2019.12.20 152
193 4064 관리자 2020.04.06 154
192 4059 관리자 2020.04.06 5938
191 4055 관리자 2020.04.06 153
190 4044 관리자 2020.04.06 159
189 4043 관리자 2020.04.06 152
188 4040 관리자 2020.04.06 151
187 4039 관리자 2019.12.20 154
186 4035 관리자 2020.04.06 156
185 4034 관리자 2020.04.06 148
184 4028 관리자 2020.04.06 166
183 4023 관리자 2019.12.20 175
182 3740 관리자 2019.12.20 213
181 3730 관리자 2019.12.20 177
180 3719 관리자 2019.12.20 155
179 3716 관리자 2019.12.20 144
178 3713 관리자 2019.12.20 164
177 3712 관리자 2019.12.20 161
176 3709 관리자 2019.12.20 147
175 3708 관리자 2019.12.20 114
174 3707 관리자 2019.12.20 109
173 3705 관리자 2019.12.20 112
위로