n=0表示輸入數據的結束,不做處理。Output對於每個測試實例,輸出在第n年的時候母牛的數量。
每個輸出佔一行。Sample Input
2 4 5 0Sample Output
2 4 6
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,i;
int a[60];
a[0]=0;
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
while(scanf("%lld",&n))
{
if(n==0)break;
if(n<5)
cout<<a[n]<<endl;
else
{
for(i=5;i<=n;i++)
{
a[i]=a[i-1]+a[i-3];
}
cout<<a[n]<<endl;
}
}
return 0;
}
這一年的母牛數=去年的母牛數+三年前的母牛數;
6 0 1 2 3 -1 0 5 1 2 3 4 0.5 0Sample Output
1 2 3 0 0 5
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
double b;ll a,c,d,e;
while(scanf("%lld",&a))
{ c=0;d=0;e=0;
if(a==0)break;
while(a>0)
{
scanf("%lf",&b);
if(b>0)d++;
if(b<0)c++;
if(b==0)e++;
a--;
}
printf("%lld %lld %lld\n",c,e,d);
}
}
數列的第一項爲n,以後各項爲前一項的平方根,求數列的前m項的和。
81 4 2 2Sample Output
94.73 3.41
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll b,d,e;
double c,a;
while(~scanf("%lf %lld",&a,&b))
{ c=a;
for(int i=1;i<b;i++)
{
a=sqrt(a);
c+=a;
}
printf("%.2lf\n",c);
}
}
For a positive integer n let's define a function f:
f(n) = - 1 + 2 - 3 + .. + ( - 1)nn
Your task is to calculate f(n) for a given integer n.
InputThe single line contains the positive integer n (1 ≤ n ≤ 1015).
Print f(n) in a single line.
4
2
5
-3
f(4) = - 1 + 2 - 3 + 4 = 2
f(5) = - 1 + 2 - 3 + 4 - 5 = - 3
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll a,b,c,d;
scanf("%lld",&a);
b=a/2;
c=b-a;
if(a%2==0)
printf("%lld",b);
else printf("%lld",c);
}