山东省高校组队训练邀请赛——AC题目(4AC:3水题,1扩展欧几里得)

按AC顺序

问题 J: Too Expensive to Buy a House

时间限制: 1 Sec  内存限制: 128 MB
提交: 141  解决: 101
[ 提交][ 状态][ 讨论版]

题目描述

WNJXYK and DIDIDI are good friends . One day, WNJXYK found DIDIDI bought a house, so he also wanted to buy a house. Because the price of house is rising continuously, so WNJXYK hope that he can buy a house before he couldn’t afford it. Currently, a house need $A, the price increases by $B per mouth, and WNJXYK only have a deposit of $C. Your assignment is to calculate in how many months should WNJXYK buy the house before he couldn’t afford it. 

输入

The first line of input contains a positive integer T telling you there are T test cases followed.
Each test case will contain 3 integers, A, B, C.

输出

For each test case, print a line “Case #x: y”, where x is the case number (starting from 1) and y is an integer indicating the number of months. 

样例输入

3
1 2 3
1 2 6
1 2 1

样例输出

Case #1: 1Case #2: 2Case #3: 0

提示


Tips:1≤T≤20,1≤A≤C≤1e9, 1≤B≤1e6

Case 1: after 1 month, price will be 3

Case 2: after 2 months, price will be 5, after 3 months, price will be 7, and he can’t afford it.

Case 3: after 1 month, price will be 3, he can’t afford it.

题意:水题

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define fin freopen("in.txt", "r", stdin)
#define line printf("\n-----------------\n")
#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);
using namespace std;
//const long long mod = 1000000007;
const int maxn = 10000+ 5;
 
int t, ans, a, b, c;
 
int main(){
    cin >> t;
    for(int k = 1; k <=t; k++){
        cin >> a>> b >> c;
        ans = (c - a) / b;
 
        printf("Case #%d: %d\n", k, ans);
    }
}


问题 I: WNJXYK loves to take a shower

时间限制: 1 Sec  内存限制: 128 MB
提交: 145  解决: 100
[提交][状态][讨论版]

题目描述

WNJXYK often takes a shower in school public bathroom. The bathroom has many cabinets. WNJXYK found that the number of the cabinet is strange. No number contains digit ‘4’, in other words, numbers like ‘14’,’34’,’456’ don’t exist. So cabinet 5 is actually the 4th cabinet. We know WNJXYK’s number is N. Your assignment is to calculate his cabinet’s real number.

输入

The first line of input contains a positive integer T telling you there are T test cases followed.
Each test case will contain one integer.

输出

For each test case, print a line “Case #x: y”, where x is the case number (starting from 1) and y is an integer indicating real number of WNJXYK’s cabinet.

样例输入

2

5

15

样例输出

Case #1: 4

Case #2: 13

提示

Tips:1≤T≤20,1≤N≤10000

Case 1: 1 2 3 5

Csse 2: 1 2 3 5 6 7 8 9 10 11 12 13 15

题意:去除含有4的数字,输入一个数字,输出它的号

解决:sprintf(str,“%d”, i);数字转字符串

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define fin freopen("in.txt", "r", stdin)
#define line printf("\n-----------------\n")
#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);
using namespace std;
//const long long mod = 1000000007;
const int maxn = 10000+ 5;
 
int t, ans, n, a[maxn];
 
 
bool f(int i){
    char str[10];
    sprintf(str, "%d", i);
    for(int j = 0; j < strlen(str); j++){
        if(str[j] == '4') return false;
    }
    return true;
}
int main(){
    int num = 0;
    char str[10];
    int flag = 4;
    int flag2 = 40;
    for(int i = 1; i <= 10000; i++){
        if(f(i)){ num++; a[i] = num;}
        else a[i] = num;
    }
    cin >> t;
    for(int k = 1; k <=t; k++){
        cin >> n;
        printf("Case #%d: %d\n", k, a[n]);
    }
}

问题 F: shovel snow

时间限制: 1 Sec  内存限制: 128 MB
提交: 151  解决: 73
[提交][状态][讨论版]

题目描述

Northeast China snows every year, so school asks freshmen to shovel snow. This tradition lasts for many years. This year WNJXYK becomes the principle, he thinks that asking freshmen to shovel snow every year is boring, so he hopes that the grade who shovel snow can be different every year. The number of the grades is N, in order to illustrate, we assume N=5 here. WNJXYK hopes that in every 5-year, grade 1 shovels snow in the first year, grade 2 the second year, grade 3 the third year, and so on. We can use a permutation 12345 to express it. But in this circumstance DIDIDI find a problem that the grade 1 in the first year is exactly the grade 2 in the second year, so this grade shovels snow for many times in fact. 
So DIDIDI use a permutation 13524, that is, first year - grade 1, second year – grade 3, third year – grade 5, fourth year – grade 2, fifth year – grade 4. In this case, every grade need to shovel snow exactly once. In more detail, we assume the first year is 2015. The grade 1 in first year is The Class of 2015. The grade 3 in second year is The Class of 2014. The grade 5 in third year is The Class of 2013. The grade 2 in forth year is The Class of 2017.  The grade 4 in fifth year is The Class of 2016. So every grade need to shovel snow exactly once. 
For every N given, you need to give a permutation with length N, which can satisfy the requirement that every grade shovels snow exactly once. If there are multiple answers, please give the one with the largest lexicographical order.

输入

The first line of input contains a positive integer T telling you there are T test cases followed.
Each test case will contain one integer N .

输出

For each test case, print two lines.
The first line is “Case #x:”, where x is the case number (starting from 1)。
The second line contains n integers, indicating a n-permutation. If threr is no answer, print “-1”.

