令我不能理解
查看原帖
令我不能理解
1271781
zhangchenyi_awa楼主2024/5/30 21:56

源代码


#include <bits/stdc++.h>
using namespace std;
int n,m;
int a[100100];
int b[1100];
int x;
int main(){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=m;i++) b[i]=a[i];
	int q=m;
	while(1){
		for(int j=1;j<=m;j++) {
			b[j]-=1;
			if(b[j]==0){
				b[j]=a[++q];
			}
		}
		x++;
		bool FLAG=1;
		for(int i=1;i<=m;i++) if(b[i]>=0) FLAG=0;
		if(FLAG) {
			printf("%d",x);
			return 0;
		}
	}
}

此代码是错误的


正确代码

#include <bits/stdc++.h>
using namespace std;
int n,m;
int a[100100];
int b[1100];
int x;
int main(){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=m;i++) b[i]=a[i];
	int q=m;
	while(1){
		for(int j=1;j<=m;j++) {
			b[j]-=1;
			if(b[j]==0){
				b[j]=a[++q];
			}
		}
		x++;
		bool FLAG=1;
		for(int i=1;i<=m;i++) if(b[i]>=0) FLAG=0;
	if(FLAG) {
		printf("%d",x-1);
		return 0;
	}

	}
}

为何在

		if(FLAG) {
			printf("%d",x-1);
			return 0;
		}

加一个减一就对了呢

2024/5/30 21:56
加载中...