標題:
508 二進位運算
[打印本頁]
作者:
李知易
時間:
2024-11-7 17:15
標題:
508 二進位運算
1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。
2. 設計說明:
請撰寫一個程式,讓使用者輸入兩個8位元的二進位字串,分別輸出兩字串以十進位、二進位相加的結果,若二進位相加超出位元顯示範圍,皆以「11111111」表示。
提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。
3. 輸入輸出:
輸入說明
兩個二進位字串
輸出說明
兩字串以十進位、二進位相加的結果
範例輸入1
11001100
00010010
範例輸出1
204 + 18 = 222
11011110
範例輸入2
11111011
10010011
範例輸出2
251 + 147 = 398
11111111
本帖隱藏的內容需要回復才可以瀏覽
作者:
朱奕祈
時間:
2024-11-30 15:01
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a,b,c;
int main()
{
getline(cin,s1);
a=stoi(s1,nullptr,2);
getline(cin,s2);
b=stoi(s2,nullptr,2);
c=a+b;
cout<<a<<" + "<<b<<" = "<<c<<endl;
if(c>255)
cout<<"11111111";
else
cout<<bitset<8>(c);
return 0;
}
複製代碼
作者:
李穎俊
時間:
2024-11-30 15:02
本帖最後由 李穎俊 於 2024-11-30 15:04 編輯
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a,b;
int main(){
getline(cin,s1);
a=stoi(s1,nullptr,2);
getline(cin,s2);
b=stoi(s2,nullptr,2);
cout<<a<<" + "<<b<<" = "<<a+b<<endl;
if(a+b>255)
cout<<"11111111"<<endl;
else
cout<<bitset<8>(a+b);
}
複製代碼
作者:
蔡宇庭
時間:
2024-11-30 15:03
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a,b,c;
int main()
{
getline(cin,s1);
a=stoi(s1,nullptr,2);
getline(cin,s2);
b=stoi(s2,nullptr,2);
c=a+b;
cout<<a<<" + "<<b<<" = "<<c<<endl;
if(c>255)
cout<<"11111111";
else
cout<<bitset<8>(c);
return 0;
}
複製代碼
作者:
張淯祺
時間:
2024-11-30 15:03
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
string s1,s2;
int main()
{
getline(cin,s1);
a=stoi(s1,nullptr,2);
getline(cin,s2);
b=stoi(s2,nullptr,2);
c=a+b;
cout<<a<<" + "<<b<<" = "<<c<<endl;
if(c>255)
cout<<"11111111";
else
cout<<bitset<8>(c);
return 0;
}
複製代碼
作者:
陳奕睿
時間:
2024-11-30 15:07
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a,b,c;
int main()
{
getline(cin, s1);
a = stoi(s1, nullptr, 2);
getline(cin, s2);
b = stoi(s2, nullptr, 2);
c = a + b ;
cout<<a<<" + "<<b<<" = "<<c<<endl;
if(c > 255)
cout<<"11111111";
else
cout<<bitset<8>(c);
return 0;
}
複製代碼
作者:
蔡岑昕
時間:
2024-11-30 15:08
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a,b,c;
int main()
{
getline(cin,s1);
a=stoi(s1,nullptr,2);
getline(cin,s2);
b=stoi(s2,nullptr,2);
c=a+b;
cout<<a<<" + "<<b<<" = "<<c<<endl;
if(c>255)
cout<<"11111111";
else
cout<<bitset<8>(c);
return 0;
}
複製代碼
作者:
劉秉昕
時間:
2024-11-30 15:08
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a,b,c;
int main()
{
getline(cin,s1);
a=stoi(s1,nullptr,2);
getline(cin,s2);
b=stoi(s2,nullptr,2);
c=a+b;
cout<<a<<" + "<<b<<" = "<<c<<endl;
if(c>255)
cout<<"11111111";
else
cout<<bitset<8>(c);
return 0;
}
複製代碼
作者:
張仲言
時間:
2024-12-2 20:54
#include<bits/stdc++.h>
using namespace std;
string str1,str2;
int xstr1,xstr2,n;
int main()
{
getline(cin,str1);
getline(cin,str2);
xstr1=stoi(str1,nullptr,2);
xstr2=stoi(str2,nullptr,2);
n=xstr1+xstr2;
printf("%d + %d = %d\n",xstr1,xstr2,n);
if(n>255)
cout<<11111111<<'\n';
else
cout<<bitset<8>(n);
return 0;
}
複製代碼
作者:
洪榮辰
時間:
2024-12-21 15:08
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1, s2;
int a, b;
getline(cin,s1);
getline(cin,s2);
a=stoi(s1, nullptr, 2);
b=stoi(s2, nullptr, 2);
int c=a+b;
cout<<a<<" + "<<b<<" = "<<c<<endl;
if(c>255)
cout<<"11111111";
else
cout<<bitset<8>(c);
return 0;
}
複製代碼
作者:
蔡岑昕
時間:
2024-12-21 15:33
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int main()
{
getline(cin,s1);
getline(cin,s2);
int sum1=0,sum2=0;
for(int i=7;i>=0;i--)
{
sum1+=(s1[7-i]-'0')*pow(2,i);
sum2+=(s2[7-i]-'0')*pow(2,i);
}
cout<<sum1<<" + "<<sum2<<" = "<<sum1+sum2<<endl;
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2