【PAT】B1012. 数字分类(C++精确到小数点、题目理解)

浙大PAT水题型简单分析归类 PAT(pat.zju.edu.cn)是一个面向C/C++程序的Online Judge系统。相比ZOJ,HDOJ,POJ等ACM题库,PAT的题目非常基础,对于数据结构、算法的入门是比较有助益的。 本文按照自己的认识,给PAT advanced level做出了分类。由于很多题目实际上兼顾有多种解法和思路,我给每道题打上了tag,来应对分类的独占性局限,供大家参考。 简单题 这类题几乎不需 阅读详情

【PAT】B1012. 数字分类(C++精确到小数点、题目理解)

@(PAT)

链接:https://www.patest.cn/contests/pat-b-practise/1012

题目不难,根据5个部分逐渐取数判断即可。
坑:
1. A1的条件是求偶数的和。
2. 输出N的条件是求出来的A1~A5的结果为空,一开始求了当被5整取的数字为0个才输出N,这是错误的。

AC code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main() {
    int n;
    cin>> n;
    vector<int> a1, a2, a3, a4, a5;
    for (int k= 0; k< n; k++) {
        int temp;
        cin>> temp;
        if (temp% 5== 0) {
            if (temp% 2== 0) a1.push_back(temp);
        } else if (temp% 5== 1) {
            a2.push_back(temp);
        } else if (temp% 5== 2) {
            a3.push_back(temp);
        } else if (temp% 5== 3) {
            a4.push_back(temp);
        } else {
            a5.push_back(temp);
        }
    }
    if (a1.size()== 0) {
        cout<< "N ";
    } else {
        int A1= 0;
        for (int i= 0; i< a1.size(); i++) {
            A1+= a1[i];
        }
        cout<< A1<< " ";
    }
    if (a2.size()== 0) {
        cout<< "N ";
    } else {
        int A2= 0;
        for (int i= 0; i< a2.size(); i++) {
            if (i% 2== 0) {
                A2+= a2[i];
            } else {
                A2-= a2[i];
            }
        }
        cout<< A2<< " ";
    }
    if (a3.size()== 0) {
        cout<< "N ";
    } else {
        cout<< a3.size()<< " ";
    }
    if (a4.size()== 0) {
        cout<< "N ";
    } else {
        int sum= 0;
        for (int i= 0; i< a4.size(); i++) {
            sum+= a4[i];
        }
        float mean= (float)sum/ (float)a4.size();
        cout<< fixed<< setprecision(1)<< mean<< " ";
    }
    if (a5.size()== 0) {
        cout<< "N";
    } else {
        int A5= a5[0];
        for (int i= 1; i< a5.size(); i++) {
            if (a5[i]> A5) {
                A5= a5[i];
            }
        }
        cout<< A5;
    }
}

注意C++中,保留小数点n位的方法:

#include <iomanip>
cout<< fixed<< setprecision(1)<< mean<< " ";
pat乙级代码 B1020月饼 #include<stdio.h> #include<algorithm> using namespace std; struct mooncake{ double store; double sell; double price; }cake[1010]; bool cmp(mooncake a,mooncake b){ return a.price&gt... 阅读详情

相关推荐

文本纠错项目代码调试

文本纠错项目调试

m0_67084346的博客 868

C++规定精确到小数点后几位的方法(cout与printf两种情况)

