Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

给出一个比较好理解的代码

Posted by ajioy at 2011-07-26 10:29:49 on Problem 2105
#include <iostream>
using namespace std;
int Calculate(char *arr,int low,int high)
{
	int i,result=0;
    for(i=low;i<=high;i++)
    if(arr[i]-'0')//由于是字符数组,要将其值转为整型
    result+=(1<<high-i);//1<<high-i是位运算,计算速度超快。相当于2^(high-i)
    return result;
}

int main()
{
	int n,i,j;
	char data[33];//int data[n][33]刚开始用int型的二维数组,脑子肯定进水了
	int hh,h,ll,l;
	cin>>n;
	while(n--)
	{
      cin>>data;
      hh=Calculate(data,0,7);//高8位 
      h=Calculate(data,8,15);//次高8位 
      ll=Calculate(data,16,23);//低8位 
      l=Calculate(data,24,31);//最低8位,因为计算过程一致,所以直接用一个方法
      cout<<hh<<'.'<<h<<'.'<<ll<<'.'<<l<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator