一下题解 均由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 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; }
这一年的母牛数=去年的母牛数+三年前的母牛数;
统计给定的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 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项的和。
Input输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。Output对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。Sample Input
数列的第一项为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.
Input
4
Output
2
Input
5
Output
-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); }