题目1:Number Sequence
题目描述:
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
#include <iostream>
using namespace std;
#define MaxSize 10000
int main(){
int a,b,n;
int f[MaxSize]={0};
f[1]=f[2]=1;
int i=0;
while(cin>>a>>b>>n,a||b||n){
for(i=3;i<MaxSize;i++){
f[i]=(a*f[i-1]+b*f[i-2])%7;
//周期性,i-2为一个周期
if(f[i]==1&&f[i-1]==1)
break;
}
n=n%(i-2);
f[0]=f[i-2];
cout<<f[n]<<endl;
}
return 0;
}
这道题要考虑到周期性,因为对7取余,所得数的范围在0~6之间,可能会有连续的两个数是1,这时候就出现了周期性,周期是i-2;数组不能设的长度太长,否则会出现“Memory Limit Exceeded”(内存超限)。
题目2:Elevator
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
Output
Print the total time on a single line for each test case.
Sample Input
1 2
3 2 3 1
0
Sample Output
17
41
#include <iostream>
using namespace std;
#define MaxSize 100
int main(){
int n;
int a[MaxSize]={0};
int sum=0;
while(cin>>n&&n!=0){
a[0]=0;
sum=0;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++){
if(a[i]>a[i-1])
sum+=(a[i]-a[i-1])*6+5;
else if(a[i]<a[i-1])
sum+=(a[i-1]-a[i])*4+5;
else
sum+=5;
//else
sum+=(a[i-1]-a[i])*4+5;
}
cout<<sum<<endl;
}
getchar();
return 0;
}
这道题只需要注意一个坑,那就是如果连着的两个数相同,也就是电梯不动,仍然要+5而不是+0.
在输入时,应该用while()循环,而不是if()判断只进行一次输入。
题目3:u Calculate e
题目描述:
A simple mathematical formula for e is
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
Sample Output
n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
#include <iostream>
using namespace std;
int fac(int n){
int sum=1;
for(int i=1;i<=n;i++){
sum=sum*i;
}
return sum;
}
int main(){
cout<<"n e"<<endl;
cout<<"- -----------"<<endl;
printf("0 1\n1 2\n2 2.5\n");
double num=2.5;
double e;
for(int i=3;i<10;i++){
e+=1.0*(1.0/fac(i));//应该用1.0/fac(i)。因为整数的除法只能得到整数,1/2=0;这是个坑!!!
num+=e;
cout<<i<<" ";
printf("%.9lf\n",num);
}
return 0;
}
这道题不难,只要解决阶乘和输出格式问题就好了,所要注意的就如代码注释里那样。
打卡第一天,虽然有点延迟了。