刷题笔记11.19-11.24 english

Balloon Popping

 Balloon popping is a fun game for kids. Now n balloons are positioned in a line. The goal of the game is very simple: to pop as many balloons as possible. Here we add a special rule to this game -- that is, you can only make ONE jump. Assume that a smart baby covers his/her body by thorns(刺), jumps to some position and lies down (as shown by the figures below), so that the balloons will be popped as soon as they are touched by any part of the baby's body. Now it is your job to tell the baby at which position he/she must jump to pop the most number of balloons.

 

Input Specification:

Each input file contains one test case. For each case, two positive integers are given in the first line: n (≤105), the number of balloons in a line, and h (≤103), the height of the baby with his/her arms stretched up. Then n integers are given in the next line, each corresponds to the coordinate of a balloon on the axis of the line. It is guaranteed that the coordinates are given in ascending order, and are all in the range [−106,106].

Output Specification:

Output in a line the coordinate at which the baby shall jump, so that if the baby jumps at this position and then lie down, the maximum number of the balloons can be popped beneath his/her body. Then also print the maximum number of balloons that can be popped. If the coordinate is not unique, output the smallest one.

The numbers must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

11 120
-120 -40 0 80 122 140 160 220 240 260 300

Sample Output:

120 5

Note: jumping at any position from 120 to 140, or from 240 to 260 can pop 5 balloons, hence 120 is printed as the smallest one.


 错误示范

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
short N,h,mx;
int mx_h;
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>N>>h;
    int ballon[N];
    for(int i=0;i<N;i++)
		cin>>ballon[i];
	for(int i=1;i<N;i++){
		for(int j=0;j<i;j++){
			if(ballon[j] > ballon[i]-h && mx< i - j+1){
				mx = i - j+1 ;    
				mx_h = ballon[i]-h;    //错误且 TLE
			}
		}
	}
	cout<<mx_h<<" "<<mx;
	return 0;
}

正确

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int N, h;
    cin >> N >> h;
    vector<int> balloon(N);
    for (int i = 0; i < N; i++) 
        cin >> balloon[i];

    int mx = 0, mx_h = balloon[0];
    for (int i = 0; i < N; i++) {
        int popped = 0;
        int j = i;
        while (j >= 0 && balloon[j] >= balloon[i] - h) {    //关键行
            popped++;
            j--;
        }
        if (popped > mx) {
            mx = popped;
            mx_h = balloon[i] - h;
        }
    }
    cout << mx_h << " " << mx << endl;
    return 0;
}

Consecutive Factor

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1<N<231).

Output Specification:

For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]*factor[2]*...*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:

630

Sample Output:

3
5*6*7

 测试点就过了2???


Shuffling Machine

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, ..., S13, 
H1, H2, ..., H13, 
C1, C2, ..., C13, 
D1, D2, ..., D13, 
J1, J2

where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for "Diamond", and "J" for "Joker". A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

Sample Input:

2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47

Sample Output:

S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5

#include <bits/stdc++.h>
using namespace std;
vector<int> card(55),action(55);
int n;

const string suits[5] ={"S", "H", "C", "D", "J"}; //牌顺序

void shuffling(vector<int>& cards, const vector<int>& action) {    //vector   洗牌方法
    vector<int> temp(55);
    for (int i = 1; i < 55; ++i) {
        temp[action[i]] = cards[i];    //嫌麻烦 直接都从一开始
    }
    cards = temp;    //直接赋值
}
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin>>n;
	for(int i=1;i<55;i++)    //洗牌顺序
		cin >>action[i];
	for(int i=1;i<55;i++){    //初始化牌  新买一幅牌
		card[i]=i;
	}
	for(int i=0;i<n;i++){    //洗牌
		shuffling(card,action);
	}
	for(int i=1;i<55;i++){
		cout<<suits[(card[i]-1)/13]<<(card[i]-1)%13+1;
        //先输出花色,card是从1开始的,suits从0开始的...  -1后/13
        //后输出牌点数,card从1开始...哦,和这个无关.   一花色13张牌 ,一第13张牌不是下一花色的开始    例如S13在card里是第13个  取模13成0了...所以减一...我在干啥...
		if(i !=54) cout<<" ";//最后不让有空格
	}
	return 0;
}

 Have Fun with Numbers

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
现在,您应该检查此属性是否有更多数字。也就是说,将给定的数字与 k 数字加倍,您将判断结果数字是否仅由原始数字中数字的排列组成。

Input Specification:

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
每个输入包含一个测试用例。每个案例包含一个不超过 20 位数字的正整数。

Output Specification:

For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.
对于每个测试用例,如果将输入数字加倍得到的数字仅由原始数字中的数字排列组成,则首先打印一行“是”,如果不是,则打印“否”。然后在下一行中,打印双倍的数字。

Sample Input:

1234567899

Sample Output: 

Yes
2469135798

有问题的:

#include <bits/stdc++.h>
using namespace std;
vector<int> num,cnt(10),cnt_yes(10);
string in;
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin>>in;
	for(int i=0;i<in.size();i++){
		cnt[in[i]-'0']++;
		num[i] = in[i]-'0';
	}
	for(int i=0;i<in.size();i++){
		num[i]*=2;
		if(num[i]>=10){
			num[i]-=10;
			num[i+1]++;    //逻辑错误 进位+1还*2
		}
	}
	for(int i=0;i<in.size();i++)
		cnt_yes[num[i]-'0']++;
	if(cnt ==cnt_yes)
		cout<<"Yes";
	else cout<<"No";
	for(int i=0;i<in.size();i++){
		cout<<num[i];
	}
	return 0;
}

int 改成   size_t

在C++中,std::string 的 size() 函数返回的是 std::string::size_type 类型,它是一个无符号整数类型。而在代码中,你使用有符号的 int 类型进行了比较,导致编译器发出警告。

为了解决这些警告,你可以将循环索引的类型改为 std::string::size_type,或者使用无符号整数类型来保持一致。

还是有2个测试点没过?  寄,

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    string in;
    cin >> in;

    vector<int> cnt(10, 0);
    vector<int> cnt_yes(10, 0);
    vector<int> num(in.size(), 0);
    for (size_t i = 0; i < in.size(); i++) {
        cnt[in[i] - '0']++;
        num[i] = in[i] - '0';
    }
	int flag=0;
    for (size_t i =in.size();i>0; i--) {//重点改了这... 从数字末尾--最低位开始
        num[i-1] *= 2;
        if(flag ==1)
        	num[i-1]++,flag=0;

        if (num[i-1] >= 10) {
            num[i-1] -= 10;        //原来进位+1还*2
            flag=1;
        }
    }
    for (size_t i = 0; i < in.size(); i++){
        cnt_yes[num[i]]++;
	}
    if (cnt == cnt_yes)
        cout << "Yes\n";
    else
        cout << "No\n";
    for (size_t i = 0; i < in.size(); i++) {
        cout << num[i];
    }

    return 0;
}

1023. Have Fun with Numbers (20)-PAT甲级真题(大整数运算) – 柳婼 の blog (liuchuo.net)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值