1 条题解

  • 0
    @ 2024-12-24 9:49:41

    C :

    #include <string.h>
    #include <stdio.h>
    
    int a[102];
     
    int main()
    {
    	int n,m,c,max,i,j,f,p,res,t;
    	int a[100],o[100];
    
    	t=0;
    	while (1==1)
    	{
    		t++;
    		scanf("%d%d%d",&n,&m,&c);
    
    		if (n==0) break;
    	
    		for (i=1;i<=n;i++)
    		{
    			scanf("%d",&a[i]);
    		}
    
    		max=0;
    		memset(o,0,400);
    		res=0;
    
    		for (i=1;i<=m;i++)
    		{
    			scanf("%d",&p);
    			if (o[p]==0) o[p]=1; else o[p]=0; 
    
    			if (o[p]==1) 
    			{
    				res=res+a[p];
    				if (res>max) max=res;
    			}else res-=a[p];
    		}
    
    		printf("Sequence %d\n",t);
    	    if (max>c) {printf("Fuse was blown.\n\n"); continue;}
    
    		printf("Fuse was not blown.\n");
    		printf("Maximal power consumption was %d amperes.\n\n",max);
    
    	}
    
    	return 0;
    }
    //0 0 1 1 0 0 0 1
    //
    

    C++ :

    #include <cstdio>
    #include <vector>
    using namespace std;
    
    void solve(int n, int m, int tot) {
        vector < int > c(n + 1), op(m);
        vector < int > flag(n + 1, -1);
        for (int i = 1; i <= n; ++i)
            scanf("%d", &c[i]);
        for (int i = 0; i < m; ++i)
            scanf("%d", &op[i]);
        int sum = 0, ans = 0;
        for (int i = 0; i < m; ++i) {
            int k = op[i];
            flag[k] *= -1;
            sum += flag[k] * c[k];
            ans = max(ans, sum);
            if (sum > tot) {
                puts("Fuse was blown.");
                return;
            }
        }
        puts("Fuse was not blown.");
        printf("Maximal power consumption was %d amperes.\n", ans);
    }
    
    int main() {
        int n, m, tot, cas = 0;
        while (scanf("%d %d %d", &n, &m, &tot)) {
            if (0 == n && 0 == m && 0 == tot)
                break;
            printf("Sequence %d\n", ++cas);
            solve(n, m, tot);
            puts("");
        }
        return 0;
    }
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		int sequence = 1;
    		while (true) {
    			int n = scanner.nextInt();
    			int m = scanner.nextInt();
    			int c = scanner.nextInt();
    			if (n == 0 && m == 0 && c == 0) {
    				break;
    			}
    			int[] array = new int[n + 1];
    			for (int i = 1; i <= n; i++) {
    				array[i] = scanner.nextInt();
    			}
    			boolean[] array2 = new boolean[n + 1];
    			int count = 0;
    			boolean flag = false;
    			int max = 0;
    			for (int i = 0; i < m; i++) {
    				int index = scanner.nextInt();
    				if (array2[index]) {
    					array2[index] = false;
    					count -= array[index];
    				} else {
    					array2[index] = true;
    					count += array[index];
    					max = Math.max(max, count);
    					if (count > c) {
    						flag = true;
    					}
    				}
    			}
    			System.out.println("Sequence " + sequence);
    			if (flag) {
    				System.out.println("Fuse was blown.");
    			} else {
    				System.out.println("Fuse was not blown.");
    				System.out.println("Maximal power consumption was " + max
    						+ " amperes.");
    			}
    			System.out.println();
    			sequence++;
    		}
    		scanner.close();
    	}
    }
    
    • 1

    信息

    ID
    1013
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者