【题一】求n!
//用的是万进制ε=ε=ε=(~ ̄▽ ̄)~
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main ()
{
int n;
int a[20100];
while(cin>>n)
{
memset(a,0,sizeof(a));
a[0] = 1;
int num = 0;
for(int i=2; i<=n; i++)
{
int add=0;
for(int j=0; j<=num; j++)
{
int t = a[j]*i+add;
a[j] = t%10000;
add = t/10000;
}
if(add>0)
{
num++;
a[num] = add;
}
}
cout<<a[num];
for(int i=num-1;i>=0;i--)
{
printf("%04d",a[i]); //之前学的都忘了。。
//C中的输入输出还是hin有用滴!!多多注意!!
}
cout<<endl;
}
return 0;
}
【题二】浮点数加法
HDU 1753
(没写完捏<( _ _ )>还剩最后0的输出处理,emm,以后有时间再说~~)
//用的是字符串模拟,好麻烦的说-_-
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
int a1[405],a2[405],b1[405],b2[405];
int c1[405],c2[405];
string s1,s2;
while(cin>>s1>>s2)
{
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
memset(b2,0,sizeof(b2));
memset(b1,0,sizeof(b1));
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
int len1 = s1.size();
int len2 = s2.size();
int dot1 = s1.find('.');
int dot2 = s2.find('.');
// cout <<dot1<<','<<len1<<endl;
// cout <<dot2<<','<<len2<<endl;
if(dot1==-1)
dot1 = len1;
if(dot2==-1)
dot2 = len2;
int k = 0;
for(int i=dot1-1; i>=0; i--)
{
a1[k] = s1[i]-'0';
k++;
}
k=0;
for(int i=dot1+1; i<len1; i++)
{
a2[k] = s1[i]-'0';
k++;
}
k=0;
for(int i=dot2-1; i>=0; i--)
{
b1[k] = s2[i]-'0';
k++;
}
k=0;
for(int i=dot2+1; i<len2; i++)
{
b2[k] = s2[i]-'0';
k++;
}
///开始模拟相加
int l2 = max(len1-dot1,len2-dot2);
k=0;
int flag = 0;
for(int j=l2-2; j>=0; j--)
{
c2[k] += a2[j]+b2[j];
if(c2[k]>9 && j!=0)
{
c2[k+1] +=c2[k]/10;
c2[k] = c2[k]%10;
}
if(c2[k]>9 && j==0)
{
flag=1;
c2[k] = c2[k]%10;
}
k++;
}
int l1 = max(dot1,dot2);
for(int j=0; j<l1; j++)
{
c1[j] += a1[j]+b1[j];
if(c1[j]>9)
{
c1[j+1] += c1[j]/10;
c1[j] = c1[j]%10;
}
}
if(flag == 1)
c1[0]++;
for(int k=l1; k>=0; k--)
{
if(c1[k]==0 && k==l1)
continue;
else
cout<<c1[k];
}
if(dot1!=len1 || dot2!=len2)
cout<<'.';
for(int j=l2-2; j>=0; j--)
cout<<c2[j];
cout<<endl;
}
}