跳到正文

题解

题解:P16341 [科大国创杯初中组 2026] 乘积

ghjgjggj约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年4月22日
收录
2026年8月21日
更新
2026年8月21日
篇幅
约 1 分钟
获赞
1
题目
P16341

版本

  1. v1当前

这题纯属水题好吧

题目要求

题目要求其实就是找一段连续的数字,随后计算他们的乘积,如果和x相等,那么就输出,如果不存在,那么输出-1

方法

使用二重循环枚举所有连续子数组,随后计算乘积,如果相等直接 return ,否则在循环结束后输出 -1

代码

C++
#include <bits/stdc++.h>
#define maxn 1000    //预处理指令
using namespace std;
int n,x;
int a[maxn];
int main(){
	cin>>n>>x;
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<n;i++){     //二重循环枚举
		int prod=1;
		for(int j=i;j<n;j++){
			prod*=a[j];
			if(prod==x){
				cout<<i+1<<" "<<j+1<<endl;
				return 0;
			}if(prod>x) break;
		}
	}
	cout<<-1<<endl;
	return 0;
}

严禁无脑抄袭!!!

原文