considering the number as long is it enough?? i think the number may be as long as 1000000 digits such as 11..1 {there's 1000000 number'1'}. test: 123 answer is 4 be carefull on test 16 when the last char is '0' the strlen of char will been dec() for 123, is the answer suppose to be 3? #include <iostream> #include <string> using namespace std; int main() { string a; long p=0,max=0,i; cin >> a; long n=a.size(); for(i=0; i<n; i++) { if(a[i]>='0' && a[i]<='9'){ p=p+(a[i]-'0'); if(a[i]-'0'>max) max=a[i]-'0'; } else { p=p+(a[i]-'A'+10); if(a[i]-'A'+10>max) max=a[i]-'A'+10;} } if(max==0) { i=0; cout << "2" << endl;} else { for(i=max+1; i<=36; i++) if(p%(i-1)==0){ cout << i << endl; break; }} if(i==37) cout << "No solution." << endl; return 0; } Because you read the whole string to a variable, and then loop through that string. You must use C. It's more quick . No C++. Just C. In the statements, string_length = 10^6, but your string_size can be maximum 256, write like this, and you will get AC : #include <iostream> #include <string> using namespace std; string a; long p,maxx,i,n; int main() { cin >> a; n=a.size(); for(i=0; i<n; i++) { if(a[i]>='0' && a[i]<='9'){ p=p+(a[i]-'0'); if(a[i]-'0'>maxx) maxx=a[i]-'0'; } else { p=p+(a[i]-'A'+10); if(a[i]-'A'+10>maxx) maxx=a[i]-'A'+10;} } if(maxx==0) { i=0; cout << "2" << endl;} else { for(i=maxx+1; i<=36; i++) if(p%(i-1)==0){ cout << i << endl; break; }} if(i==37) cout << "No solution." << endl; return 0; } No need to use complicated input styles. Just include cstdio as a header file and use scanf instead. You dont need to switch over to a C file. You can simply use C functions from C++. Regards Nitin 2 change code and AC:-) /* #include <iostream> #include <string> using namespace std; int main() { string a; long p=0,max=0,i; while(cin >> a)//1 { p=0;max=0;//2 long n=a.size(); for(i=0; i<n; i++) { if(a[i]>='0' && a[i]<='9'){ p=p+(a[i]-'0'); if(a[i]-'0'>max) max=a[i]-'0'; } else { p=p+(a[i]-'A'+10); if(a[i]-'A'+10>max) max=a[i]-'A'+10;} } if(max==0) { i=0; cout << "2" << endl;} else { for(i=max+1; i<=36; i++) if(p%(i-1)==0){ cout << i << endl; break; }} if(i==37) cout << "No solution." << endl; } return 0; } */ Admins:What is 12th test? Be careful! The input string may contain spaces and/or line breaks! #include <stdio.h> #include <ctype.h> int main() { long num=0,k,v,max=1; char ch; ch=getchar(); while(ch!=EOF) { if(isdigit(ch)) v=ch-'0'; else v=ch-'A'+10; num+=v; if(v>max) max=v; ch=getchar(); } for(k=max;k<36;k++) if(num%k==0) break; if(k<36) printf("%ld",k+1); else printf("No solution."); return 0; } > #include <stdio.h> > #include <ctype.h> > int main() > { > long num=0,k,v,max=1; > char ch; > ch=getchar(); > while(ch!=EOF) > { > if(isdigit(ch)) v=ch-'0'; > else v=ch-'A'+10; > num+=v; > if(v>max) max=v; > ch=getchar(); > } > for(k=max;k<36;k++) > if(num%k==0) break; > if(k<36) printf("%ld",k+1); > else printf("No solution."); > return 0; > } > > #include <stdio.h> > > #include <ctype.h> > > int main() > > { > > long num=0,k,v,max=1; > > char ch; > > ch=getchar(); > > while(ch!=EOF) > > { > > if(isdigit(ch)) v=ch-'0'; > > else v=ch-'A'+10; > > num+=v; > > if(v>max) max=v; > > ch=getchar(); > > } > > for(k=max;k<36;k++) > > if(num%k==0) break; > > if(k<36) printf("%ld",k+1); > > else printf("No solution."); > > return 0; > > } NOT EOF BUT '\n' #include <stdio.h> #include <ctype.h> long num=0,v,max=1,k; char ch; int main() { ch=getchar(); do { if(isdigit(ch)) v=ch-48; else v=ch-55; num+=v; if(v>max) max=v; ch=getchar(); } while(ch!='\n'); if(max==0) printf("2"); else { for(k=max;k<36;k++) if(num%k==0) break; if(k<36) printf("%ld",k+1); else printf("No solution."); } return 0; } Edited by author 28.03.2008 20:19 I tested my program several times but I can't find what's wrong with it please help ! my program : #include<iostream> #include<stdio.h> using namespace std; int main() { char a[1000000]; cin>>a; int i; int counter=0; int maxint=0; for(i=0;i<strlen(a);i++) { if(a[i]>64){ if(maxint<a[i]-55) maxint=a[i]-55; counter+=a[i]-55; } else { if(maxint<a[i]-48) maxint=a[i]-48; counter+=a[i]-48; } } for(i=1;i<36;i++) { if((counter%i==0)&&(maxint<i)) { cout<<i+1; return 0; } } cout<<"No solution."; return 0; } Edited by author 07.11.2007 07:13 Edited by author 07.11.2007 07:13 Can somebody help me with my solution - i don't know where is my mistake? As you can see i always get WA on test 8. #include<iostream> #include<string> using namespace std; string s; char c='0'; int base,st; int f(int b) { long long sol=0; for(int i=0;i<s.size();i++) if(s[i]>='0' && s[i]<='9') sol=(sol*b+int(s[i]-'0'))%(b-1); else if(s[i]>='A' && s[i]<='Z') sol=(sol*b+int(c-'A')+10)%(b-1); if(sol==0) return 1; else return 0; } int main() { while(c!='\n') {scanf("%c",&c); if((c>='0' && c<='9' ) || (c>='A' && c<='Z'))s+=c;} c='0'; for(int i=0;i<s.size();i++) { if(int(c)<int(s[i])) c=s[i]; } if(c>='0' && c<='9') st=int(c-'0'); else if(c>='A' && c<='Z') st=int(c-'A')+10; if(st==0) {printf("2\n"); return 0;} for(int i=st+1;i<=36;i++) if(f(i)==1) { printf("%d\n",i); return 0; } printf("No solution.\n"); return 0; } On the 1st test output is wrong !!! k must be minimal -> 4, not 22 !! k cannot be 4 since the number contains digit A which is 10 thus K=4 is absurd Here is my solution! I can't find my mistake! Edited by author 03.09.2007 22:01 All ok with your code/ You must output "No solution." instead of "No solution" You forget point "." here. Edited by author 28.07.2007 20:38 Edited by author 28.07.2007 20:41 Thank you very much!!! :) Please give me some more test cases! <code deleted> Edited by author 20.02.2007 19:37 please give me some test cases WA 7 As you can see all the problems of the Tetrahedron Team Contest (1100-1107) are under invesigation. We add some tests for this problem. 86 authors lost AC after rejudge. Good luck for all authors catching bugs in their solutions! My AC stands too:), but what's new tests were add? Edited by author 06.01.2007 22:37 WA 4#.Any one,please,tell me the reason.Thank you! var a:array[1..10] of integer; i,j,k,n,max:longint; s,m:int64; c:char; function num(c:char):integer; begin if (ord(c)>=48) and (ord(c)<=57) then num:=ord(c)-48; if (ord(c)>=65) and (ord(c)<=90) then num:=ord(c)-55; end; begin n:=0;max:=0; while not eof do begin while not eoln do begin read(c); inc(n); a[n]:=num(c); if a[n]>max then max:=a[n]; end; readln; end; if max<1 then max:=1; for k:=max+1 to 36 do begin s:=0; for i:=n downto 1 do begin m:=1; for j:=1 to n-i do m:=m*k; s:=s+m*a[i]; end; if s mod (k-1) =0 then begin writeln(k); halt; end; end; writeln('No solution.'); end. This task was like a nightmare. I tryed 12 times and finally i've got AC. BUt my biggest mistake was not a lack of mathematic thinking but the issue that i try'ed to cin>> this big table. Finally, I tryed to get it one char after char and that was a good way. It is only one number "0". I got "Crash" the first time. The right answer is "2". May be, you've used some strange algorithm... Help me please my English is not good can you translate me this problem?! Edited by author 05.02.2006 17:36 Edited by author 05.02.2006 17:36 |
|