ACM競賽-516round4# 題解

一下題解 均由c++語言編寫    c語言的話頭三行改爲一行#include<stdio.h>  把cin cout 輸入輸出改爲scanf printf 格式   歡迎各位參加比賽吐舌頭

                          母牛的故事
有一頭母牛,它每年年初生一頭小母牛。每頭小母牛從第四個年頭開始,每年年初也生一頭小母牛。請編程實現在第n年的時候,共有多少頭母牛?
Input輸入數據由多個測試實例組成,每個測試實例佔一行,包括一個整數n(0<n<55),n的含義如題目中描述。 
n=0表示輸入數據的結束,不做處理。Output對於每個測試實例,輸出在第n年的時候母牛的數量。 
每個輸出佔一行。Sample Input
2
4
5
0
Sample 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;
}

這一年的母牛數=去年的母牛數+三年前的母牛數;

統計給定的n個數中,負數、零和正數的個數。
Input輸入數據有多組,每組佔一行,每行的第一個數是整數n(n<100),表示需要統計的數值的個數,然後是n個實數;如果n=0,則表示輸入結束,該行不做處理。Output對於每組輸入數據,輸出一行a,b和c,分別表示給定的數據中負數、零和正數的個數。Sample Input
6 0 1 2 3 -1 0
5 1 2 3 4 0.5
0 
Sample 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項的和。
Input輸入數據有多組,每組佔一行,由兩個整數n(n<10000)和m(m<1000)組成,n和m的含義如前所述。Output對於每組輸入數據,輸出該數列的和,每個測試實例佔一行,要求精度保留2位小數。Sample Input
81 4
2 2
Sample 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.

Input

The single line contains the positive integer n (1 ≤ n ≤ 1015).

Output

Print f(n) in a single line.

Example
Input
4
Output
2
Input
5
Output
-3
Note

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);



}












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