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 KatrineYang at 2017-01-18 11:56:16 on Problem 1537
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int hasExp(string s){
	//return the exponent position; if no exponent return -1, if already illegal return -2
	int l = s.length();
	int hasPt = 0;
	for(int i = 0; i < l; i++){
		char c = s[i];
		if((c>'9' || c<'0') && c!='+' && c!='-' && c!='e' && c!='E' && c!='.') return -2;
		if(c=='e' || c=='E') return i;
		if(c=='.') hasPt++;
	}
	if(!hasPt || hasPt > 1) return -2;
	return -1;
}

bool isNoExp(string s){
	int l = s.length();
	if(!l) return false;
	if(s[0] == '+' || s[0] == '-'){
		s = s.substr(1);
		l--;
	}
	if(!l) return false;
	int ptPos = -1;
	for(int i = 0; i < l; i++){
		if(s[i] == '.'){
			ptPos = i;
			break;
		}
	}
	if(ptPos <= 0 || ptPos >= l-1) return false;
	for(int i = 0; i < l; i++){
		if(i==ptPos) continue;
		if(s[i] < '0' || s[i] > '9') return false;
	}
	return true;
}

bool isAExp(string s){
	int l = s.length();
	if(!l) return false;
	if(s[0] == '+' || s[0] == '-'){
		s = s.substr(1);
		l--;
	}
	if(!l) return false;
	for(int i = 0; i < l; i++){
		if(s[i] < '0' || s[i] > '9') return false;
	}
	return true;
}

bool legal(string s){
	int state = hasExp(s);
	if(state == -2) return false;
	if(state == -1) return isNoExp(s);
	string st = s.substr(0, state);
	return (isNoExp(st) || isAExp(st)) && isAExp(s.substr(state+1));
}

int main() {
	string s;
	while(1){
		cin >> s;
		if(!strcmp("*", s.c_str())){
			break;
		}
		cout << s << " is " << (legal(s) ? "" : "il") << "legal.\n";
	}
	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