1104 Sum of Number Segments
测试点3需要用long double才能过…
long double用%Lf来格式控制
double 用%lf来格式控制
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >>N;
vector<long double> nums;
for(int i=0;i<N;i++){
long double tmp;
scanf("%Lf",&tmp);
nums.push_back(tmp);
}
long double ans=0;
long double tmp=0;
for(int i=0;i<N;i++){
tmp+=nums[i];
ans+=tmp;
}
long double pre=ans;
for(int i=1;i<N;i++){
pre=(pre-(N-i+1)*nums[i-1]);
ans+=pre;
}
printf("%.2Lf",ans);
return 0;
}
1105 Spiral Matrix
要注意每一行最后不要输出多余空格
#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int N;
cin >> N;
vector<int> nums;
for(int i=0;i<N;i++){
int tmp;
scanf("%d",&tmp);
nums.push_back(tmp);
}
sort(nums.begin(),nums.end(),cmp);
int n=(int)sqrt(N);
for(;n>=1;n--){
if(N%n==0){
break;
}
}
int m=N/n;
vector<vector<int> > ans(m,vector<int>(n,0));
vector<vector<bool> > vis(m,vector<bool>(n,false));
int d=0;//右下左上
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int x=0,y=0;
int cnt=0;
while(cnt<N){
if(x<0 || x>=m || y<0 || y>=n || vis[x][y]){
x-=dx[d];
y-=dy[d];
d=(d+1)%4;
x+=dx[d];
y+=dy[d];
continue;
}
vis[x][y]=true;
ans[x][y]=nums[cnt];
cnt++;
x+=dx[d];
y+=dy[d];
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout << ans[i][j];
if(j!=n-1){
cout << " ";
}
}
cout << endl;
}
return 0;
}