程序经常被要求输出结果精确到小数点后几位,又不会c++格式化输出,代码中如何编写?` #include #include #include “stdlib.h” using namespace std; int main(){ double PI=3.1415926; cout<<setprecision(n)<<PI<<endl; return 0; } 这里的n指保留n位有效数字,即保留小数点后(n-1)位。 而整数输出也想保留小数点,就要将setprecision(

zhaoweining2001的博客 1万+

三菱PLC通信库大比拼:手把手教你用C#实现最高效的McProtocol数据读写

本文深入比较了三菱PLC通信库的性能,重点介绍了如何使用C#高效实现McProtocol数据读写。通过实测数据对比HslCommunication、McProtocol-next和自定义方案的优劣,提供优化技巧和代码示例,帮助开发者在工业自动化场景中提升通信效率。

m0n1o2p的博客 255

PAT basic 1012 数字分类 (20分) C++

PAT basic 1012 数字分类 (20分) C++ 一、题目描述 给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字: A​1= 能被 5 整除的数字中所有偶数的和; A​2= 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n​1−n​2 +n​3−n​4⋯; A​3= 被 5 除后余 2 的数字的个数; A4 = 被 5 除后余 3 的数字的平均数,精确到小数...

雨渊的博客 457

新手必看:5分钟搞定Unitree Go1机器狗Gazebo仿真运动控制(附按键图解)

本文为新手提供了Unitree Go1机器狗在Gazebo仿真环境中实现运动控制的快速指南。通过5分钟极简配置和按键图解,帮助开发者快速掌握宇树科技明星产品的基础操作,包括环境搭建、仿真启动和运动控制技巧,适合零基础用户快速上手。

weixin_42550597的博客 300

ROS2仿真抓取分拣案例,使用myCobot 280机械臂结合ROS2系统搭建机械分拣站

本案例展示了如何利用myCobot 280 urdf模型构建一个对象分类站,并在ROS2 jazzy中完成运动规划。

m0_71627844的博客 2125

【PAT乙级】1012 数字分类 (20 分) C++实现

1.题目描述 2.完整代码 #include<iostream> #include<math.h> using namespace std; int main(){ int N; cin >> N; int s[N]; int a=0,b=0,j=0,k=0,max=0,num1=0; double q,y=0; for(int i=0;i<N;i++){ cin >> s[i]; if(s[i]%5==0&&

m0_53171456的博客 606

《QT+PCL 第一章》:点云读取显示

点云读取显示一、核心代码二、QT设置三、效果展示 一、核心代码 二、QT设置 三、效果展示

阁楼 1万+

PAT初级1012 数字分类 (C++)

** PAT初级1012 数字分类 (C++) ** 思路: 对数字分类后按分支处理,特别注意A2,交错求和可能会导致和为0(后续判断是否有数字可能会受影响,不绝对) C++的按精度输出需注意 代码: # include <iostream> #include <iomanip> # include <cmath> using namespace std; int main() { int N=0,number=0; cin>>N;

weixin_43860330的博客 1081

Modbus协议学习第一篇之基础概念

因工作需要而接触Modbus,正在恶补知识,利用博客的方式来加深理解和记忆,同时希望同行大佬能一起交流。

hzf978742221的博客 880

air780e stm32标准库发送英文短信,如何实现?(附相关源码,仅供参考)

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1239

PAT Basic Level 1012 数字分类(C++实现)

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字: A1 = 能被 5 整除的数字中所有偶数的和; A2 = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n1−n2+n3−n4⋯; A3 = 被 5 除后余 2 的数字的个数; A4 = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位; A5 = 被 5 除后余 4 的数字中最大数字。 输入格式: 每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 100

菜鸟 419

Obsidian与RAG:知识管理的未来之战

摘要: 本文对比了传统RAG、Obsidian和llm-wiki-skill三种知识管理方案。传统RAG依赖查询时临时检索,知识无法积累;Obsidian需人工整理,成本高但结构化强;llm-wiki-skill通过AI在摄入时自动编译知识为结构化Wiki,实现持续增值。核心差异在于知识处理时机——llm-wiki-skill将"查询时检索"转变为"摄入时编译",通过三层Markdown结构(sources/concepts/entities)和自动双向链接构建可进化

ZhangJiqun&191cm&85kg 617

PAT B级 1012数字分类 (20point(s))

题目 给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字: A1 = 能被 5 整除的数字中所有偶数的和;A​2 = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n​1 − n​2 +n3−n​4⋯;A3 = 被 5 除后余 2 的数字的个数;A​4 = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;A​5 = 被 5 除后余 4 的数字中最大数字。 输...

一杯刘 219

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

3.1 PAT B1012 数字分类 (20分)

1012 数字分类 (20分) 给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字: A​1​​ = 能被 5 整除的数字中所有偶数的和; A​2​​ = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n​1​​−n​2​​+n​3​​−n​4​​⋯; A​3​​ = 被 5 除后余 2 的数字的个数; A​4​​ = 被 5 除后余 3 的数字的平均数,精确到小数点后...

小魚兒 142

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【PAT_B】1012 数字分类(c/c++)

题目描述 给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字: A​1​​ = 能被 5 整除的数字中所有偶数的和; A​2​​ = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n​1−n​2+n​3−n​4⋯; A​3​​ = 被 5 除后余 2 的数字的个数; A​4​​ = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位; A​5​​ = ...

叶柖的博客 231

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

PAT乙级 1012 数字分类(20分)

1012 数字分类 (20 分) 输入格式: 每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。 输出格式: 输入样例 1: 13 1 2 3 4 5 6 7 8 9 10 20 16 18 输出样例 1: 30 11 2 9.7 9 输入样例 2: 8 1 2 4 5 6 7 9 16 输出样例 2: N 11 2 N 9 仅供参考 #include<stdio.h> int m

leigm 175

【PAT乙级】PAT (Basic Level) Practice (中文)训练记录

本博客用于记录PAT乙级题库做题新得

初见还是重逢的博客 4682

pat 甲级题目目录

A1001 A1003Emergency(25分) C++ ...

dutmathjc的博客 413

[PAT Basic Level] 1009、1010、1011、1012

1009. 说反话 题目分析: 这道题,难度不大,但是要写的精炼还需要熟练掌握一些库函数,如果纯靠自己自己编相应的代码会稍微复杂一些。我对这些库函数不太熟悉,自己写了一个一个,后来在网上看到了人家写的更便捷的实现,贴出来别人的优质代码和自己写的,做一个比较吧。 源代码(本人) #include <stdio.h> int main() { int sentence[81]; ...

weixin_44452361的博客 449

PAT乙级

题号 题目 题解 分类 备注

qq_42347617的博客 729

开始学习cpp——the first day of pat

看了看cpp的语法感觉和C很像,加上最近学习数据结构,看了很多C和CPP混用的代码,感觉还算得心应手。故有了学习cpp的打算。上手一门语言最快的就是用它来写东西,然而鉴于之前上来就写项目发现啥都不会导致了很深的挫败感- -……这次吸取教训,打算先把PAT乙级刷一遍。没学会走就想跑的行为都是耍流氓- -。。1011. A+B和C #include &lt;iostream&gt; using nam...

less_than_one的博客 228

浙大PAT 乙级(Basic Level) Practice 题解合集(全AC版)

写下这篇是为了记录一下在暑假这段时间里从零开始自解的PAT乙级基础题。

weixin_53024141的博客 1132

PAT练习题概览

更新至 1068 2013-10-08 更新至 1072 2013-11-07 PAT(pat.zju.edu.cn)是一个面向 C/C++程序的 Online Judge 系统。相比 ZOJ,HDOJ,POJ 等 ACM 题库,PAT 的题目非常基础,对于数据结构、算法的入门是比较有助益的。 本文按照自己的认识,给 PAT advanced level 做出了分类。由于很多题目实际

harmony的专栏 955

pat乙级题解目录

pat乙级题解目录 编号 标题 题解 分类 1001 害死人不偿命的(3n+1)猜想 (15) C++ 简单模拟 1002 写出这个数 (20) C++ 字符串处理 1003 我要通过!(20) C++ 数学 1004 成绩排名 (20) C++ 查找元素 1005 继续(3n+1)猜想 (25) C++ 散列 1006 换个格式输出整数 (15) C++ 字符...

日沉云起的博客 3885

PAT basic 练习题 C++ 代码合集及测试点错误解决方法

1001 害死人不偿命的(3n+1)猜想 (15分) 1002 写出这个数 (20分)

雨渊的博客 2455
上一篇: 【PAT】B1009. 说反话(C++接受带有空格的字符串)
下一篇: 【PAT】B1014. 福尔摩斯的约会(题目理解,输出格式)
timsooo
博客等级 码龄11年 16粉丝 61原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值