1. 程式人生 > >湖南大學第十四屆ACM程式設計新生杯(重現賽)a+b+c+d=?

湖南大學第十四屆ACM程式設計新生杯(重現賽)a+b+c+d=?

a+b+c+d=?

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld

題目描述

This is a very simple problem! Your only job is to calculate a + b + c + d!

輸入描述:

There are several cases.

In the first line, there is a single integer T.(T <= 200)

In the next T lines, each line contains four integers a, b, c and d(-2^61 <= a,b,c,d <=2^61)

輸出描述:

output T lines.

Each line output one integer represent the answer of a + b + c + d

示例1

輸入

1
1 2 3 4

輸出

10

%.0f 會四捨五入,貌似根據.後第一位是否大於4判斷。

floor函式 向下取整,即取不大於x的最大整數(與“四捨五入”不同,下取整是直接取按照數軸上最接近要求值的左邊值,即不大於要求值的最大的那個值)。

long double 是用 %Lf 輸出!


AC Code

c++ version

#include<bits/stdc++.h>
using namespace std;

long double a,b,c,d;
int main() {
	int t;
	scanf("%d",&t);
	while(t--) {
        scanf("%Lf%Lf%Lf%Lf",&a,&b,&c,&d);
        printf("%.0Lf\n",floor(a+b+c+d));	
	}
	return 0;
}

java version

import java.util.Scanner;
import java.math.BigDecimal;

public class Main
{
    public static void main(String args[]){
        int t=0;
        Scanner scan = new Scanner(System.in);
        t = scan.nextInt();
        for(int i=0;i<t;i++){
            BigDecimal a = new BigDecimal(scan.next().toString());
            BigDecimal b = new BigDecimal(scan.next().toString());
            BigDecimal c = new BigDecimal(scan.next().toString());
            BigDecimal d = new BigDecimal(scan.next().toString());
            System.out.println(a.add(b.add(c.add(d))));//不能直接加
        }
        scan.close();
    }
}

最騷的python version

t = int(input())
for case in range(t):
    number = list(map(int,input().split(' ')))
    print(sum(number))

Less taolu

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 131072K,其他語言262144K
64bit IO Format: %lld

題目描述

Less taolu, more sincerity.

This problem is very easy to solve.

You may be very tired during this contest. So we prepared a gift for you.

You just copy and paste this code and you will get AC!

Ctrl + C && Ctrl + V is a necessary skill for a programming ape.

#include<iostream>
using namespace std;
const long long mod = 1e9+7;
long long func(int x){
    if (x==1||x==0){
        return 1;
    }
    return (x*func(x-1)+(x-1)*func(x-2))%mod;
}
int n;
int main(){
    cin>>n;
    cout<<func(n);
    return 0;
}

輸入描述:

Input only a single
integer n.

輸出描述:

Please output
the answer by this code.

示例1

輸入

3

輸出

11

示例2

輸入

100

輸出

372497045

備註:

0<=N<=1e5

真*套路題,我還傻乎乎的交了一發,果然TEL

遞迴超時,記憶化一下即可。

#include<iostream>
using namespace std;
const long long mod = 1e9+7;
const int maxn = 1e5+10;
typedef long long ll;
ll vis[maxn];
long long func(int x){
    if (x==1||x==0){
        return 1;
    }
    if(vis[x]) return vis[x];
    return vis[x] = (x*func(x-1)+(x-1)*func(x-2))%mod;
}
int n;
int main(){
    cin>>n;
    cout<<func(n);
    return 0;
}

Sleepy Kaguya

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld

題目描述

Houraisan☆Kaguya is the princess who lives in Literally House of Eternity. However, she is very playful and often stays up late. This morning, her tutor, Eirin Yagokoro was going to teach her some knowledge about the Fibonacci sequence. Unfortunately, the poor princess was so sleepy in class that she fell asleep. Angry Eirin asked her to complete the following task:

This sequence can be described by following equations:
    1.F[1]=F[2]=1

    2.F[n]=F[n-1]+F[n-2]  (n>2)

 

