P89 习题6.1
http://t.cn/AiCu0lHe
#include <iostream>
#include <stack>
using namespace std;
int main(){
freopen("D://case.txt","r",stdin);
int numcase;
cin>>numcase;
stack <int> sta;
while(numcase!=0){
sta.push(numcase%8);
numcase /= 8;
}
while(!sta.empty()){
cout<<sta.top();
sta.pop();
}
return 0;
}
P89 习题6.2
http://t.cn/AiCuOSWv
#include <iostream>
#include <stack>
using namespace std;
int main(){
int m;
unsigned int x,y;
while(cin>>m>>x>>y){
if (x==0&&y==0){
cout<<'0'<<endl;continue;}
stack <int> sta;
unsigned int z = x+y;
while(z!=0){
sta.push(z%m);
z /= m;
}
while(!sta.empty()){
cout<<sta.top();
sta.pop();
}
cout<<endl;
}
}
P89 习题6.3
http://t.cn/AiCuig9B
#include <iostream>
using namespace std;
int main(){
int n;
while(cin>>hex>>n){
cout<<dec<<n<<endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int change(char x){
if (isdigit(x)){
return x-'0';}
else {
return x-'A'+10;}
}
int main(){
freopen("D://case.txt","r",stdin);
string stringcase;
while(cin>>stringcase){
int result = 0;
int hex = 1;
for(int i=stringcase.size()-1;i>=2;i--){
if (stringcase[i] == 'x'){
break;}
result += change(stringcase[i])*hex;
hex *= 16;
}
cout<<result<<endl;
}
return 0;
}
P89 习题6.4
http://t.cn/AiCu6ne4
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int change(char x){
if (isdigit(x)){
return x-'0';}
else if (x>='a' && x<='z'){
return x-'a'+10;}
else if (x>='A' && x<='Z'){
return x-'A'+10;}
}
char changeback(int x){
if (x>=0&&x<=9){
return x+'0';}
else {
return x+'A'-10;}
}
int main(){
string n;
int a,b;
cin>>a>>n>>b;
int result = 0;
stack <int> sta;
int bi = 1;
for (int i=n.size()-1;i>=0;i--){
result += change(n[i])*bi;
bi *= a;
}
while(result!=0){
sta.push(result%b);
result /= b;
}
while(!sta.empty()){
cout<<changeback(sta.top());
sta.pop();
}
return 0;
}
P92 习题6.5
#include <iostream>
#include <vector>
using namespace std;
int gcd(int x,int y){
if (y == 0){
return x;
}
else {
return gcd(y,x%y);
}
}
bool judge(int x,int y){
if (x<y && gcd(x,y) == 1){
return true;}
return false;
}
int main(){
freopen("D://case.txt","r",stdin);
int casenum;
while(cin>>casenum){
if (casenum == 0){
break;}
vector <int> vec;
int temp;
while(casenum--){
cin>>temp;
vec.push_back(temp);
}
int casecount = 0;
for (int i=0;i<vec.size();i++){
for (int j=0;j<vec.size();j++){
if (i!=j && judge(vec[i],vec[j])){
casecount++;}
}
}
cout<<casecount<<endl;
}
return 0;
}
P96 习题6.6
http://t.cn/AiCulrSh
#include <iostream>
#include <cmath>
using namespace std;
bool isprime(int x){
if (x == 2||x == 3){
return true;