#include<stdio.h> #include<string.h> int main() { char a[120][20]={"i","ii","iii","iv","v","vi","vii","viii","ix","x","xi","xii","xiii","xiv","xv","xvi","xvii","xviii","xix","xx","xxi","xxii","xxiii","xxiv","xxv","xxvi","xxvii","xxviii","xxix","xxx","xxxi","xxxii","xxxiii","xxxiv","xxxv","xxxvi","xxxvii","xxxviii","xxxix","xl","xli","xlii","xliii","xliv","xlv","xlvi","xlvii","xlviii","xlix","l","li","lii","liii","liv","lv","lvi","lvii","lviii","lix","lx","lxi","lxii","lxiii","lxiv","lxv","lxvi","lxvii","lxviii","lxix","lxx","lxxi","lxxii","lxxiii","lxxiv","lxxv","lxxvi","lxxvii","lxxviii","lxxix","lxxx","lxxxi","lxxxii","lxxxiii","lxxxiv","lxxxv","lxxxvi","lxxxvii","lxxxviii","lxxxix","xc","xci","xcii","xciii","xciv","xcv","xcvi","xcvii","xcviii","xcix","c"}; int num,i,j; while(scanf("%d",&num)==1) { if(num==0) { break; } int c1=0,c2=0,c3=0,c4=0,c5=0; for(i=0;i<num;i++) { for(j=0;j<strlen(a[i]);j++) { if(a[i][j]=='i') c1++; else if(a[i][j]=='v') c2++; else if(a[i][j]=='x') c3++; else if(a[i][j]=='l') c4++; else if(a[i][j]=='c') c5++; } } printf("%d: %d i, %d v, %d x, %d l, %d c\n",num,c1,c2,c3,c4,c5); } return 0; }
Tuesday, October 4, 2011
uva : 344- Roman Digititis
uva :382 - Perfection
#include<stdio.h> int main() { int n; printf("PERFECTION OUTPUT\n"); while(scanf("%d",&n)==1) { if(n==0) break; int i,j,k,l,m,sum=0; for(i=1;i<=n/2;i++) { if(n%i==0) sum=sum+i; } if(sum==n) { printf("%5d PERFECT\n",n); } else if(sum<n) { printf("%5d DEFICIENT\n",n); } else { printf("%5d ABUNDANT\n",n); } } printf("END OF OUTPUT\n"); return 0; }
ACM - UVA 371 - Ackermann Functions
Ackermann Functions |
#include<string.h>
int main()
{
long long l,u;
while(scanf("%lld %lld",&l,&u)==2)
{
if(l==0&&u==0)
break;
long long count=0,i,j,k,m,n,max=0;
if(u<l)
{
i=l;
l=u;
u=i;
}
for(i=l;i<=u;i++)
{
m=i;count=0;
while(m!=0)
{
count++;
if(m%2==0)
{
m=m/2;
}
else
{
m=3*m+1;
}
if(m==1)
break;
}
if(count>max)
{
max=count;
n=i;
}
}
printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n",l,u,n,max);
}
return 0;
}
uva : 369- Combinations
#include<stdio.h> int main() { long n,m; while(scanf("%ld %ld",&n,&m)==2) { if(n==0&&m==0) break; else if(n==0&&m==1) { printf("0 things taken 1 at a time is 0 exactly.\n"); } else{ long num1[1000],lnum1=1,i,j,k,l,r=0,div[1000],ldiv=0; long sum=1; num1[0]=1; l=n-m; if(l<m) { for(i=m+1;i<=n;i++) { for(j=0;j<lnum1;j++) { k=(num1[j]*i+r)%10; r=(num1[j]*i+r)/10; num1[j]=k; } while(r!=0) { num1[lnum1++]=r%10; r=r/10; } } for(i=2;i<=l;i++) { sum=sum*i; } } else { for(i=l+1;i<=n;i++) { for(j=0;j<lnum1;j++) { k=(num1[j]*i+r)%10; r=(num1[j]*i+r)/10; num1[j]=k; } while(r!=0) { num1[lnum1++]=r%10; r=r/10; } } for(i=2;i<=m;i++) { sum=sum*i; } } j=0;k=0; for(i=lnum1-1;i>=0;i--) { k=(num1[i]+r*10)/sum; r=(num1[i]+r*10)%sum; if(k>0) { j=1; } if(j==1) { div[ldiv++]=k; } } printf("%ld things taken %ld at a time is ",n,m); for(i=0;i<ldiv;i++) { printf("%ld",div[i]); }printf(" exactly.\n"); } } return 0; }
uva : 324 - Factorial Frequencies
#include<stdio.h> int main() { int n; while(scanf("%d",&n)==1) { if(n==0) break; int num1[3000],lnum1=1,i,j,k,l,m,r=0,lnum2=0,num2[10]; for(i=0;i<10;i++) { num2[i]=0; } num1[0]=1; for(i=2;i<=n;i++) { for(j=0;j<lnum1;j++) { m=(num1[j]*i+r)%10; r=(num1[j]*i+r)/10; num1[j]=m; } while(r!=0) { num1[lnum1++]=r%10; r=r/10; } } for(i=lnum1-1;i>=0;i--) { m=num1[i]; num2[m]=num2[m]+1; } printf("%d! --\n ",n); for(i=0;i<10;i++) { printf("(%d)%5d ",i,num2[i]); if(i==4) { printf("\n "); } }printf("\n"); } return 0; }
uva : 299 - Train Swapping
#include<stdio.h> int main() { int no; scanf("%d",&no); while(no!=0) { int n; scanf("%d",&n); int m[n+1],i,j,k,l,count=0; for(i=0;i<n;i++) { scanf("%d",&m[i]); } for(i=0;i<n;i++) { for(j=1;j<n;j++) { if(m[j]<m[j-1]) { k=m[j]; m[j]=m[j-1]; m[j-1]=k; count++; } } } printf("Optimal train swapping takes %d swaps.\n",count); no--; } return 0; }
uva : 272 - TeX Quotes
#include<stdio.h> #include<string.h> int main() { /* freopen("input.txt","r",stdin);*/ char a; int n=0; while(scanf("%c",&a)==1) { int i,j,k,l,m; if(a=='"') { n++; if(n%2==1) { printf("``"); } else { printf("''"); } } else { printf("%c",a); } } return 0; }
uva : 160 - Factors and Factorials
#include<stdio.h> #include<math.h> int main() { int n; while(scanf("%d",&n)==1) { if(n==0) break; int num1[3000],lnum1=1,i,j,k,l,m,r=0,prime[100],lprime=0; int num2[100],lnum2=0,div[1000],ldiv=0,count=0; num1[0]=1; for(i=2;i<=n;i++) { for(j=0;j<lnum1;j++) { m=(num1[j]*i+r)%10; r=(num1[j]*i+r)/10; num1[j]=m; } while(r!=0) { num1[lnum1++]=r%10; r=r/10; } } for(i=2;i<=n;i++) { m=0; for(j=2;j<=sqrt(i);j++) { if(i%j==0) m++; } if(m==0) { prime[lprime++]=i; } } ldiv=0; for(i=lnum1-1;i>=0;i--) { div[ldiv++]=num1[i]; } count=0; for(i=0;i<lprime;i++) { k=0;r=0;lnum1=0; for(j=0;j<ldiv;j++) { m=(div[j]+r*10)/prime[i]; r=(div[j]+r*10)%prime[i]; if(m>0) { k=1; } if(k==1) { num1[lnum1++]=m; } } if(r==0) { count++; i=i-1; ldiv=0; for(j=0;j<lnum1;j++) { div[ldiv++]=num1[j]; } } else { num2[lnum2++]=count; count=0; } } printf("%3d! =",n); j=0; for(i=0;i<lnum2;i++) { if(j>14) { printf("\n "); j=0; } j++; printf("%3d",num2[i]); }printf("\n"); } return 0; }
uva : 136 - Ugly Number
#include<stdio.h> int main() { printf("The 1500'th ugly number is 859963392.\n"); }
uva : 113 - Power of Cryptography
#include<stdio.h> #include<math.h> int main() { double n,p; while(scanf("%lf %lf",&n,&p)==2) { printf("%.0lf\n",(pow(p,1/n))); } return 0; }
uva: 100 - 3n+1 problem
#include<stdio.h> void funtion(int n1,int n2); int main() { int n1,n2; while(scanf("%d %d",&n1,&n2)==2) { funtion(n1,n2); } return 0; } void funtion(int n1,int n2) { int i,m1,m2,j,k,l,m=0,n=1,c=0; m1=n1; m2=n2; if(m1>m2) { i=m1; m1=m2; m2=i; } for(i=m1;i<=m2;i++) { k=i;n=1; while(k!=1) { if(k%2==0) { k=k/2; } else { k=k*3+1; } n++; } if(n>m) { m=n; } } printf("%d %d %d\n",n1,n2,m); }
Subscribe to:
Posts (Atom)