Now, Kaguya is required to calculate F[k+1]*F[k+1]-F[k]*F[k+2] for each integer k that does not exceed 10^18.

Kaguya is so pathetic. You have an obligation to help her.

 

(I love Houraisan Kaguya forever!!!)

image from pixiv,id=51208622

輸入描述:

Input
Only one integer k.

輸出描述:

Output
Only one integer as the result which is equal to F[k+1]*F[k+1]-F[k]*F[k+2].

示例1

輸入

2

輸出

1

說明

F[2]=1,F[3]=2,F[4]=3

2*2-1*3=1

備註:

0 < k ≤ 1^18

If necessary, please use %I64d instead of %lld when you use "scanf", or just use "cin" to get the cases.

The online judge of HNU has the above feature, thank you for your cooperation.

資料太大本來想著矩陣快速冪,但是結果沒有取餘就存不下。註定是有規律的題

奇1偶-1

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long k;
    cin>>k;
    if(k%2) cout<<1<<endl;
    else cout<<-1<<endl;
    return 0;
}

Sleeping is a favorite of little bearBaby, because the wetness of Changsha in winter is too uncomfortable. One morning, little bearBaby accidentally overslept. The result of being late is very serious. You are the smartest artificial intelligence. Now little bearBaby  asks you to help him figure out the minimum time it takes to reach the teaching building.
The school map is a grid of n*m, each cell is either an open space or a building (cannot pass), and the bedroom of little bearBaby is at (1,1)—— the starting point coordinates.The teaching building is at (x, y)——the target point coordinates, he  can only go up, down, left or right, it takes 1 minute for each step. The input data ensures that the teaching building is reachable.


輸入描述:

The first line has two positive integers n, m , separated by spaces(1 <= n, m <= 100), n for the row, m for the column
Next there are two positive integers x, y, separated by spaces(1 <= x <= n, 1 <= y <= m) indicating the coordinates of the teaching building
Next is a map of n rows and m columns, 0  indicate a open space and 1  indicate a obstacles.

輸出描述:

For each test case, output a single line containing an integer giving the minimum time little bearBaby takes to reach the teaching building, in minutes.

示例1

輸入

5 4
4 3
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1

輸出

7

說明

For the input example, you could go like this:
(1,1)-->(1,2)-->(2,2)-->(2,3)-->(2,4)-->(3,4)-->(4,4)-->(4,3),so the minimum time is 7.

備註:

First grid in the upper left corner is(1,1)

BFS板子題

忘了vis陣列判重一下還能執行!但是記憶體超了w了一發,估計是佇列放滿了吧。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e2+10;
bool mp[maxn][maxn];
bool vis[maxn][maxn];
int dx[] = {1,-1,0,0,},dy[] = {0,0,1,-1};
int sx,sy;
int n,m;
struct unt {
	int x,y,step;
};
bool check(unt a) {
	if(a.x>0 && a.y>0 && a.x<=n && a.y<=m &&mp[a.x][a.y] == 0 && (!vis[a.x][a.y]) ) return 1;
	return 0;
}
int BFS() {
	queue<unt> q;
	unt cur,next;
	cur.x = 1;
	cur.y = 1;
	cur.step = 0;
	q.push(cur);
	while(!q.empty()) {
		cur = q.front();
		q.pop();
		for(int i=0; i<4; i++) {
			next.x = cur.x+dx[i];
			next.y = cur.y+dy[i];
			next.step = cur.step + 1;
			if(check(next)) {	
				if(next.x == sx && next.y == sy) {
					return next.step;
				}
				q.push(next);
				vis[next.x][next.y] = 1;
			}
		}
	}
	return 0;
}
int main() {

	cin>>n>>m;
	cin>>sx>>sy;
	for(int i = 1; i<= n; i++) {
		for(int j= 1; j<=m; j++) {
			cin>>mp[i][j];
		}
	}


	cout<<BFS()<<endl;
	return 0;
}

矩陣快速冪 參考 https://blog.csdn.net/dreamispossible/article/details/80413530

https://blog.csdn.net/MIKASA3/article/details/52087934