IP的計算 hdu2206

來源:互聯網
上載者:User

http://acm.hdu.edu.cn/showproblem.php?pid=2206

//在wrong 10次後終於AC
//為何簡單的題目要考慮這麼久?
#include<iostream>//2265653 2010-03-29 21:34:12 Accepted 2206 0MS 228K 1452 B C++ 悔惜晟
#include<string>
#include<cstdio>
using namespace std;
int main()
{
 char str[104];
 int i, j, k, num1;
 while(gets(str))
 {
  for(i = 0; str[i] ==' '; i++);//輸入有空格的情況例如:  1.2.3.2
  for( j = strlen(str) - 1; str[j] ==' '; j--);//最後也有空格
  if(str[i] =='.' || str[j] == '.')//開始和最後是‘.’ 
  {
   printf("NO/n");
   continue;
  }
  if(j - i > 14 || j - i < 6)//比最大的長,比最小的短
  {
   printf("NO/n");
   continue;
  }
  int num = 0;
  bool flag = false;
  for(k = i; k <= j ; k++)
  {
   if(str[k] =='.') num++;
   else if(!(str[k] >='0' && str[k] <= '9'))//有不合法的字元
   {
    flag =true;
    break;
   }
  }
  if(num > 3 || flag)
  {
   printf("NO/n");
   continue;
  }
  int sum= 0;
  int a = 0;
         num1 = 0;
  flag = false;
  str[j+1] = '.';
  for(k = i; k <= j+1; k++)
  {
   if(str[k] != '.')
   {
    a += str[k] - '0';
    num1++;
    a *= 10;
   }
   else
   {

    if( num1 == 1 && a /10 >=0 )
    {
     a = 0;
     num1 = 0;
     continue;
     //flag = true;
     //break;
    }
    else if(num1 == 2 && a /10 >=10 )//1.00.2.3不合法
    {
     //flag = true;
     //break;
            a = 0;
     num1 = 0;
     continue;
    }
    else if(num1 == 3 && a /10 >=100 &&  a /10 <= 255 ) //12.23.003.2  1.23.25.256 不合法
    {
     a = 0;
     num1 = 0;
     continue;
    }
    else //1111.23.2.3不合法
    {
     flag = true;
     break;
    }
   }
  }
  //if(flag)
   //printf("NO/n");
  //else
  if(!flag && num == 3)
   printf("YES/n");
  else
   printf("NO/n");
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.