乐视模拟编程题
来源:互联网 发布:道教佛教 知乎 编辑:程序博客网 时间:2024/05/16 05:14
这几道题都是以前遇到过的,看似比较简单,但也有一些细节AC不过! 注意1.cin.ignore()的用法。2.最后空格的处理。3,4.质因数的循环求解,质数的判断。
1.//输入字符串,统计某一字符出现的次数#include<iostream>using namespace std;#include<string>int main(){ string str; while (getline(cin,str)) { char ch; int times = 0; cin >> ch; for (int i = 0; i < str.size();i++) { if (str[i]==ch) { times++; } } cout << times<<endl; } return 0;} //AC 70%#include<iostream>#include<string>using namespace std;int main(){ string str; while (getline(cin, str)) { char s; cin >> s; int count = 0; for (int i = 0; i < str.size(); i++) { if (s == str[i]) count++; } cout << count << endl; cin.ignore(); }}//下面有问题//while (getline(cin, str))//cin >> s//循环的时候会出问题,第二次循环读不出s,后面加上一个cin.ignore()就好了,给所有出现此问题的人。。2.//计算字符串最后一个单词的字符数#include <string>#include <iostream>using namespace std;int main(){ //cout << "请输入字符串:"; string str; int count = 0; getline(cin, str); for (int i = str.size()-1; i >=0; i--) { if (str[i] == ' ') break; else count++; } cout << count << endl; // cout << str.size(); return 0;}// C++//有些同学的答案没考虑到末尾有空格的情况,对于末尾有空格的都输出为0了。//“hello world ”依然输出5.#include<iostream>#include<string>using namespace std;int main(){ string s; while (getline(cin, s)){ int n = 0, flag = 1; for (int i = s.length() - 1; i >= 0; --i){//倒着计算 if (flag && s[i] == ' '){//如果末尾有空格,先清除末尾空格 continue; } else if (s[i] != ' '){ flag = 0; ++n; } else{ break; } } cout << n << endl; } return 0;}3.//输出某一正整数的所有公因数//质数因子#include<iostream>using namespace std;int main(){ int N; while (cin>>N) { int i = 2; while (N!=1) { if (N%i==0) { N = N / i; cout << i << " "; continue; } i++; } cout << endl; } return 0;}4.//质因数的个数#include<iostream>using namespace std;int main(){ int N; while (cin >> N) { int i = 2; int n = 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 namespace std;int main(){ int n; int count = 0; while (cin >> n) { count = 0; int i = 2; while (i <= sqrt(n)) { while (n % i == 0) { n = n / i; count++; if (n == 1) break; } i++; } if (n > sqrt(n)) //考虑n是否存在大于sqrt(n)的质因数,如果存在,最多存在一个,因为两个大于sqrt(n)的数相乘大于n //本身就是大的质数 count++; cout << count << endl; }}//////////////////////////////////////////////////////////////////////////#include<iostream>#include<cmath> using namespace std;//是否本身就是为质数int isprime(int n){ if (n == 1 || n == 2 || n == 3){ return 1; } for (int i = 2; i <= sqrt(n); i++){ if (n%i == 0){ return 0; } } return 1;}int judge(int n){ for (int i = 2; i < sqrt(n); i++){ if (isprime(i)){ if (n%i == 0){ return judge(n / i) + 1; } } } return 1;}int main(){ int n; while (cin >> n){ cout << judge(n) << endl; } return 0;}
0 0
- 乐视模拟编程题
- 腾讯模拟笔试编程题
- 【算法】牛客网模拟题编程部分
- 校招模拟 [编程题]页码统计
- 牛客网模拟测试 3 道编程题
- 编程模拟试题1
- 编程模拟试题2
- 编程模拟试题
- JS模拟多线程编程
- 模拟QQ(Socket编程)
- MyBatis接口编程模拟
- 编程模拟五
- 牛客网互联网秋招模拟笔试编程题第三题
- Google™ Code Jam - 中国编程挑战赛模拟题一
- Google™ Code Jam - 中国编程挑战赛模拟题二
- Google™ Code Jam - 中国编程挑战赛模拟题三
- google编程大赛模拟题及入围赛真题
- 网易有道2017内推编程题 洗牌(模拟)
- 【神经网络与深度学习】CIFAR-10数据集介绍
- @Autowired注解与@Resource注解的区别
- 大数据时代的数据存储,非关系型数据库MongoDB
- NoSQL
- 关系数据库&&NoSQL数据库
- 乐视模拟编程题
- cin.ignore()函数的用法
- 初识HTTP 1.1与HTTP 1.0
- 观察者模式
- 简单的多线程理解
- C++11 多线程
- GC算法 垃圾收集器
- Treap树
- 主键为多个字段的映射问题