These questions have been encountered before, it seems relatively simple, but there are some details AC but! Note the use of 1.cin.ignore (). 2. Processing of the last space. 3,4. Factorization's cyclic solution, prime number judgment.
1.//enter a string to count the occurrences of a character#include<iostream>using namespacestd; #include<string>intMain () {stringstr; while(Getline (cin,str)) {Charch; intTimes =0; CIN>>ch; for(inti =0; I < str.size (); i++) { if(str[i]==ch) { times++; }} cout<< times<<Endl; } return 0;} AC 70% #include<iostream>#include<string>using namespacestd;intMain () {stringstr; while(Getline (CIN, str)) {Chars; CIN>>s; intCount =0; for(inti =0; I < str.size (); i++) { if(s = =Str[i]) Count++; } cout<< Count <<Endl; Cin.ignore (); }}//here's the problem.//While (Getline (CIN, str))//Cin >> S//the cycle will be problematic, the second cycle can not read S, followed by a cin.ignore () is good, to all the people who have this problem: 2.//computes the number of characters in the last word of a string#include<string>#include<iostream>using namespacestd;intMain () {//cout << "Please enter string:"; stringstr; intCount =0; Getline (CIN, str); for(inti = str.size ()-1; I >=0; i--) { if(Str[i] = =' ') Break; ElseCount++; } cout<< Count <<Endl; //cout << str.size (); return 0;}//C + +//Some students of the answer does not take into account the end of a space, for the end of the output of a space is 0. //"Hello World" still outputs 5.#include<iostream>#include<string>using namespacestd;intMain () {strings; while(Getline (CIN, s)) {intn =0, flag =1; for(inti = s.length ()-1; I >=0; -I.) {//counting backwards. if(flag && s[i] = =' '){//If there are spaces at the end, clear the trailing spaces first Continue; } Else if(S[i]! =' ') {flag=0; ++N; } Else{ Break; }} cout<< N <<Endl; } return 0;}3.//outputs all common factor of a positive integer//Prime number Factor#include<iostream>using namespacestd;intMain () {intN; while(cin>>N) {inti =2; while(n!=1) { if(n%i==0) {N= N/i; cout<< I <<" "; Continue; } I++; } cout<<Endl; } return 0;}4.//Number of factorization#include<iostream>using namespacestd;intMain () {intN; while(Cin >>N) {inti =2; intn =0; while(N! =1) { if(n%i = =0) {N= N/i; //cout << i << "";n++; Continue; } I++; } cout<< N <<Endl; } return 0;}//////////////////////////////////////////////////////////////////////////#include <iostream>#include<math.h>using namespacestd;intMain () {intN; intCount =0; while(Cin >>N) {count=0; inti =2; while(I <=sqrt (n)) { while(n% i = =0) {n= N/i; Count++; if(n = =1) Break; } I++; } if(N > sqrt (n))//consider whether n has a mass factor greater than sqrt (n), and if so, there is at most one, since two of the numbers greater than sqrt (n) multiply by more than n//is a big prime number in itself.count++; cout<< Count <<Endl; }}//////////////////////////////////////////////////////////////////////////#include <iostream>#include<cmath>using namespacestd;//whether it's a prime number in itselfintIsPrime (intN) { if(n = =1|| n = =2|| n = =3){ return 1; } for(inti =2; I <= sqrt (n); i++){ if(n%i = =0){ return 0; } } return 1;}intJudgeintN) { for(inti =2; I < sqrt (n); i++){ if(IsPrime (i)) {if(n%i = =0){ returnJudge (n/i) +1; } } } return 1;}intMain () {intN; while(Cin >>N) {cout<< judge (N) <<Endl; } return 0;}
Music Visual Simulation Programming problem