纪中集训2020.01.16【NOIP普及组】模拟赛C组——————【1.product】解析

纪中集训2020.01.16【NOIP普及组】模拟赛C组——————【1.product】解析

经典的题目有BUG的题目:

题目大意:纪中集训2020.01.16【NOIP普及组】模拟赛C组——————【1.product】解析_第1张图片
输入:
纪中集训2020.01.16【NOIP普及组】模拟赛C组——————【1.product】解析_第2张图片
输出:
在这里插入图片描述
样例输入
5
2 4 2 3 3
1 0 0 1 0
样例输出
59

附上本来AC Pascal代码:

var
        i,j,k,l,n,m:longint;
        a,b:array[-105..1000005]of longint;
        bz:array[-105..1000,-105..1000]of boolean;
        sum:int64;
begin
        assign(input,'product.in');
        reset(input);
        assign(output,'product.out');
        rewrite(output);
        readln(n);
        fillchar(bz,sizeof(bz),true);
        for i:=1 to n do
        begin
                read(a[i]);
        end;
        sum:=0;
        for i:=1 to n do
        begin
                sum:=sum+a[i]*a[i+1];
                bz[i,i+1]:=false;
                bz[i+1,i]:=false;
        end;
        bz[n,1]:=false;
        bz[1,n]:=false;
        sum:=sum+a[n]*a[1];
        for i:=1 to n do
        begin
                read(b[i]);
                if b[i]=1 then
                begin
                        for j:=1 to n do
                        begin
                                if (bz[i,j]=true) and (i<>j) then
                                begin
                                        sum:=sum+(a[i]*a[j]);
                                        bz[i,j]:=false;
                                        bz[j,i]:=false;
                                end;
                        end;
                end;
        end;
        writeln(sum);
        close(input);
        close(output);
end.

分析:题目实际上是有问题的,但是按照数据的意思是说,只用求一圈环,然后乘一下两个顶点,累加一下每个遍即可。(连我这种蒟蒻都觉得水呀!
附上 Pascal AC 代码:

var
        ans:int64;
        n,i:longint;
        a,b:array[0..1000005] of longint;
begin
        assign(input,'product.in');
        reset(input);
        assign(output,'product.out');
        rewrite(output);
        readln(n);
        for i:=1 to n do
        begin
                read(a[i]);
        end;
        for i:=1 to n do
        begin
                read(b[i]);
        end;
        for i:=1 to n do
        begin
                ans:=ans+a[i]*a[i-1];
        end;
        ans:=ans+a[1]*a[n];
        write(ans);
        close(input);
        close(output);
end.

附上C++ AC 代码:

#include
long long ans=0;
int a[100010];
using namespace std;
int main()
{
	int n;
	freopen("product.in","r",stdin);
	freopen("product.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	a[n+1]=a[1];
	for (int i=1;i<=n;i++)
	{
		ans+=a[i]*a[i+1];
	}
	printf("%lld",ans);
	fclose(stdin);
	fclose(stdout);
}

END!

下一题:纪中集训2020.01.16【NOIP普及组】模拟赛C组——————【2. binary】解析

你可能感兴趣的:(纪中集训2020.01.16【NOIP普及组】模拟赛C组——————【1.product】解析)