1.2020.2
#include <iostream>
#include <string>
#include <stdlib.h> //atoi的头文件
using namespace std;
int main() {
int t;
cin >> t;
string str, substr; //这些都写外边吧,写里面运行好像有点问题
int len, a, b;
while(t) {
cin >> str;
cin >> len >> a >> b;
for (int j = 0;j <= str.length() - len;j++) {
//substr = str.substr(j, len); //提取子串,好好好,这个也别用了
//int temp = atoi(substr.c_str()); //string转int
int temp = 0;
for (int k = j;k < j+len;k++) { //string转int用这个吧,能朴素点就朴素点吧
temp = temp * 10 + str[k] - '0';
}
if (temp % a == b) {
for (int i = j; i < j + len;i++) //输出子串
cout << str[i];
cout << endl;
}
}
t--;
}
return 0;
}
atoi考试非要写的话,感觉括号里可以不写那一堆,可以直接atoi(substr),也不知道我这编译器又抽哪门子疯
substr(a,b) :a是需要提取的子字符串的起始下标,b是需要提取的子字符串的长度
2.2018.1
这个其实也是处理字符串,再加个快排
#include <iostream>
#include <string>
using namespace std;
void quickSort(int a[], int low, int high);
int partition(int a[], int low, int high);
int main() {
int t;
cin >> t;
string str;
int n; //存放拆分后数字的数组 的 下标
while (t) {
n = 0;
cin >> str;
int len = str.length();
int* a = new int[len](); //动态数组,存放拆分后的数字
for (int i = 0; i < len;i++) { //从前往后扫描字符串
if (str[i] != '5') {
a[n] = a[n] * 10 + str[i] - '0';
}
else {
if (i != len - 1) { //若最后一个数字是5
n++;
}
}
}
quickSort(a, 0, n);
for (int i = 0; i <= n;i++) {
cout << a[i] << " ";
}
cout << endl;
t--;
}
return 0;
}
//快排
void quickSort(int a[], int low, int high) {
if (low < high) { //跳出递归条件
int pivotpos = partition(a, low, high);
quickSort(a, low, pivotpos - 1); //依次对两个子表递归排序
quickSort(a, pivotpos + 1, high); //依次对两个子表递归排序
}
}
//一次划分
int partition(int a[], int low, int high) {
int pivot = a[low]; //枢轴元素
while (low < high) {
while (low < high && a[high] >= pivot) {
high--;
}
a[low] = a[high];
while (low < high && a[low] <= pivot) {
low++;
}
a[high] = a[low];
}
a[low] = pivot; //枢轴元素放到最终位置
return low; //返回最终存放枢轴元素的位置
}