uva 1590
2015-09-13 11:20
218 查看
十进制与二进制的转换, 简单题
但是刚写起来思路太乱, 改了好多遍, 所以写得比较丑, 仅供娱乐
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <list>
using namespace std;
const int MAXN = 60;
const int SIZE = 6;
typedef long long LL;
/*
uva 1590
关键 : 十进制(字符串)转二进制问题
*/
inline void Dec2Bi(int dec,char *result){
for(int i=0; i<8; i++){
if( dec & 1 ){
result[7-i] = '1';
}else{
result[7-i] = '0';
}
dec >>= 1;
}
result[8] = '\0';
}
string Bi2Dec(char bi[MAXN]){
string res;
for(int i=0; i<4; i++){
int sum = 0;
for(int j=0; j<8; j++){
if( bi[i*8+j]=='1' ){
sum += (1<<(7-j));
}
}
char tmp[10];
sprintf(tmp,"%d",sum);
res += tmp;
if( i!=3 ) res += ".";
}
return res;
}
int Compare(string a,string b){
int i;
for(i=0; i<a.length(); i++){
if( a[i]!=b[i] ){
break;
}
}
return i;
}
void test(){
unsigned int sum = pow(2,9)-1;
cout << (sum<<8) << endl;
cout << (sum<<16) << endl;
cout << (sum<<24) << endl;
}
int main(){
int m,t;
unsigned int dec;
char tmp[9];
string ip;
vector<string> input;
while( scanf("%d",&m)!=EOF ){
ip.clear();
input.clear();
while( m-- ){
t = 4;
ip.clear();
while( t-- ){
scanf("%d",&dec);
getchar();
Dec2Bi(dec,tmp);
ip += tmp;
}
input.push_back(ip);
}
char mask[MAXN]={0},res[MAXN]={0};
strcpy(mask,input[0].c_str());
for(int i=1; i<input.size(); i++){
mask[Compare(mask,input[i])] = '\0';
}
int masklen = strlen(mask);
strcpy(res,input[0].c_str());
for(int i=masklen; i<32; i++){
res[i] = '0';
}
cout << Bi2Dec(res) << endl;
strcpy(res,input[0].c_str());
int i=0;
for(i=0; i<masklen; i++){
res[i] = '1';
}
for( ; i<32; i++){
res[i] = '0';
}
cout << Bi2Dec(res) << endl;
}
return 0;
}
但是刚写起来思路太乱, 改了好多遍, 所以写得比较丑, 仅供娱乐
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <list>
using namespace std;
const int MAXN = 60;
const int SIZE = 6;
typedef long long LL;
/*
uva 1590
关键 : 十进制(字符串)转二进制问题
*/
inline void Dec2Bi(int dec,char *result){
for(int i=0; i<8; i++){
if( dec & 1 ){
result[7-i] = '1';
}else{
result[7-i] = '0';
}
dec >>= 1;
}
result[8] = '\0';
}
string Bi2Dec(char bi[MAXN]){
string res;
for(int i=0; i<4; i++){
int sum = 0;
for(int j=0; j<8; j++){
if( bi[i*8+j]=='1' ){
sum += (1<<(7-j));
}
}
char tmp[10];
sprintf(tmp,"%d",sum);
res += tmp;
if( i!=3 ) res += ".";
}
return res;
}
int Compare(string a,string b){
int i;
for(i=0; i<a.length(); i++){
if( a[i]!=b[i] ){
break;
}
}
return i;
}
void test(){
unsigned int sum = pow(2,9)-1;
cout << (sum<<8) << endl;
cout << (sum<<16) << endl;
cout << (sum<<24) << endl;
}
int main(){
int m,t;
unsigned int dec;
char tmp[9];
string ip;
vector<string> input;
while( scanf("%d",&m)!=EOF ){
ip.clear();
input.clear();
while( m-- ){
t = 4;
ip.clear();
while( t-- ){
scanf("%d",&dec);
getchar();
Dec2Bi(dec,tmp);
ip += tmp;
}
input.push_back(ip);
}
char mask[MAXN]={0},res[MAXN]={0};
strcpy(mask,input[0].c_str());
for(int i=1; i<input.size(); i++){
mask[Compare(mask,input[i])] = '\0';
}
int masklen = strlen(mask);
strcpy(res,input[0].c_str());
for(int i=masklen; i<32; i++){
res[i] = '0';
}
cout << Bi2Dec(res) << endl;
strcpy(res,input[0].c_str());
int i=0;
for(i=0; i<masklen; i++){
res[i] = '1';
}
for( ; i<32; i++){
res[i] = '0';
}
cout << Bi2Dec(res) << endl;
}
return 0;
}
相关文章推荐
- Binder进程间通信机制的Service Manager组件的启动过程
- Android菜鸟的成长笔记——开发环境的搭建
- 华为 2016届实习笔试面试总结
- TestList
- 碱基序列的儿子最长上涨
- 关于迷你DVD管理器的一些总结
- 华为机试题第三题
- wpf之数据触发器DataTrigger
- android EditText 设置密码锁源码
- 一篇很详细的Freemarker讲解(小demo、Servlet中运用、freemarker语言的各种)
- 四层和七层负载均衡
- Github for Windows使用介绍
- java设计模式系列--外观模式
- JavaScript权威指南_157_第17章_事件处理_17.0-概述
- 《他的国》读后感
- iOS属性修饰符
- 安卓开发问题及其解决
- 的问题上,下数
- 【转载】2015年8月编程语言排行榜:Java遥遥领先
- php的时间输出格式