2.18 CodeForces补题

Codeforces Round #762 Div. 3


B. Squares and Cubes

time limit per test1 second
memory limit per test256 megabytes

Polycarp likes squares and cubes of positive integers. Here is the beginning of the sequence of numbers he likes: 1, 4, 8, 9, …

For a given number n, count the number of integers from 1 to n that Polycarp likes. In other words, find the number of such x that x is a square of a positive integer number or a cube of a positive integer number (or both a square and a cube simultaneously).

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

Then t lines contain the test cases, one per line. Each of the lines contains one integer n (1≤n≤109).

Output
For each test case, print the answer you are looking for — the number of integers from 1 to n that Polycarp likes.

Example
input
6
10
1
25
1000000000
999999999
500000000
output
4
1
6
32591
32590
23125

解题思路:

根据样例可知,1000000000开方向下取整为32591,开三次方为1000,因此我们只需要遍历1到32591并记录输入它的平方,遍历1到1000并记录它的立方。

两者会有一些重合的数字(例如64既为8的平方又为4的立方),因此总共一定不会超过33591个数字,样例最多20组,大约估计一下,暴力不会超时。那还说什么,直接暴力解决!暴力yyds!

我们可以使用STL库中的set函数,这个函数可以理解为一个集合,我们往这个集合中放入数字,它会自己去掉重复的数字并按照从小到大的顺序排好,简直是为了这道题而生的

附上我的AC代码:

#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
	set<long long>a;
	int i,j=0;
	for(i=1;i<=32591;i++) a.insert(i*i);
	
	for(i=2;i<=1000;i++) a.insert(i*i*i);
	
	long long n;
	cin>>n;
	while(n--){
		long long x;
		cin>>x;
		long long sum=0;
		
		for(set<long long>::iterator it = a.begin();it!=a.end();++it)
		{
			if(*it<=x)sum++;
			else break;
		}
		cout<<sum<<endl;
	}
	return 0; 
}

其实昨天晚上比赛时,我的队友有提出另外的方法,但是今天在尝试的时候发现会出现精度丢失的问题(我怀疑是pow函数的问题),所以这个方法还有待研究探讨。

C. Wrong Addition

time limit per test1 second
memory limit per test256 megabytes

Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers a and b using the following algorithm:

If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length.
The numbers are processed from right to left (that is, from the least significant digits to the most significant).
In the first step, she adds the last digit of a to the last digit of b and writes their sum in the answer.
At each next step, she performs the same operation on each pair of digits in the same place and writes the result to the left side of the answer.
For example, the numbers a=17236 and b=3465 Tanya adds up as follows:

 	  17236
 	 +03465
 	————————
	1106911

calculates the sum of 6+5=11 and writes 11 in the answer.
calculates the sum of 3+6=9 and writes the result to the left side of the answer to get 911.
calculates the sum of 2+4=6 and writes the result to the left side of the answer to get 6911.
calculates the sum of 7+3=10, and writes the result to the left side of the answer to get 106911.
calculates the sum of 1+0=1 and writes the result to the left side of the answer and get 1106911.
As a result, she gets 1106911.

You are given two positive integers a and s. Find the number b such that by adding a and b as described above, Tanya will get s. Or determine that no suitable b exists.

Input
The first line of input data contains an integer t (1≤t≤104) — the number of test cases.

Each test case consists of a single line containing two positive integers a and s (1≤a<s≤1018) separated by a space.

Output
For each test case print the answer on a separate line.

If the solution exists, print a single positive integer b. The answer must be written without leading zeros. If multiple answers exist, print any of them.

If no suitable number b exists, output -1.

Example
input
6
17236 1106911
1 5
108 112
12345 1023412
1 11
1 20

output
3465
4
-1
90007
10
-1
Note
The first test case is explained in the main part of the statement.

In the third test case, we cannot choose b that satisfies the problem statement.

解题思路:

不说了家人们,这道题改bug差点给我改崩溃了。补题的时候WA了6次,结果发现就是自己敲代码的时候不仔细,一些细节条件漏掉了。

这道题我的思路是用数组存,然后一位一位操作。基本流程没啥好说的,大家都会,就说一些需要注意的点吧:

  • 去掉前导0的时候要考虑到答案就是0本身的情况。
  • 当处于两位数减一位数这种情况时,要注意减出来的得数大于等于0且小于10。
  • 判断-1的情况要考虑全面。

注意这几点应该就没什么大问题了,多WA几次就会了
我的代码如下:

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int n;
	cin>>n;
	while(n--){
		string x,y;
		cin>>x>>y;
		int len1=x.size();
		int len2=y.size();
		int k=-1,flag=0,i,j;
		if(len2<len1) cout<<"-1"<<endl;
		else 
		{
			int ans[len2],a[len2];//ans[]用于储存减出来的得数,a[]用于最后跟第二个数多出来那几位合并储存结果并删除前导零。
			//不要问我为什么不用ans[]来储存结果,问就是我脑抽写反了。
			for(i=len1-1,j=len2-1;i>=0;i--,j--)
			{
				if(x[i]<=y[j]) ans[++k]=y[j]-x[i];
				else
				{
					ans[++k]=(y[j-1]-'0')*10+(y[j]-'0')-(x[i]-'0');
					j--;
					if(ans[k]>=10||ans[k]<0){
						flag=1;
						break;
					}
				}
				
				if(j<0)//如果第一个数还有位数但是第二个数位数用光了,那么不符合条件
				{
					flag=1;
					break;
				}
			}
			if(flag==0)
			{
				int v=-1,f=0;
				for(i=0;i<=j;i++) a[++v]=y[i]-'0';//第二个数多出来那几位直接写到结果数组里
				for(;k>=0;k--) a[++v]=ans[k];//减出来的得数写进结果数组
				for(i=0;i<=v;i++)
				{
					if(v==0)cout<<a[v];
					else 
					{
						while(f==0)
						{
							if(a[i]==0)i++;//删去前导零
							else f=1;
						}
						cout<<a[i];
					}
				
				}
				cout<<endl;
			}else cout<<"-1"<<endl;
			
		}
	}
	return 0;
}

总结

  • 题目不难,但是我代码能力还是太弱,很多细节也没有注意到。
  • 第一次打团队赛,敲代码的时候会有些慌乱,以后多练习,尽量做到条理清晰、有条不紊。
  • 看英文题对我来说是一大难关,这次多亏队友读题,以后要尽量尝试自己读英文题。
  • 写题的时候尽量让框架清晰一些,使用变量也要尽量用便于区分和理解的。

D题和F题今天也尝试了,敲了一下午没敲出来,可见我目前码力之弱 。改天再战。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值