Input & output VII

计算两个32位整数A和B的和!

输入
输入数据有多组,每组为一行,包括两个32位整数。

输出
对每组输入,在单独的行中输出结果,每两组结果之间以单个空行分隔。

C语言:

#include <stdio.h>

int main()
{
	long long int A, B;
	int i=0;
	while (scanf("%lld  %lld", &A, &B) != EOF)
	{
		if(i==1){
			printf("\n");
		}
		printf("%lld\n", A + B);
		i=1;
	}
	return 0;	
}

Python:

flag = False
while True:
    try:
        m, n = map(int, input().split())
        if flag == True:
            print()

        print(m + n)
        flag = True

    except:
        break
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的手写数字识别的C++程序实现: ```c++ #include <iostream> #include <fstream> #include <vector> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; // 读取MNIST数据集 void readDataset(string imagesFile, string labelsFile, vector<Mat>& images, vector<int>& labels) { ifstream ifsImages(imagesFile, ios::binary); ifstream ifsLabels(labelsFile, ios::binary); if (!ifsImages.is_open() || !ifsLabels.is_open()) { cout << "Failed to open file." << endl; return; } int magicImages, magicLabels, numImages, numLabels; ifsImages.read((char*)&magicImages, sizeof(magicImages)); ifsLabels.read((char*)&magicLabels, sizeof(magicLabels)); ifsImages.read((char*)&numImages, sizeof(numImages)); ifsLabels.read((char*)&numLabels, sizeof(numLabels)); magicImages = reverseInt(magicImages); magicLabels = reverseInt(magicLabels); numImages = reverseInt(numImages); numLabels = reverseInt(numLabels); for (int i = 0; i < numImages; ++i) { Mat image = Mat::zeros(28, 28, CV_8UC1); for (int r = 0; r < 28; ++r) { for (int c = 0; c < 28; ++c) { unsigned char pixel; ifsImages.read((char*)&pixel, sizeof(pixel)); image.at<uchar>(r, c) = pixel; } } images.push_back(image); } for (int i = 0; i < numLabels; ++i) { unsigned char label; ifsLabels.read((char*)&label, sizeof(label)); labels.push_back((int)label); } } // 卷积操作 void convolve(Mat& input, Mat filter, Mat& output) { int filterSize = filter.rows; int pad = filterSize / 2; Mat paddedInput; copyMakeBorder(input, paddedInput, pad, pad, pad, pad, BORDER_CONSTANT, Scalar(0)); output.create(input.rows, input.cols, input.type()); for (int r = pad; r < paddedInput.rows - pad; ++r) { for (int c = pad; c < paddedInput.cols - pad; ++c) { double sum = 0.0; for (int fr = 0; fr < filterSize; ++fr) { for (int fc = 0; fc < filterSize; ++fc) { int ir = r - pad + fr; int ic = c - pad + fc; sum += paddedInput.at<double>(ir, ic) * filter.at<double>(fr, fc); } } output.at<double>(r - pad, c - pad) = sum; } } } // 池化操作 void pool(Mat& input, Mat& output) { output.create(input.rows / 2, input.cols / 2, input.type()); for (int r = 0; r < input.rows; r += 2) { for (int c = 0; c < input.cols; c += 2) { double maxVal = 0.0; for (int pr = 0; pr < 2; ++pr) { for (int pc = 0; pc < 2; ++pc) { double val = input.at<double>(r + pr, c + pc); if (val > maxVal) { maxVal = val; } } } output.at<double>(r / 2, c / 2) = maxVal; } } } // 全连接层操作 void fullyConnected(Mat& input, Mat& weight, Mat& bias, Mat& output) { output.create(1, weight.cols, CV_64FC1); for (int c = 0; c < weight.cols; ++c) { double sum = 0.0; for (int r = 0; r < weight.rows; ++r) { sum += input.at<double>(r) * weight.at<double>(r, c); } sum += bias.at<double>(0, c); output.at<double>(0, c) = sum; } } // Softmax操作 void softmax(Mat& input, Mat& output) { output.create(1, input.cols, CV_64FC1); double sum = 0.0; for (int c = 0; c < input.cols; ++c) { double expVal = exp(input.at<double>(0, c)); output.at<double>(0, c) = expVal; sum += expVal; } for (int c = 0; c < input.cols; ++c) { output.at<double>(0, c) /= sum; } } // CNN模型 void cnnModel(Mat& input, Mat& output) { // 卷积层1 Mat filter1 = (Mat_<double>(5, 5) << -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,24,24,24,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1); Mat conv1; convolve(input, filter1, conv1); // 激活函数ReLU Mat relu1; cv::max(0, conv1, relu1); // 池化层1 Mat pool1; pool(relu1, pool1); // 卷积层2 Mat filter2 = (Mat_<double>(5, 5) << -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,24,24,24,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1); Mat conv2; convolve(pool1, filter2, conv2); // 激活函数ReLU Mat relu2; cv::max(0, conv2, relu2); // 池化层2 Mat pool2; pool(relu2, pool2); // 全连接层1 Mat fcInput = pool2.reshape(1, 1); Mat weight1 = (Mat_<double>(fcInput.cols, 128) << ...); Mat bias1 = (Mat_<double>(1, 128) << ...); Mat fc1; fullyConnected(fcInput, weight1, bias1, fc1); // 激活函数ReLU Mat relu3; cv::max(0, fc1, relu3); // 全连接层2 Mat weight2 = (Mat_<double>(relu3.cols, 10) << ...); Mat bias2 = (Mat_<double>(1, 10) << ...); Mat fc2; fullyConnected(relu3, weight2, bias2, fc2); // Softmax softmax(fc2, output); } int main() { // 读取MNIST数据集 vector<Mat> images; vector<int> labels; readDataset("train-images-idx3-ubyte", "train-labels-idx1-ubyte", images, labels); // 训练CNN模型 for (int i = 0; i < images.size(); ++i) { Mat input = images[i] / 255.0; Mat output; cnnModel(input, output); // 计算损失函数 double loss = 0.0; for (int c = 0; c < output.cols; ++c) { int label = (c == labels[i]) ? 1 : 0; loss += -label * log(output.at<double>(0, c)); } // 反向传播更新权重和偏置 // ... } return 0; } ``` 需要注意的是,这只是一个简单的手写数字识别程序实现,具体的实现细节还需要根据实际需求进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐风—云端行者

喜欢请打赏,感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值