A1104
题目链接
Sample Input:
4
0.1 0.2 0.3 0.4
Sample Output:
5.00
Note
- 题意为输出所有增序的序列合
- 题目简单但是直接模拟做回导致运行超时
- 推导所需的公式:假设序列的首尾指针 分别为p和q,那么对于p,有i种选择,即12…i,对于q,有n-i+1种选择,即i, i+1, … n,所以p和q组合 形成的⾸首尾⽚片段有i * (n-i+1)种,因为每个里⾯都会出现temp,所以temp引起的总和为temp * i * (n – i +1);遍历完所有数字,将每个temp引起的总和都累加到sum中,最后输出sum的值
Code1
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e6;
int main(){
#ifdef _DEBUG
ifstream cin("data.txt");
#endif
int num;
double a[maxn], sum = 0;
cin >> num;
for(int i = 0; i < num; i++){
cin >> a[i];
}
for(int i = 0; i < num; i++){
double temprecord = 0;
for(int j = i; j < num; j++){
sum += temprecord + a[j] ;
temprecord += a[j ];
}
}
printf("%.2lf", sum);
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
Code2
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e6;
int main(){
#ifdef _DEBUG
ifstream cin("data.txt");
#endif
int num;
double sum = 0.0, temp;
cin >> num;
for (int i = 1; i <= num; i++) {
cin >> temp; sum = sum + temp * i * (num - i + 1);
}
printf("%.2lf", sum);
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
A1105
题目链接
Sample Input:
12
37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93
42 37 81
53 20 76
58 60 76
Note
- 题意,输入n个数,递减排列后螺旋输出
- 思路,完全按照状态机模拟的, 速度较慢, 而且有几个点格式错误
Code1
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e3;
int a[maxn];
bool cmp(int a, int b){
return a > b;
}
int visited[maxn][maxn] = {0}, temp_load[maxn][maxn] = {0}, output[maxn][maxn];
int main(){
#ifdef _DEBUG
ifstream cin("data2.txt");
#endif
int num, m, n;
cin >> num;
for(int i = 0; i < num; i++){
cin >> a[i];
}
int temp;
for(temp = (int)sqrt(num); ; temp++) if(num % temp == 0) break;
n = temp;
m = num / temp;
sort(a, a+num, cmp);
int cnt = 0;
fill(visited[0], visited[0] + maxn*maxn, 1);
for(int i = 0; i <= m; i++){
for(int j = 0; j <= n; j++){
if(i > 0 && j > 0){
temp_load[i][j] = a[cnt++];
visited[i][j] = 0;
}
}
}
int df = 0, rf = 0, lf =0, uf = 0, i = 1, j = 1, a = 1, b = 1;
while(num--){
if(rf == 0 && visited[i][j] == 0){
visited[i][j] = 1;
output[i][j] = temp_load[a][b++];
j++;
}
if(rf == 1 && df == 0 && visited[i][j] == 0){
visited[i][j] = 1;
output[i][j] = temp_load[a][b++];
i++;
}
if(rf == 1 && df == 1 && lf == 0 && visited[i][j] == 0){
visited[i][j] = 1;
output[i][j] = temp_load[a][b++];
j--;
}
if(rf == 1 && df == 1 && lf == 1 && uf == 0 && visited[i][j] == 0){
visited[i][j] = 1;
output[i][j] = temp_load[a][b++];
i--;
}
if(visited[i][j] == 1 && rf == 0) {
j--;
i++;
rf = 1;
}
if(visited[i][j] == 1 &&rf == 1&& df == 0) {
i--;
j--;
df = 1;
}
if(visited[i][j] == 1 &&rf == 1&& df == 1&& lf == 0) {
i--;
j++;
lf = 1;
}
if(visited[i][j] == 1 &&rf == 1&& df == 1&& lf == 1 && uf == 0) {
i++;
j++;
rf = lf = df = uf = 0;
}
if(b == n + 1){
b = 1;
a++;
}
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
if( j ==1) cout << output[i][j] ;
else cout << " " << output[i][j];
}
cout << endl;
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
Code2
To be continued
A1101
题目链接
Sample Input:
5
1 3 2 4 5
Sample Output:
3
1 4 5
Note
- 找到可以当作quick sort的pivot的值,
- 初始思路: 瞬间找规律觉得排序完,相同位置值相同的就可以当做pivot, 其实不然,若左右都有要互换位置的就相互抵消,其实就差一步, 判断其大于左侧最大值即可
Code1
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e6;
int a[maxn], b[maxn], c[maxn];
int main(){
#ifdef _DEBUG
ifstream cin("data3.txt");
#endif
int num, m, n;
cin >> num;
for(int i = 0; i < num; i++){
cin >> a[i];
b[i] = a[i];
}
sort(a, a+num);
int cnt = 0;
for(int i = 0; i < num; i++){
if(a[i] == b[i]) c[cnt++] = a[i];
}
cout << cnt << endl;
for(int i = 0; i < cnt; i++){
if(i == 0) cout << c[i];
else cout << " " << c[i];
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
Code2
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e6;
int a[maxn], b[maxn], c[maxn];
int main(){
#ifdef _DEBUG
ifstream cin("data3.txt");
#endif
int num, m, n, max = 0;
cin >> num;
for(int i = 0; i < num; i++){
cin >> a[i];
b[i] = a[i];
}
sort(a, a+num);
int cnt = 0;
for(int i = 0; i < num; i++){
if(a[i] == b[i] && b[i] > max) c[cnt++] = a[i];
if(b[i] > max) max = b[i];
}
cout << cnt << endl;
for(int i = 0; i < cnt; i++){
if(i == 0) cout << c[i];
else cout << " " << c[i];
}
cout << endl; //没有能当pivot的输出空行
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
A1103
题目链接
Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossible
Note
- 题意: 给一个数,输出第二个数个的第三个数方和,若多个,输出底数和最大者,若依旧多个,输出字典序最大者
- 典型dfs题目,还需整理
Code1
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e3;
int a[maxn];
int num, factor_num, index;//目标 因式个数 指数
int max_base_sum = -1, temp;
vector <int> result, temp_result;
void Dfs(int n, int depth, int sum, int base_sum){
if(depth > factor_num || sum > num) return;
if(sum == num && depth == factor_num){
if(base_sum > max_base_sum){
max_base_sum = base_sum;
result = temp_result;
}
return ;
}
if(n - 1 >= 0){
temp_result.push_back(n);
Dfs(n, depth + 1, sum + a[n], base_sum + n);
temp_result.pop_back();
Dfs(n - 1, depth, sum, base_sum);
}
}
int main(){
#ifdef _DEBUG
ifstream cin("data4.txt");
#endif
cin >> num >> factor_num >> index;
temp = (int)pow(num, (double)(1.0/index));
for(int i = 0; i <= temp; i++){
a[i] = pow(i, index);
}
Dfs(temp, 0, 0, 0);
if(max_base_sum == -1) cout << "Impossible" << endl;
else{
cout << num << " = " << result[0] << "^" <<index;
for(int i = 1; i < result.size(); i++)
cout << " + " << result[i] << "^" <<index;
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}