What the hell Ans to 0 not 10,but 0 cause it whole number Your task is to find the minimal POSITIVE integer import java.util.*; import java.io.*; import java.lang.String; public class P1014{ public static void main(String[] args){ Scanner in=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); long m=0; int i=0; String num; try{ int n=in.nextInt(); if(n==1) out.println("1"); else if(n<10) out.println("1"+n); else { for(i=9;i>1;i--) { if(n%i==0) { n=n/i; m=m*10+i; i++; }} if(n!=1) out.print("-1"); else{ String s=Long.toString(m); for(i=s.length()-1;i>=0;i--) out.print(s.charAt(i)); }} }catch(Exception e){ out.println(e); } out.flush(); }} For C++ code and other queqtions write imran_yusubov@yahoo.com Edited by author 10.09.2008 06:38 I think Mister Bean,too . Timus Online Judge is playground for intelligence of student information technology. Learn algorithm to solve is main. thank you Mister Bean, you help me very much. Edited by author 28.03.2010 18:31 yes, it will be 1 just in case, for 0 it will be 10 #include<iostream> using namespace std; int main() { int j,i,temp=0,p=0,a[10]; long long int n,k; cin>>n; if (n/10==0) { cout<<1<<n; p=1; }
for(i=9,k=n,j=0;i>1&&p==0;) { if(k%i==0) { a[j++]=i; k=k/i; temp=1; } else i--; } for(i=--j;temp==1&&i>=0&&p==0;i--) { cout<<a[i]; } if (temp==0) cout<<-1; system("PAUSE"); return(0); }
system("PAUSE"); Don't use system pause. Edited by author 12.11.2009 09:02Here is my code - #include<stdio.h> int main() { __int64 n; int m[100],i=0,j=0;
printf("Enter the number\t"); scanf("%lld",&n);
if(n==1) { j=1; printf("Result is %d\n",j); } else if(n == 0) { j=10; printf("Result is %d\n",j); } else if(n < 10) { j=n; printf("Result is %d\n",j); } else { for(i=9;i>1;i--) { if(n%i == 0) { n=n/i; m[j++]=i; i++; } } if(n!=1) { j=-1; printf("Result is %d\n",j); } else { for(i=j-1;i>=0;i--) printf("%d",m[i]); } }
return 0; } It gives the correct output, i have preformed following test cases -- Input->output 0 -> 10 1 -> 1 2 -> 2 3 -> 3 4 -> 4 5 -> 5 6 -> 6 7 -> 7 8 -> 8 9 -> 9 10 -> 25 11 -> -1 12 -> 26 13 -> -1 14 -> 27 15 -> 35 16 -> 28 17 -> -1 18 -> 29 19 -> -1 20 -> 45 21 -> 37 22 -> -1 23 -> -1 24 -> 38 25 -> 55 26 -> -1 27 -> 39 28 -> 47 29 -> -1 30 -> 56 31 -> -1 32 -> 48 33 -> -1 34 -> -1 35 -> 57 36 -> 49 37 -> -1 38 -> -1 39 -> -1 40 -> 58 41 -> -1 42 -> 67 43 -> -1 44 -> -1 45 -> 59 46 -> -1 47 -> -1 48 -> 68 49 -> 77 50 -> 255 51 -> -1 52 -> -1 53 -> -1 54 -> 69 55 -> -1 56 -> 78 57 -> -1 58 -> -1 59 -> -1 60 -> 256 61 -> -1 62 -> -1 63 -> 79 64 -> 88 65 -> -1 66 -> -1 67 -> -1 68 -> -1 69 -> -1 70 -> 257 71 -> -1 72 -> 89 73 -> -1 74 -> -1 75 -> 355 76 -> -1 77 -> -1 78 -> -1 79 -> -1 80 -> 258 81 -> 99 82 -> -1 83 -> -1 84 -> 267 85 -> -1 86 -> -1 87 -> -1 88 -> -1 89 -> -1 90 -> 259 91 -> -1 92 -> -1 93 -> -1 94 -> -1 95 -> -1 96 -> 268 97 -> -1 98 -> 277 99 -> -1 100 -> 455 still i am getting WA#1, I don't know what the hell is wrong in that. Can any body tell me, why am i getting WA#1?? if(n==1) { j=1; printf("Result is %d\n",j); } else if(n == 0) { j=10; printf("Result is %d\n",j); } else if(n < 10) { j=n; printf("Result is %d\n",j); if(n!=1) { j=-1; printf("Result is %d\n",j); No need to printf "Result is"! Please, help me with this problem. I don't know why WA. here my program (JAVA): import java.util.Scanner; public class Solution implements Runnable{ Scanner in;
public void run(){ try{ in = new Scanner (System.in); int n1 = in.nextInt(); int n = n1; if (n == 0){ System.out.print(10); } else{ if (n == 1){ System.out.print(n); } else{ int[]mass = new int [n1];
for (int i = 0; i < n1; i++){ mass[i] = 0; }
int j = 0;//указатель массива mass
for (int i = 9; i > 1; i--){ boolean f = true; while (f){ if (n % i == 0){ n = n / i; mass[j] = i; j++; //System.out.print(i + " "); } if (n % i != 0){ f = false; } } } if (n >= 11){ System.out.print(-1); } else{ for (int i = n1 - 1; i >= 0; i--){ if (mass[i] != 0){ System.out.print(mass[i]); } } } } }
} catch( Exception er){ er.printStackTrace(); System.exit(-1); } }
public static void main(String[] args) { new Thread (new Solution()).start(); } } the test 3 is: number: 1 answer 1 but is 11... Why? for example in the test 4 the number is 0 and the answer is 10 !!! so that the product of digits of Q is exactly equal to N. exactly!!! and my program is AC but no the test... my english no is very good... I can't discuss it much (: < ) Edited by author 05.04.2007 02:20 Edited by author 05.04.2007 02:20 Edited by author 05.04.2007 02:21 where do you see the tests? I want to be able too see the test cases. tanks! i'm sure about my program... i tested it for anynumber between 1 and 100000000..... Edited by author 30.08.2008 02:09 Edited by author 30.08.2008 02:09 It is 1: [code is deleted] Edited by moderator 10.08.2009 13:15 Accepted,For 1,it is 1. [code delete] Edited by author 10.08.2009 13:12 what should I print if the number is a prime number Hi, I think it should print -1 but for 7, 5 etc. answer would be 7 and 5 respectively. For 11, it will be -1. Edited by author 12.03.2009 23:22 void rec(int n){ if (n<10){ out.print(n); }else{ boolean locFind = false; for (int i = 9; i>1; i--){ if (n % i==0){ locFind = true; rec(n/i); out.print(i); break; } } if (!locFind){ out.print(-1); out.close(); } } }
void solve(){ int n = in.nextInt(); if (n==0){ out.print(10); }else{ rec(n); } } #include <iostream> #include <set> using namespace std; int main() {multiset<int> ms; int i, n; cin>>n; if(n==0) {cout<<10; return 0;} if(n==1) {cout<<1; return 0;} for(i=9; i>=2; i--) while(n%i==0) {ms.insert(i); n/=i; } if(n>1) {cout<<-1; return 0;} else for(multiset<int>::iterator it=ms.begin(); it!=ms.end(); it++) cout<<*it; return 0; } //--------------------------------------------------------------------------- Edited by author 08.11.2008 01:39 This is a stupid trick, since q = 0 is not considered positive. Hope it saves a few minutes of your life. Oh, thanks man! It would take me years to guess! Thanks Dan Stefan. I must be more attentively for statement of a problem Thank you thank you very much! this case wasted me 10^n minutes... 太不好了!底些! <the line above, Chinese, which you can ignore it.> Thank you man! You saved my life :) thank you! :) I think that integers must be > 0, but there stay 0<=N<=... Edited by author 21.04.2008 20:58 Edited by author 21.04.2008 20:59 WA test#1 but it works normally on my computer actually i don't understand what is the matter of it? give me some more tests: HERE my program: #include <iostream> using namespace std; int a[30],contr=0,n,s=0; int main() { int i; cin>>n; for(i=0;i<100;i++) a[i]=0; int f(int x); bool pr=true; while(n>=10) { f(n); contr++; if(contr!=s) { cout<<-1<<endl; n=5; pr=false; } } if(n==0) { cout<<"10"<<endl; pr=false; } if(pr) { a[s]=n; for(i=s;i>=0;i--) cout<<a[i]; cout<<endl; } return 0; } int f( int k) { int i; for(i=9;i>1;i--) if(k%i==0) { n/=i; a[s++]=i; break; } return n; } Check your program once again.It is giving wrong answer to sample input.It is outputing 10 for sample input where i make mistake #include <iostream.h> int main(){ int n,q; cin>>n; if (n==0) {q=10; cout<<q; goto tu;} if(n<100); { for(int x=1;x<10;x++) { for(int y=1;y<10;y++) { q=x*y; if(q==n) {cout<<x<<y; goto tu; }}}}
if((n>=100) && (n<1000)) { for(int x=1;x<10;x++) {for(int y=1;y<10;y++) {for(int o=1;o<10;o++) { q=x*y*o; if(q==n) {cout<<x<<y<<o; goto tu; }}}}} if((n>=1000) && (n<10000)) {for(int x=1;x<10;x++) {for(int y=1;y<10;y++) {for(int o=1;o<10;o++) {for(int a=1;a<10;a++) { q=x*y*o*a; if(q==n) {cout<<x<<y<<o<<a; goto tu; }}}}}} if((n>=10000) && (n<100000)) {for(int x=1;x<10;x++) {for(int y=1;y<10;y++) {for(int o=1;o<10;o++) {for(int a=1;a<10;a++) {for(int b=1;b<0;b++) { q=x*y*o*a*b; if(q==n) {cout<<x<<y<<o<<a<<b; goto tu; }}}}}}} if((n>=100000) && (n<1000000)) {for(int x=1;x<10;x++) {for(int y=1;y<10;y++) {for(int o=1;o<10;o++) {for(int a=1;a<10;a++) {for(int b=1;b<0;b++) {for(int c=1;c<10;c++) { q=x*y*o*a*b*c; if(q==n) {cout<<x<<y<<o<<a<<b<<c; goto tu; }}}}}}}} if((n>=1000000) && (n<10000000)) {for(int x=1;x<10;x++) {for(int y=1;y<10;y++) {for(int o=1;o<10;o++) {for(int a=1;a<10;a++) {for(int b=1;b<0;b++) {for(int c=1;c<10;c++) {for(int d=1;d<10;d++) { q=x*y*o*a*b*c*d; if(q==n) {cout<<x<<y<<o<<a<<b<<c<<d; goto tu; }}}}}}}}} if((n>=10000000) && (n<100000000)) {for(int x=1;x<10;x++) {for(int y=1;y<10;y++) {for(int o=1;o<10;o++) {for(int a=1;a<10;a++) {for(int b=1;b<0;b++) {for(int c=1;c<10;c++) {for(int d=1;d<10;d++) {for(int e=1;e<10;e++) { q=x*y*o*a*b*c*d*e; if(q==n) {cout<<x<<y<<o<<a<<b<<c<<d<<e; goto tu; }}}}}}}}}} cout<<-1; tu: ; system("pause"); return 0; } Your program goes wrong for this test case 1000000000.The output is 555555555888 Your Output is -1. I've finally managed to get the ACCEPTED message and i'm quite satisfied with that... had to change 2-3 conceipt ideas... At the end may be not the best i could write but please tell me what do you think about the source: #include <stdio.h> int main(void) { unsigned long int aa; scanf("%ld",&aa); int dvoiki=0,troiki=0,petici=0,sedmici=0,chetvorki=0,devetki=0,shestici=0,osmici=0; switch(aa){ case 0: printf("10"); break; case 1: printf("1"); break; default: for (int j=2;j<=9;j++)if(aa%j==0) {switch (j){ case 2: dvoiki++; break; case 3: troiki++; break; case 5: petici++; break; case 7: sedmici++; break; } aa=aa/j--; } osmici=dvoiki/3; dvoiki=dvoiki%3; devetki=troiki/2; troiki=troiki%2; if ((dvoiki>1)&&(troiki>0)) {dvoiki--;troiki--;shestici++;} if ((dvoiki>0)&&(troiki>2)) {dvoiki--;troiki--;shestici++;} if ((dvoiki>0)&&(troiki>0)) {dvoiki--;troiki--;shestici++;}
chetvorki=dvoiki/2; dvoiki=dvoiki%2;
if (aa>7) printf("-1"); else{ for(int i=1;i<=dvoiki;i++)printf("2"); for(int i=1;i<=troiki;i++)printf("3"); for(int i=1;i<=chetvorki;i++)printf("4"); for(int i=1;i<=petici;i++)printf("5"); for(int i=1;i<=shestici;i++)printf("6"); for(int i=1;i<=sedmici;i++)printf("7"); for(int i=1;i<=osmici;i++)printf("8"); for(int i=1;i<=devetki;i++)printf("9"); } } return 0; } I think C# RULIT using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1014 { class Program { static void Main(string[] args) { string result = ""; int num = int.Parse(Console.ReadLine()); if (num == 1) Console.WriteLine("1"); else { if (num == 0) Console.WriteLine("10"); else { int[] digitArr = new int[10]; for (int i = 9; i > 1; i--) { while (num % i == 0) { digitArr[i]++; num /= i; } } if (num != 1) Console.WriteLine("-1"); else { for (int i = 2; i < 10; i++) { for (int j = 0; j < digitArr[i]; j++) result += i.ToString(); } Console.WriteLine(result); } } } } } } #include <iostream.h> bool next(unsigned long int n) { for (unsigned short int i=9;i>1;i--) if (n%i==0&&(n/i>9?next(n/i):i+=n/i*10)) { cout << i; return 1; } return 0; } int main() { unsigned long int n; cin >> n; if (n<10) cout << (n==0?10:n); else if (!next(n)) cout << -1; return 0; } Do you like it as much as i do? :) I don't understand your solution. Can you explaint to me. :) good good. I had the same approach when I read it at first. Very easy solution. Very nice, indeed. I wish i had thought of that :) Nevertheless i've solved the problem in my own way :D I've tried 0 it gives 10 I've tried 1 it gives 1 I've tried nos <10 and >1 gives the same I've tried characters and strings gives -1 I've tried 1000 000 000 it gives 555 555 555 888 I've tried 12 It gives 26 Not 34 I've used the method of coming back from 9 To 1 dividing the number.. Please tell what is Test #8 Thanks for 12 test It really helped me If you still don't know how to solve - 426287475 Hey Got the Mistake... I declared the array small.... That is the array that is used to store the digits... I think I know whats the problem. My program had error on T#8 too. and it couldn't work for 100000000. try 910 = 13 * 7 * 5 * 2 answer is -1 try 26 I've got WA on test 2, test 4 and test 6. I did'nt know why, but my problem was sorting;) my quicksort algorithm was really strange - it didn't sort;) Now i've got AC 0.015 I don't think we need to do additional sorting as long as you always pickup the biggest factor each time. Correct? |
|