C++ 实现1012 数字分类

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int N;
    cin >> N;
    int num;
    int a1 = 0,a2 = 0,cnt1 = 0,square = 0,a3 = 0,cnt2 = 0,a5 = 0;
    float a4 = 0.0;
    while(cin>>num)
    {
        if(!(num % 5) && !(num % 2))
            a1 += num;
        else if(num % 5 == 1)
        {
            a2 += pow(-1,square++) * num;
            cnt1++;
        }
        else if(num % 5 == 2)
            a3++;
        else if(num % 5 == 3)
        {
            a4 += num;
            cnt2++;
        } 
        else if(num % 5 == 4)
            if(a5 < num)
                a5 = num;
    }
    if(!a1)
        cout<<"N ";
    else
        cout<<a1<<" ";
    if(!cnt1)
        cout<<"N ";
    else
        cout<<a2<<" ";
    if(!a3)
        cout<<"N ";
    else
        cout<<a3<<" ";
    if(!a4)
        cout<<"N ";
    else
        printf("%.1f ",a4 / cnt2);
    if(!a5)
        cout<<"N";
    else
        cout<<a5;   
    return 0;
}

以下文章使用了swith替代了我的各个if语句

1012 数字分类 (20 分)_Eccentric_t的博客-CSDN博客

里面最后转载的代码看着整洁,但是感觉使用数组,内存肯定有差别,可以自己对比下,我就不放对比图了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现CNN文本分类,您需要掌握以下步骤: 1. 数据预处理:将文本数据转换为数字形式,例如使用词袋模型或Word2Vec进行编码。 2. 构建卷积神经网络模型:CNN模型由卷积层、池化层、全连接层和输出层组成。您可以使用C++的深度学习库,如TensorFlow C++或Caffe C++来构建模型。 3. 模型训练:训练CNN模型需要大量的数据和计算资源。您可以使用GPU来加速训练过程。在训练过程中,您需要定义损失函数和优化器,并使用反向传播算法来更新模型参数。 4. 模型测试:在测试过程中,您可以使用测试数据集来评估模型的准确性和性能。 下面是一个使用TensorFlow C++实现CNN文本分类的示例代码: ```C++ #include <tensorflow/cc/client/client_session.h> #include <tensorflow/cc/ops/standard_ops.h> #include <tensorflow/core/framework/tensor.h> #include <tensorflow/core/framework/tensor_shape.h> #include <tensorflow/core/platform/env.h> using namespace tensorflow; using namespace tensorflow::ops; int main() { // 构建CNN模型 Scope root = Scope::NewRootScope(); auto input = Placeholder(root, DT_FLOAT, Placeholder::Shape({-1, 28, 28, 1})); auto conv1 = Conv2D(root, input, 32, {5, 5}, {1, 1}, "SAME"); auto relu1 = Relu(root, conv1); auto pool1 = MaxPool(root, relu1, {2, 2}, {2, 2}, "SAME"); auto conv2 = Conv2D(root, pool1, 64, {5, 5}, {1, 1}, "SAME"); auto relu2 = Relu(root, conv2); auto pool2 = MaxPool(root, relu2, {2, 2}, {2, 2}, "SAME"); auto flat = Flatten(root, pool2); auto fc1 = Dense(root, flat, 1024); auto relu3 = Relu(root, fc1); auto fc2 = Dense(root, relu3, 10); auto logits = fc2; // 定义损失函数和优化器 auto labels = Placeholder(root, DT_FLOAT, Placeholder::Shape({-1, 10})); auto cross_entropy = Mean(root, SoftmaxCrossEntropyWithLogits(root, logits, labels)); auto train_step = GradientDescentOptimizer(root, 0.5).Minimize(cross_entropy); // 初始化变量 ClientSession session(root); TF_CHECK_OK(session.Run({InitOp()})); // 训练模型 for (int i = 0; i < 1000; i++) { // 获取训练数据 Tensor input_tensor(DT_FLOAT, TensorShape({batch_size, 28, 28, 1})); Tensor label_tensor(DT_FLOAT, TensorShape({batch_size, 10})); // 填充数据 // 运行训练操作 TF_CHECK_OK(session.Run({{input, input_tensor}, {labels, label_tensor}}, {train_step})); } // 测试模型 // 获取测试数据 Tensor test_input_tensor(DT_FLOAT, TensorShape({test_size, 28, 28, 1})); Tensor test_label_tensor(DT_FLOAT, TensorShape({test_size, 10})); // 填充数据 // 运行测试操作 std::vector<Tensor> test_outputs; TF_CHECK_OK(session.Run({{input, test_input_tensor}, {labels, test_label_tensor}}, {logits}, &test_outputs)); auto test_logits = test_outputs[0].flat<float>(); // 计算准确率 return 0; } ``` 请注意,此示例仅供参考,您需要根据具体的数据集和任务调整模型结构和参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值