hdu1099 lottery 假分式 字符串函數 strncpy sprintf 函數 數字輸入字符數組 公約數


#include <cstdlib>  
#include <cstdio>  
#include <cassert>  
#include <cstring>  
#include <algorithm> 

long long GCD(long long a,long long b){
 if(b == 0)
  return a;
 
  return GCD(b,a%b);
}
int main(){
 long long n;
 while(scanf("%lld",&n) == 1 ){  //==2則表示輸入2個數
  long long don = 1,num = 0;
  for(  long long i=1;i<=n;++i){
   num = num * i + n * don ;
   don = don * i;
   long long gcd = GCD(don,num);
   don/=gcd;
   num/=gcd;
  }
  if(num%don == 0)
   printf("%lld\n",num/don);
  else{
   static char strNum[1024],strDon[1024],strInt[1024];
   static char strWS[1024],strLn[1024];
   static char ws[]="                              ";
   static char ln[]="------------------------------";
  sprintf(strNum,"%lld",num % don);
  sprintf(strInt,"%lld",num / don);
  sprintf(strDon,"%lld",don);
  
  memset(strWS,0,sizeof(strWS));
  strncpy(strWS,ws,strlen(strInt));

  memset(strLn,0,sizeof(strLn));
  strncpy(strLn,ln,std::max(strlen (strNum), strlen (strDon)));
              printf ("%s %s\n%s %s\n%s %s\n", strWS, strNum, strInt,strLn,  strWS, strDon);  
        
  }
 }
  return 0; 
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章