LightOJ -1354 - IP Checking(字符串的处理)

题目大意:

  这题的意思就是说,给两个字符串,一个字符串中的数字是用10进制表示的,一个字符串的数字是用2进制表示的,看看十进制和二进制是否对应。

解题思路:

  处理在'.'上。。。。

代码:

 1 # include<cstdio>

 2 # include<iostream>

 3 # include<cstring>

 4 # include<cmath>

 5 

 6 using namespace std;

 7 

 8 # define MAX 50

 9 

10 char s1[MAX];

11 char s2[MAX];

12 int n1,n2,n3,n4;

13 string str;

14 

15 int check()

16 {

17     int i = 0, temp = 0;

18     while ( str[i]!='.' )

19     {

20         temp*=2;

21         temp+=(str[i]-'0');

22         i++;

23     }

24     if ( temp!= n1 )

25         return 0;

26     i++; temp = 0;

27     while ( str[i]!='.' )

28     {

29        temp*=2;

30        temp+=(str[i]-'0');

31        i++;

32     }

33     if ( temp!=n2 )

34         return 0;

35 

36     i++; temp = 0;

37     while ( str[i]!='.' )

38     {

39         temp*=2;

40         temp+=(str[i]-'0');

41           i++;

42     }

43     if ( temp!=n3 )

44         return 0;

45     i++; temp = 0;

46     while ( str[i]!='\0' )

47     {

48         temp*=2;

49         temp+=(str[i]-'0');

50           i++;

51     }

52     if ( temp!=n4 )

53         return 0;

54     return 1;

55 }

56 

57 

58 int main(void)

59 {

60     int icase = 1;

61     int t;cin>>t;

62     while ( t-- )

63     {

64         scanf("%d.%d.%d.%d",&n1,&n2,&n3,&n4);

65         cin>>str;

66         int flag = check();

67         printf("Case %d: ",icase++);

68         if ( flag )

69             cout<<"Yes"<<endl;

70         else

71             cout<<"No"<<endl;

72     }

73 

74     return 0;

75 }

 

你可能感兴趣的:(check)