PAT 1104-1105

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值