1.C++基础

A - A + B Problem II HDU - 1002(大数加法)

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
AC代码

//大数加法
//思路:将两个数字倒置补零,133 8--->331 800 ,进位相加。 
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
	int t;
	cin>>t; 
	int num=0;
	while(t--)
	{
		char a[11111],b[11111];
		cin>>a>>b;
		num++;
		int la=strlen(a),lb=strlen(b),lmax=max(la,lb);

		int aa[1111],bb[1111],cc[1111];
		memset(aa,0,sizeof(aa));
		memset(bb,0,sizeof(bb));
		memset(cc,0,sizeof(cc));
		//数组初始化,将数组内所有元素置0 
		 
		for(int i=0;i<la;i++)	aa[i] = a[la-i-1]-'0';
		for(int i=0;i<lb;i++)	bb[i] = b[lb-i-1]-'0';
		//将数字倒置 
		
		int jin=0,i;
		for(i=0;i<lmax;i++)
		{
			int sum=aa[i]+bb[i]+jin;
			cc[i]=sum%10;
			jin=sum/10;
		}
		//进位相加 
		if(jin)
		{
			cc[i] = jin;
			i++;
		}
		//如果计算完了还有一位进位	
		
		cout<<"Case "<<num<<":"<<endl;
		cout<<a<<" + "<<b<<" = ";
		for(int j=i-1;j>=0;j--)	cout<<cc[j];
		cout<<endl;
		if(t>0)	cout<<endl;
	}
	return 0;
}

D - Magical Bamboos Gym - 101350D (规律题)

In a magical forest, there exists N bamboos that don’t quite get cut down the way you would expect.

Originally, the height of the ith bamboo is equal to hi. In one move, you can push down a bamboo and decrease its height by one, but this move magically causes all the other bamboos to increase in height by one.

If you can do as many moves as you like, is it possible to make all the bamboos have the same height?

Input
The first line of input is T – the number of test cases.

The first line of each test case contains an integer N (1 ≤ N ≤ 105) - the number of bamboos.

The second line contains N space-separated integers hi (1 ≤ hi ≤ 105) - the original heights of the bamboos.

Output
For each test case, output on a single line "yes” (without quotes), if you can make all the bamboos have the same height, and “no” otherwise.

Example
Input
2
3
2 4 2
2
1 2
Output
yes
no
AC代码

//英语差真要命....这里长度的变换是一个单位一个单位的增加减少,而不是一倍一倍的增加减少,所以只要所有数字奇偶性相同就行了
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int a[111111];
		for(int i=0;i<n;i++)	cin>>a[i];
		int vis=1;
		sort(a,a+n); 
		for(int i=0;i<n-1;i++)
		{
			if((a[i+1]-a[i])%2)
			{
				vis=0;
				break;
			}
		}
		if(vis)	cout<<"yes"<<endl;
		else	cout<<"no"<<endl;
	}
	return 0;
}

G - Yet Another Walking Robot CodeForces - 1296C (map,pair)

There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0). Its path is described as a string s of length n consisting of characters ‘L’, ‘R’, ‘U’, ‘D’.

Each of these characters corresponds to some move:

‘L’ (left): means that the robot moves from the point (x,y) to the point (x−1,y);
‘R’ (right): means that the robot moves from the point (x,y) to the point (x+1,y);
‘U’ (up): means that the robot moves from the point (x,y) to the point (x,y+1);
‘D’ (down): means that the robot moves from the point (x,y) to the point (x,y−1).
The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn’t want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye), then after optimization (i.e. removing some single substring from s) the robot also ends its path at the point (xe,ye).

This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot’s path such that the endpoint of his path doesn’t change. It is possible that you can’t optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string s).

Recall that the substring of s is such string that can be obtained from s by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of “LURLLR” are “LU”, “LR”, “LURLLR”, “URL”, but not “RR” and “UL”.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤1000) — the number of test cases.

The next 2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer n (1≤n≤2⋅105) — the length of the robot’s path. The second line of the test case contains one string s consisting of n characters ‘L’, ‘R’, ‘U’, ‘D’ — the robot’s path.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot’s path doesn’t change, print -1. Otherwise, print two integers l and r such that 1≤l≤r≤n — endpoints of the substring you remove. The value r−l+1 should be minimum possible. If there are several answers, print any of them.

Example
Input
4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR
Output
1 2
1 4
3 4
-1
AC代码

#include <iostream>
#include <map>
#include <cstring>
using namespace std;

