A - Adjacent Product

2024-03-24 10:52
文章标签 product adjacent

本文主要是介绍A - Adjacent Product,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题陈述

您将得到 N 个整数 A[1],A[2],…,A[n]​ 。另外,定义 b[i]]=a[i]×a[i+1] (1≤i≤n−1)Bi​=Ai​×Ai+1​ (1≤i≤N−1) 。

按顺序打印 b[1],b[2],…,b[n-1]​ ,用空格分隔。

数据范围

  • 2≤N≤100

  • 1≤a[i]≤100

所有输入值均为整数。

输入

输入来自标准输入,格式如下:

N

A[1]  A[2]  ...  A[N]

输出

按顺序打印 B1​,B2​,…,BN−1​ ,用空格分隔。

样本输入1

3
3 4 6

 

示例输出1

12 24

我们有 B1​=A1​×A2​=12,B2​=A2​×A3​=24 。

代码和解析:

#include<bits/stdc++.h> // 引入标准库,包含了C++的多数头文件  
using namespace std;    // 使用标准命名空间,这样可以直接使用标准库中的类、函数等,无需std::前缀  const int N = 100007;   // 定义一个常量N,表示数组a的最大长度  
int n;                  // 定义一个变量n,用于存储将要输入的整数的数量  
int a[N];               // 定义一个整型数组a,用于存储输入的整数  int main()             // 主函数入口  
{  cin >> n;          // 从标准输入读取一个整数n,表示后续将要输入的整数数量  // 循环读取n个整数,并将它们存储在数组a中  // 注意:这里数组是从a[1]开始使用的,而不是通常的a[0]  for(int i = 1; i <= n; i++) cin >> a[i];  // 循环计算并打印相邻整数的乘积  // 注意:这里是从a[1]开始到a[n-1]结束,因为a[n]不存在(数组越界)  for(int i = 1; i <= n - 1; i++) cout << a[i] * a[i + 1] << " ";  return 0; // 主函数返回0,表示程序正常结束  
}  /*  * 解析:  * 1. 程序首先读取一个整数n,表示后续要输入的整数数量。  * 2. 然后,程序读取n个整数,并将它们存储在数组a中,从a[1]开始存储。  * 3. 接下来,程序计算并打印数组a中相邻整数的乘积,即从a[1]和a[2]的乘积开始,  *    一直到a[n-1]和a[n](如果存在的话)的乘积结束。但由于数组只到a[n],所以实际上计算的是a[n-1]和a[n]之前的元素乘积。  * 4. 每个乘积之间用空格分隔,最后打印出所有乘积。  *  * 注意:  * - 由于数组a是从a[1]开始使用的,而不是通常的a[0],这在实际编程中并不常见,可能会造成混淆。  * - 在实际编程中,通常建议从数组的第一个元素a[0]开始使用,以符合C++的惯例。  * - 另外,这段代码没有进行错误处理,比如输入的n是否超过了数组a的大小。在实际应用中,应该加上相应的错误处理机制。  */

这篇关于A - Adjacent Product的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/841378

相关文章

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

如何将Product依赖的LibraryModule导出成jar

在Android Studio新建Module时可以选择创建的module是工程module还是Android Library。 或者可以在工程module中的build.gradle文件中将 apply plugin: 'com.android.application'改为apply plugin: 'com.android.library' 同时将applicati

[LeetCode] 238. Product of Array Except Self

题:https://leetcode.com/problems/product-of-array-except-self/description/ 题目 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all

CodeForces 487C Prefix Product Sequence

题意: 构造一个1~n的排列  使得n个前缀积%n是一个0~n-1的排列 思路: 首先确定n一定放最后  要不然会有%n会有多个0  这时n-1位置的前缀积为(n-1)! 接着讨论n  n为合数时只有n=4有解  因为如果n为合数一定可以拆成p*q的形式  明显pq|(n-1)! 然后构造ai=(i+1)*inv[i]  因为(i+1)*inv[i] == (j+1)*inv[j]时一

Subtract the Product and Sum of Digits of an Integer

Given an integer number n, return the difference between the product of its digits and the sum of its digits. Example 1: Input: n = 234Output: 15 Explanation: Product of digits = 2 * 3 * 4 = 24

caffe源码解析-inner_product_layer

打开inner_product_layer.hpp文件,发现全连接层是非常清晰简单的,我们主要关注如下四个函数就行。 LayerSetUp(SetUp的作用一般用于初始化,比如网络结构参数的获取)ReshapeForward_cpuBackward_cpu ** inner_product_layer.hpp ** namespace caffe {template <typename

Largest Palindrome Product问题及解法

问题描述: Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. 示例: Input: 2 Output: 987 E

产品为何总是做不好 (六): Product Owner 惯性的行为

2016.9.3,  北京, Ken Fang 回顾这近二十年的敏捷、软件工程的旅程,我的收获是相当的丰富的;尤其是我面对面了许多不同层级的部门领导、数千位的团队成员, 使我能不断的验证了 “人类惯性的行为“ 对团队开发效率与产品质量 (品味)的影响。 1. 当 Product Owner 惯性的行为, 只是希望能在某月某日交付版本。 2. 当 Product Owner 惯性的行为,

【PAT】【Advanced Level】1009. Product of Polynomials (25)

1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A*B where A and B are two pol