样例输入

1

3

样例输出

Case #1:

3 2 1

提示

Tips:1≤T≤20,1≤N≤10000

Case 1: we assume the first year is 2015. The grade 3 in first year is The Class of 2013. The grade 2 in second year is The Class of 2015. The grade 1 in third year is The Class of 2017. The grade 3 in fourth year is The Class of 2016. The grade 2 in fifth year is The Class of 2018. The grade 1 in sixth year is The Class of 2020. The grade 3 in seventh year is The Class of 2019. And so on.

题意:学校每年要喊学生扫雪,学校有N级学生,每级只扫一次,输出字典序最大。

解决:坑题,找规律——奇数就是从大到小输出,偶数就没有解决方案输出-1.

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define fin freopen("in.txt", "r", stdin)
#define line printf("\n-----------------\n")
#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);
using namespace std;
//const long long mod = 1000000007;
const int maxn = 10000+ 5;
 
int t, n;
 
int main(){
    cin >> t;
    for(int k = 1; k <= t; k++){
        cin >> n;
        printf("Case #%d:\n", k);
        if( !(n % 2)) printf("-1\n");
        else{
            for(int i = n; i > 1; i--){
            printf("%d ", i);
            }
            printf("%d\n", 1);
        }
 
    }
}

问题 H: DIDIDI loves to take a shower

时间限制: 1 Sec  内存限制: 128 MB
提交: 75  解决: 6
[提交][状态][讨论版]

题目描述

DIDIDI often takes a shower in school public bathroom. DIDIDI must take a shower in B days after the previous one, or he will die. For routine maintenance, bathroom closes one day per A days. But DIDIDI is lazy, he hopes he can take a shower as less as possible. So he wants to find a stable period for arranging dates of shower. For example, DIDIDI should take a shower every 3-day, and bathroom closes every Sunday. In order to minimize the shower times, in every two-week, DIDIDI can choose Monday, Thursday, Saturday (to avoid Sunday), Tuesday, Friday to take shower. In this case, he need to take a shower 2.5 times per week. Your assignment is to calculate how many times per A days DIDIDI need to take a shower.

输入

The first line of input contains a positive integer T telling you there are T test cases followed.
Each test case will contain two integer, A, B.

输出

For each test case, print a line “Case #x: y”, where x is the case number (starting from 1) and y is times per A days of taking a shower. (if y is not a integer, please print fraction like “a/b”, gcd(a,b) = 1)

样例输入

2
7 3

7 4

样例输出

Case #1: 5/2

Case #2: 2

提示

Tips:1≤T≤2000,2≤A,B≤1e8

Case 1: if bathroom closes in 7th day every 7 days, he can take a shower in 1st,4th, 6th, 2nd,5th,1st, 4th, 6th, 2nd,5th …… every period contains five shower times and two 7-day, so answer is 5/2.

Csse 2: if bathroom closes in 7th day every 7 days, he can take a shower in 3rd,6th, 3rd, 6th ……every period contains two shower times and a 7-day, so answer is 2.

题意:不是水题咯!澡堂每A天关一次门,这个人必须B天之内洗一次澡,问每A天最少洗几次澡。

解决:扩展欧几里得算法:a*x+b*y=gcd(a,b)

对于这个题,gcd == 1 的时候才会用到这个来求x,y。 但是得出的只有其中一个(x,y),其中一个为负数,而且题目中必须 b*y - a*x == 1.公式得出来的可能是 -1.

得不出答案,书中写到有多组解,解等于 x+ka' , y-kb' (a' = a / gcd(a,b) , b' = b / gcd(a,b) ,k为整数).

但是, 只有a>0才不符合情况,a只能每次减b。

详情看代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define fin freopen("in.txt", "r", stdin)
#define line printf("\n-----------------\n")
#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);
using namespace std;
//const long long mod = 1000000007;
const int maxn = 10000+ 5;
int gcd(int a, int b){
    return a % b == 0 ? b : gcd(b, a % b);
}
int t, n, a0, b0, num, ans;
long long num2;
void gcd2(int a, int b, int &d, int &x, int &y)
{
    if(!b){d = a; x = 1; y = 0;}
    else {gcd2(b, a % b, d, y, x); y -= x * (a / b);}
}
int  extgcd(int a, int b, int &x, int &y){
    int d = a;
    if(b != 0){
        d = extgcd(b, a%b, y, x);
        y -= (a/b)*x;
    }else {
        x = 1; y = 0;
    }
    return d;
}
int main(){
    cin >> t;
    for(int k = 1; k <=t ; k++)
    {
        cin >> a0 >> b0;
        int x, y, d = 1;
        int g = gcd(a0,b0);
        if(g == 1){
            extgcd(a0, b0, x, y);
//            cout << "***" << x << " " << y << endl;
            int xx = abs(x), yy = abs(y);
            if(xx * a0 < yy * b0){
                if(xx == 1)
                    cout << "Case #" << k <<": "<< yy << endl;
                else
                    cout << "Case #" << k <<": "<< yy << "/" << xx << endl;
            }
            else{
                while(abs(x) * a0 >= abs(y) * b0){
                    y = y + a0;
                    x = x - b0;
                }
                xx = abs(x);
                yy = abs(y);
 
                if(xx == 1)
                    cout << "Case #" << k <<": " << yy << endl;
                else
                    cout << "Case #" << k <<": "<< yy << "/" << xx << endl;
            }
        }
        else{
            if(b0/g == 1){
                cout << "Case #" << k <<": "<< a0/g << endl;
            }
            else{
                cout << "Case #" << k <<": "<< a0/g << "/" << b0/g << endl;
            }
 
        }
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值