题目链接:http://ac.jobdu.com/problem.php?pid=1006
题目分析:
要读懂题目里给的3条规则,找到会Accepted的规律。我找到的规律是:
1.首字母为z:那么不管中间有多少个o,只要最后只有1个j就结束的话,那么全部Accepted。
2.首字母不为z,则必须为o:根据后面两条规则,找到规律----只要a*b = c(这里b是z和j之间o的个数),那么全部Accepted。当然之前要进行第二条规则的判断。
这道题倒是没什么难度,就是考虑条件的时候要考虑周全,还要考虑字符串的结束问题。
源代码:
#include <iostream> #include <string> using namespace std; int main() { string s; while (cin>>s) { int x = 0; int a = 0, b = 0, c = 0; int temp = -1; if (s[0] == 'z') { int d = 1; //判断z后边为o if (s[d] != 'o') { cout<<"Wrong Answer"<<endl; } else { while (s[d] == 'o') { d++; } //判断o后边为j if (s[d] != 'j') { cout<<"Wrong Answer"<<endl; } else { d ++; //判断j后边是否还有字符 if (d != s.length()) { cout<<"Wrong Answer"<<endl; } else { cout<<"Accepted"<<endl; } } } } else {//首字母不为z,则必为o,计算o的个数 if (s[0] != 'o') { cout<<"Wrong Answer"<<endl; } else { int i = 0; while (s[i] == 'o') { i++; } x = i; a = i; //o判断结束,o之后必须是z if(s[i] != 'z') { cout<<"Wrong Answer"<<endl; } else {//o后边是z i++; //判断z后边是否为o if (s[i] != 'o') { cout<<"Wrong Answer"<<endl; } else {//z后边为o,计算o的个数 int j = 0; while (s[i] == 'o') { i++; j++; //o的个数 } b = j; if (s[i] != 'j') { cout<<"Wrong Answer"<<endl; } else {//判断j后边o的个数 int k = 0; i++; if (s[i] != 'o') { cout<<"Wrong Answer"<<endl; } else { while (s[i] == 'o') { i++; k++; } //判断字符串是否结束 if (i != s.length()) { cout<<"Wrong Answer"<<endl; } else { c = k; //比较a与c if (a * b == c) { cout<<"Accepted"<<endl; } else { cout<<"Wrong Answer"<<endl; } } } } } } } } } return 0; }