2진수를 2개 입력받고 그 두 수를 더한결과를 2진수로 출력하라.
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main(){
string a,b;
int s[100],i,j,k=0,l=0;
cin >> a >>b;
i=a.length()-1;
j=b.length()-1;
while(k>0||i >= 0||j>= 0){
if(i>=0) k+=a[i]-'0';
if(j>=0) k+=b[j]-'0';
s[l++]=k%2;
k/=2;
i--;
j--;
}
for(l;l>0;l--)cout<<s[l-1];
cout<<endl;
system("PAUSE");
return 0;
}
결과
100100
10111
111011
Press any key to continue . . .
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main(){
string a,b;
int s[100],i,j,k=0,l=0;
cin >> a >>b;
i=a.length()-1;
j=b.length()-1;
while(k>0||i >= 0||j>= 0){
if(i>=0) k+=a[i]-'0';
if(j>=0) k+=b[j]-'0';
s[l++]=k%2;
k/=2;
i--;
j--;
}
for(l;l>0;l--)cout<<s[l-1];
cout<<endl;
system("PAUSE");
return 0;
}
결과
100100
10111
111011
Press any key to continue . . .
댓글
댓글 쓰기