int main()
{
	int t,n;
	string s;
	cin>>t;
	while(t--)
	{
		cin>>n>>s;
		int l=-1,r=n; //当前可删除的字符串的左右位置 
		map<pair<int,int>,int> mp; //建立映射关系,每个坐标对应一个或多个字符串下标
		pair<int,int> flag={0,0}; //flag表示当前行走到的坐标,初始坐标(0,0) 
		mp[flag]=0; //坐标为flag时对应的字符串下标 
		for(int i=0;i<n;i++)
		{
			if(s[i]=='L')	flag.first--;
			if(s[i]=='R')	flag.first++;
			if(s[i]=='D')	flag.second--;
			if(s[i]=='U')	flag.second++;
			if(mp.count(flag)) //mp.count()判断该键值在容器中是否存在,存在返回1,不存在返回0
			{
				if(i-mp[flag]<r-l)
				{
					l=mp[flag];
					r=i;
				}
			}
			mp[flag]=i+1; 
		}
		if(l==-1)	cout<<-1;
		else	cout<<l+1<<' '<<r+1;
		if(t>0)	cout<<endl;
	}
	return 0;
}

I - Assigning to Classes CodeForces - 1300B (规律题)

Reminder: the median of the array [a1,a2,…,a2k+1] of odd number of elements is defined as follows: let [b1,b2,…,b2k+1] be the elements of the array in the sorted order. Then median of this array is equal to bk+1.

There are 2n students, the i-th student has skill level ai. It’s not guaranteed that all skill levels are distinct.

Let’s define skill level of a class as the median of skill levels of students of the class.

As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.

What is the minimum possible absolute difference you can achieve?

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤105) — the number of students halved.

The second line of each test case contains 2n integers a1,a2,…,a2n (1≤ai≤109) — skill levels of students.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.

Example
Input

3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
题目大意:
2n个数,分为两数组,使其中位数差值最小
思路:
先对数组排序,分为两组,那么两组的中位数一定一个在前半段,一个在后半段。
那么要使中位数差值最小,那么选取最中间两个数分别作为两组中位数一定为最优解。

AC代码

#include <iostream>
#include <algorithm> //sort函数头文件 
using namespace std;
const int N=2e5+10; //注意这里是2e不是1e 
int main()
{
	int t,n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		int a[N];
		for(int i=1;i<=2*n;i++)
			cin>>a[i];
		sort(a+1,a+1+2*n); //将数组从小到大排序 
		cout<<a[n+1]-a[n]<<endl;
	}
	return 0;	
}

J - Display The Number CodeForces - 1295A (规律题)

You have a large electronic screen which can display up to 998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of 7 segments which can be turned on and off to compose different digits. The following picture describes how you can display all 10 decimal digits:

As you can see, different digits may require different number of segments to be turned on. For example, if you want to display 1, you have to turn on 2 segments of the screen, and if you want to display 8, all 7 segments of some place to display a digit should be turned on.

You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than n segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than n segments.

Your program should be able to process t different test cases.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases in the input.

Then the test cases follow, each of them is represented by a separate line containing one integer n (2≤n≤105) — the maximum number of segments that can be turned on in the corresponding testcase.

It is guaranteed that the sum of n over all test cases in the input does not exceed 105.

Output
For each test case, print the greatest integer that can be displayed by turning on no more than n segments of the screen. Note that the answer may not fit in the standard 32-bit or 64-bit integral data type.

Example
Input

2
3
4
Output
7
11
思路:
1和7的收益最高,先尽可能多取7,最后一个取1。
AC代码

#include <iostream>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		if(n%2)
		{
			cout<<7;
			n=(n-3)/2;
			while(n--)	cout<<1;
		}
		else
		{
			n=n/2;
			while(n--)	cout<<1;
		}
		cout<<endl;
	}
	return 0;
}

K - Yet Another Meme Problem CodeForces - 1288B

Try guessing the statement from this picture http://tiny.cc/ogyoiz.

You are given two integers A and B, calculate the number of pairs (a,b) such that 1≤a≤A, 1≤b≤B, and the equation a⋅b+a+b=conc(a,b) is true; conc(a,b) is the concatenation of a and b (for example, conc(12,23)=1223, conc(100,11)=10011). a and b should not contain leading zeroes.

Input
The first line contains t (1≤t≤100) — the number of test cases.

Each test case contains two integers A and B (1≤A,B≤109).

Output
Print one integer — the number of pairs (a,b) such that 1≤a≤A, 1≤b≤B, and the equation a⋅b+a+b=conc(a,b) is true.

Example
Input

3
1 11
4 2
191 31415926
Output
1
0
1337
Note
There is only one suitable pair in the first test case: a=1, b=9 (1+9+1⋅9=19).
思路:
ab+a+b=conc(a,b)
即 a
b+a+b=a*(10)^n+b(n为b的位数)
即 ab+a=a(10)^n
即 b+1=(10)^n
可以看出要想满足条件只需b=9、99、999·····即可,a可取任意值。
所以ans为满足条件的b的个数*A。
AC代码

#include <iostream>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long a,b;
		cin>>a>>b;
		long long cont =0;
		int c=10;
		while(b>=c-1)
		{
			cont++;
			c*=10;
		}
		cout<<a*cont<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值