系列文章目录
#include<iostream>
using namespace std;
int flag;
int n;
int main()
{
scanf("%d", &n);
while(n != 1)
{
if(n%2 == 0)
{
n = n / 2;
flag ++ ;
}
else
{
n = (3 * n + 1) / 2;
flag ++ ;
}
}
printf("%d", flag);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n, count = 0;
cin >> n;
while (n != 1) {
if (n % 2 != 0) n = 3 * n + 1;
n = n / 2;
count++;
}
cout << count;
return 0;
}
1002 写出这个数
不知道为什么这种做法一个样例通不过
#include <iostream>
#include <map>
using namespace std;
string s;
int n;
int temp;
int R(int x)//100 这种情况无法处理
{
int digit;
int ret = 0;
while (x > 0)
{
digit = x % 10;
ret = ret * 10 + digit;
x /= 10;
}
return ret;
}
int main()
{
cin >> s;
for (int i = 0; i < s.size(); i++)
n += (s[i] - '0');
n = R(n);
map<int, string> num;
num[0] = "ling";
num[1] = "yi";
num[2] = "er";
num[3] = "san";
num[4] = "si";
num[5] = "wu";
num[6] = "liu";
num[7] = "qi";
num[8] = "ba";
num[9] = "jiu";
if (n == 0)
cout << num[0];
while (n > 0)
{
temp = n % 10;
if (n / 10 != 0)
cout << num[temp] << "+";
else
cout << num[temp];
n = n / 10;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int sum = 0;
string str[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
for (int i = 0; i < s.length(); i++)
sum += (s[i] - '0');
string num = to_string(sum);
for (int i = 0; i < num.length(); i++) {
if (i != 0) cout << " ";
cout << str[num[i] - '0'];
}
return 0;
}
#include <iostream>
#include <map>
using namespace std;
int main() {
int n, p = 0, t = 0;
string s;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> s;
map<char, int> m;
for(int j = 0; j < s.size(); j++) {
m[s[j]]++;
if (s[j] == 'P') p = j;
if (s[j] == 'T') t = j;
}
if (m['P'] == 1 && m['A'] != 0 && m['T'] == 1 && m.size() == 3 && t-p != 1 && p * (t-p-1) == s.length()-t-1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n, max = -1, min = 101, score;
cin >> n;
string maxname, minname, maxnum, minnum, name, num;
for (int i = 0; i < n; i++) {
cin >> name >> num >> score;
if (max < score) {
max = score;
maxname = name;
maxnum = num;
}
if (min > score) {
min = score;
minname = name;
minnum = num;
}
}
cout << maxname << " " << maxnum << endl << minname << " " << minnum;
return 0;
}
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 1e3 + 10;
struct str {
string name, num;
int score;
}a, high, low;
int n;
int main()
{
high.score = 0;
low.score = 100;
scanf("%d", &n);
while (n--)
{
cin >> a.name >> a.num >> a.score;
if (a.score > high.score)high = a;
if (a.score < low.score)low = a;
}
cout << high.name << " " << high.num << endl;
cout << low.name << " " << low.num << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int arr[10000];
bool cmp(int a, int b) {return a > b;}
int main() {
int k, n, flag = 0;
cin >> k;
vector<int> v(k);
for (int i = 0; i < k; i++) {
cin >> n;
v[i] = n;
while (n != 1) {
if (n % 2 != 0) n = 3 * n + 1;
n = n / 2;
if (arr[n] == 1) break;
arr[n] = 1;
}
}
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++) {
if (arr[v[i]] == 0) {
if (flag == 1) cout << " ";
cout << v[i];
flag = 1;
}
}
return 0;
}
1006 换个格式输出整数
分析:因为n小于1000,所以数字不会超过百位~输入数据首先保存在a中,然后将a的每一个数字保存在int b[3]中,然后将b[2]、b[1]、b[0]中存储的数字看作输出次数依次输出B、S和12…b[0]
#include <iostream>
using namespace std;
int main() {
int a, i = 0;
cin >> a;
int b[3] = {0};
while (a != 0) {
b[i++] = a % 10;
a = a / 10;
}
for (int k = 0; k < b[2]; k++)
cout << "B";
for (int k = 0; k < b[1]; k++)
cout << "S";
for (int k = 0; k < b[0]; k++)
cout << k + 1;
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
const int N = 100010;
int n;
bool mark[N];
vector<int> a;
int flag;
bool isprime(int x)
{
if (x < 2) return false;
for (int i = 2; i * i <= x; i ++ )
if(x % i == 0) return false;
return true;
}
int main()
{
scanf("%d",&n);
for (int i = 1; i <= n ; i ++ ) mark[i] = isprime(i);
for (int i = 1; i <= n; i ++ )
if (mark[i] == true ) a.push_back(i);
for (int i = 0; i < a.size(); i ++)
if ( a[i+1] - a[i] == 2 ) flag ++;
cout << flag;
return 0;
}
#include <iostream>
using namespace std;
bool isprime(int a) {
for (int i = 2; i * i <= a; i++)
if (a % i == 0) return false;
return true;
}
int main() {
int N, cnt = 0;
cin >> N;
for (int i = 5; i <= N; i++)
if (isprime(i-2) && isprime(i)) cnt++;
cout << cnt;
return 0;
}
1008 数组元素循环右移问题
分析:数组长度为n,要想把数组循环右移m位,只需要先将整个数组a倒置,再将数组前m位倒置,最后将数组后n-m位倒置即可完成循环右移m位~reverse函数可以实现将一个数组或者vector中元素倒置,这个函数在algorithm头文件中~(如果m大于n,那么循环右移m位相当于循环右移m%n位,因为那些n倍数位的移动是多余的,所以在使用m之前,先将m = m%n)
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
m %= n;
if (m != 0) {
reverse(begin(a), begin(a) + n);
reverse(begin(a), begin(a) + m);
reverse(begin(a) + m, begin(a) + n);
}
for (int i = 0; i < n - 1; i++)
cout << a[i] << " ";
cout << a[n - 1];
return 0;
}
如果使用数组而不是vector的话 便要
先反转前面部分,再反转后面部分,最后再对整体进行反转。三步走策略。
也就是代码中这三行,关键是数字的设定,哪里该减一,哪里不能减一,方法就是用具体数字代入试一试。另外,reverse函数的编写,也是同样,重点是下标的计算,功能是原地反转。
reverse(a, 0, n-m-1);
reverse(a, n-m, n-1);
reverse(a, 0, n-1);
此题有一个坑点,M有可能大于等于N。
因此m = m%n;这句代码是不可缺少的,否则case 1,case 2错误,扣6分。
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<string> v;
string s;
while(cin >> s) { //cin遇到回车与空格结束读取
v.push(s);
}
cout << v.top();
v.pop();
while(!v.empty()) {
cout << " " << v.top();
v.pop();
}
return 0;
}
//输入结束时,在OJ里面会在输入文件结束后自动结束,如果是在IDE里面测试,会需要按回车 ctrl + Z 手动结束输入
#include <iostream>
using namespace std;
int main() {
int a, b, flag = 0;
while (cin >> a >> b) {
if (b != 0) {
if (flag == 1) cout << " ";
cout << a * b << " " << b - 1;
flag = 1;
}
}
if (flag == 0) cout << "0 0";
return 0;
}
补充知识:C++ reverse函数的用法
逆序(反转)无论是在C或是C++中用的都特别多,常用于数组,字符串,容器等,其本身的函数参数也不复杂。
标准C中是没有recerse()函数的,这是C++的一个新增函数,使用需要包含头文件
#include <algorithm>
reverse函数用于反转在[first,last)范围内的顺序(包括first指向的元素,不包括last指向的元素),reverse函数没有返回值
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first,BidirectionalIterator last);
例如,交换vector容器中元素的顺序
vector<int> v = {5,4,3,2,1};
reverse(v.begin(),v.end());//v的值为1,2,3,4,5
还有string类的字符串
string str="www.mathor.top";
reverse(str.begin(),str.end());//str结果为pot.rohtam.wwww
最后给出函数原型,该函数等价于通过调用iter_swap来交换元素位置
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last))
{
std::iter_swap (first,last);
++first;
}
}