1012 数字分类 (20分) C++示例代码

1012 数字分类 (20分)

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:
在这里插入图片描述
输入格式:
每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。

输出格式:
对给定的 N 个正整数,按题目要求计算 A​1​​ ~A​5
​​ 并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出 N。

输入样例 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

思路:

这题…按要求写即可
注意:第2个可能返回值可能会为0,需注意,否则测试点8过不去

示例代码:

#include<iostream>
using namespace std;
int num[1001];
int N=0;
void A1(){
	int sum=0;
	int cnt=0;
	for(int i=0;i<N;i++){
		if(num[i]%10==0){
			cnt++;
			sum+=num[i];
		}
	}
	if(cnt==0)cout<<"N";
	else cout<<sum;
}

void A2(){
	int sum=0;
	int flag=1;
	int cnt=0;
	for(int i=0;i<N;i++){
		if(num[i]%5==1){
			cnt++;
			if(flag==1){
				sum+=num[i];
				flag=0;
			}else{
				sum-=num[i];
				flag=1;
			}
		}
	}
	if(cnt==0)cout<<"N";
	else cout<<sum;
}

void A3(){
	int cnt=0;
	for(int i=0;i<N;i++){
		if(num[i]%5==2){
			cnt++;
		}
	}
	if(cnt==0)cout<<"N";
	else cout<<cnt;
}

void A4(){
	int sum=0;
	int cnt=0;
	for(int i=0;i<N;i++){
		if(num[i]%5==3){
			sum+=num[i];
			cnt++;
		}
	}
	if(cnt==0)cout<<"N";
	else printf("%.1lf",double(sum)/cnt);
}

void A5(){
	int max=0;
	int cnt=0;
	for(int i=0;i<N;i++){
		if(num[i]%5==4){
			if(max<num[i]){
				max=num[i];
				cnt++;
			}	
		}
	}
	if(cnt==0)cout<<"N";
	else cout<<max;
}
int main(){
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>num[i];
	}	
	A1();cout<<" ";
	A2();cout<<" ";
	A3();cout<<" ";
	A4();cout<<" ";
	A5();
	return 0;
}
  • 1
    点赞
  • 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、付费专栏及课程。

余额充值