P5015 [NOIP2018 普及组] 标题统计
1.getline(cin,x)的使用
题意:计算除空格和换行符以外的字符数
输入:
234
输出:
3
输入:
Ca 45
输出:
4
#includeusing namespace std; string s; int main() { getline(cin,s); int l=s.length(); int ans=l; for(int i=0;i P1055 [NOIP2008 普及组] ISBN 号码
1.注意整型和字符的转化
2.学习优化后的代码实现方式计算规则:见题
输入:0-670-82162-4
输出:Right
输入:0-670-82162-0
输出:0-670-82162-4#includeusing namespace std; int main() { string s; cin>>s; int j=1; int sum=0; int num=1; int ans=0; for(int i=0; i<=12; i++) { if(i==11) { ans=s[12]-'0'; break; } if(s[i]!='-') { sum+=j*int(s[i]-'0'); j++; } } sum=sum%11; if(sum==ans||(sum==10&&s[12]=='X')) cout<<"Right"< 优化:
#includeusing namespace std; int main() { char a[14]; char mod[12]="0123456789X"; cin>>a;//输入字符串 int i,j=1,sum=0; for(i=0; i<12; i++) { if(a[i]=='-') continue; sum+=(a[i]-'0')*j++; } if(mod[sum%11]==a[12]) cout<<"Right"< P1308 NOIP2011 普及组 统计单词数
C++ string 的使用
字符匹配:b.find(a)!=string::npos
转化大小写: b[i]=tolower(b[i]);题意:
输入一个单词,和一行文章,输出出现该单词的个数和第一次出现的位置
没有该单词则输出 -1输入:
To
to be or not to be is a question
输出:
2 0输入:
to
Did the Ottoman Empire lose its power at that time
输出:
-1虽然这个代码还没过,40 scores .
#includeusing namespace std; //知识点: //字符匹配 //转化大小写 int main() { string a,b; getline(cin,a); getline(cin,b); a=' '+a+' '; b=' '+b+' '; for(int i=0;i 未完



