[DidaOJ][306] The Magical Number 9

Problem Description

Loading...

Problem Analysis

999 3
2 0
3 0
2 9
10000000000000000000000000000000000000000000000 1
2 8
10000000000000000000000000000000000000000000000 4
2 9
3 8
4 9
5 9

The principle is that if the sum of the digits in each place is divisible by 9
then the number itself is divisible by 9
Just update the record with each operation

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<string.h>
char s[1000000];
int main() {
	int Q;
	while(scanf("%s %d",s,&Q)!=EOF) {
		int x=0;
		int l=strlen(s);
		for(int i=0; i<l; i++)
			x=x+s[i]-'0';
		while(Q--) {
			int n,v;
			scanf("%d %d",&n,&v);
			x-=s[n-1]-'0';
			s[n-1]=v+'0';
			x+=v;
			printf("%d\n",x%9==0?1:0);
		}
	}

	return 0;
}

Problem link: 【DidaOJ】[306]Magical Number 9)

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...