C++基础题题解

A- A+B Problem Ⅱ

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

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.

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

这道题是计算两个数的和,但是这里的两个数的范围太大,超过了long long的范围,所以只能使用数组进行代替进行计算。
在计算的时候,直接利用数组进行正向相加的话,如果不同位的话,需要在前面补0,才方便计算,但是如果第一位的相加超过十,又会向前进位,所有可以选择反向相加,不需要担心前面进位
并且这道题的格式很重要****
方法一:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char a[1010],b[1010];
int a1[1010],b1[1010],c[1010];
int main()
{
	int T,F,cnt=0;
	scanf("%d",&T);
	F=T;
	while(T--)
	{
		scanf("%s %s",a,b);
		memset(a1,0,sizeof a1);         //先将int类型的数组进行初始化
		memset(b1,0,sizeof b1);
		memset(c,0,sizeof c);
		int lena,lenb,maxn;
		lena = strlen(a),lenb = strlen(b);
		maxn = max(lena,lenb);        //maxn是两个字符数组的最大值
		for(int i=0;i<lena;i++) a1[i]=a[lena-i-1] - '0';      //将字符数组中的字符转化为int类型存入到数组中
		for(int i=0;i<lenb;i++) b1[i]=b[lenb-i-1] - '0';
		for(int i=0;i<maxn+1;i++)      //使用maxn+1是为了保证能够进位
		{
			c[i]=a1[i] + b1[i] + c[i];
			if(c[i]>=10)
			{
				c[i+1]=c[i+1]+1;      //反向数字的方便
				c[i]=c[i]-10;
			}
		}
		printf("Case %d:\n",++cnt);
		printf("%s + %s = ",a,b);
		if(c[maxn]==0)       //这里需要进行判断,如果数组在maxn出为0的话,代表没有进位,从maxn-1位开始向前读取
		{
			for(int i=maxn-1;i>=0;i--)
			printf("%d",c[i]);
		}
		else          
		{
			for(int i=maxn;i>=0;i--)
				printf("%d",c[i]);
		}
		printf("\n");          //格式很重要
		if(cnt!=F) printf("\n");
	 } 
	 return 0;
}

方法二

#include<iostream>
#include<cstring>
using namespace std;
const int N=1010;
int T,cnt,lena,lenb,lenmax;
string A,B;
int a[N],b[N],c[N],i,sum,jin;
int main()
{
	cin>>T;
	cnt=1;
	while(T--)
	{
		cin>>A>>B;
		lena=A.size();
		lenb=B.size();
		lenmax=max(lena,lenb);
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		for(i=lena-1;i>=0;i--)
			a[lena-i-1]=A[i]-'0';
		for(i=lenb-1;i>=0;i--)
			b[lenb-i-1]=B[i]-'0';
		for(i=0;i<lenmax;i++)
		{
			sum=a[i]+b[i]+jin;
			c[i]=sum%10;           //C[i]代表的当前位的数
			jin=sum/10;            //如果sum是大于10的话,jin就是1
		}
		if(jin>0)
		{
			c[i]=jin;
			i++;
		}
		cout<<"Case "<<cnt<<":"<<endl;
		cout<<A<<" + "<<B<<" = ";
		for(int j=i-1;j>=0;j--)            //反向读取
		{
			cout<<c[j];
		}
		cout<<endl;
		if(T!=0)	cout<<endl;
		cnt++;
	}
	return 0;
}

B - 进制转换

输入一个十进制数N,将它转换成R进制数输出。
Sample Input

111
1B
-11

Sample Output

111
1B
-11

这题就是将10进制的数转化位其他进制的数,如果这个数超过10的话,需要使用字母进行代替。

#include<iostream>
#include<algorithm>
using namespace std;
char a[1100];
int main()
{
	int n,r;
	while(~scanf("%d %d",&n,&r))
	{
		int cnt=0,c;
		while(n)         //循环条件是n位0结束
		{
			if(n<0)
			{
				printf("-");        //如果是负数的话,需要先把负数转化为正数
				n=-n;
			}
			c=n%r;
			if(c>=10) a[cnt++]=c-10+'A';    //如果大于10的话,用大写字母来代替
			else a[cnt++]=c+'0';
			n=n/r;  
		}
		for(int i=cnt-1;i>=0;i--)     //需要反向输出
		{
			printf("%c",a[i]);
		}
		printf("\n");
	}
	return 0;
}

C - Palindromes _easy version

“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。

Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。

Output
如果一个字符串是回文串,则输出"yes",否则输出"no".

这道题就是判断字符串前后是否相同。

方法一

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char a[1000],b[1000];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(a,'\0',sizeof a);      //每一回需要重新初始化字符数组
		memset(b,'\0',sizeof b);
		int len,cnt=0;
		scanf("%s",a);
		len=strlen(a);
		for(int i=len-1;i>=0;i--)        //将反向的字符串存入另一个数组中
			b[cnt++]=a[i];
		if(strcmp(a,b)==0) printf("yes\n");     //比较两个字符数组是否相同
		else printf("no\n"); 
	}
	return 0;
}

方法二

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string a;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int len,flag=1;
		cin>>a;
		len=a.size();
		for(int i=0;i<len/2;i++)
		{
			if(a[i]!=a[len-i-1])      //如果两个不一样的话,直接退出循环
			{
				flag=0;
				break;
			}
		}
		if(flag) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

D - Magical Bamboos

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 i th bamboo is equal to h i. 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 h i (1 ≤ h i ≤ 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.

这道题就是一个数组中,一个元素减1,其他的元素都加1,能否使它们相等。
这道题只需要保证任意两个元素差为偶数即可

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

int a[100005];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int N,ans=1;
		scanf("%d",&N);
		for(int i=0;i<N;i++)
			scanf("%d",&a[i]);
		sort(a,a+N);           //这个sort排序其实可以不用
		for(int i=1;i<N;i++)
		{
			if((a[i]-a[i-1])%2 !=0)      //如果存在两个差不为偶数,就break,跳出循环
			{
				ans=0;         
				break;
			}	
		}
		if(ans) printf("yes\n");
		else printf("no\n"); 
	} 
	return 0;
}

E - 首字母变大写

输入一个英文句子,将每个单词的第一个字母改成大写字母。

Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。

Sample Input

i like acm
i want to get an accepted

Sample Output

I Like Acm
I Want To Get An Accepted

因为是英文句子,所以每个单词之间的间隔为一个空格,如果当前数组元素为空格的话,则下一个元素就不是空格。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string> 
using namespace std;
char a[110];
int main()
{
	while(gets(a)!=NULL)        //输入字符串的格式,也可以写成gets(a)
	{
		a[0]=a[0]-32;
		for(int i=1;i<strlen(a);i++)
		{
			if(a[i]==' ') a[i+1]=a[i+1]-32;
		}
		for(int i=0;i<strlen(a);i++)
			printf("%c",a[i]);
		printf("\n");
	}
	return 0;
}

F - Food Buying

Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number 1≤x≤s, buy food that costs exactly x burles and obtain ⌊x10⌋ burles as a cashback (in other words, Mishka spends x burles and obtains ⌊x10⌋ back). The operation ⌊ab⌋ means a divided by b rounded down.

It is guaranteed that you can always buy some food that costs x
for any possible value of x

Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.

For example, if Mishka has s=19
burles then the maximum number of burles he can spend is 21. Firstly, he can spend x=10 burles, obtain 1 burle as a cashback. Now he has s=10 burles, so can spend x=10 burles, obtain 1
burle as a cashback and spend it too.

You have to answer t
independent test cases.

Input

6
1
10
19
9876
12345
1000000000

Output

1
11
21
10973
13716
1111111111

题意就是每10元可以得到1元钱,给你一定的钱数,你需要计算你可以花出的最多的钱。

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		ll a,sum=0;
		cin>>a;
		while(a)
		{
			if(a>9)        //如果a大于9的话,说明可以再得到1元钱,需要在进行操作
			{
				sum+=a-a%10;    // a-a%10 代表的是除去个位数的钱数
				a=a/10+a%10;     //更新当前拥有的钱数,a/10代表的得到的钱数,a%10代表的个位小于9的钱数,两者相加,就是当前总钱数
			}
			else{
				sum+=a;     //如果小于9的话,不能再花10元,所以直接进行加和即可
				a=0;     //需要初始化a,使其等0,以便结束循环
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

H - Non-zero

Guy-Manuel and Thomas have an array a of n integers [a1,a2,…,an]. In one step they can add 1 to any element of the array. Formally, in one step they can choose any integer index i (1≤i≤n) and do ai:=ai+1
If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.

What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make a1+a2+… +an≠0 and a1⋅a2⋅ … ⋅an≠0.

Input

4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1

Output

1
2
0
2

给一组数,要求这些数的和与乘积不能为0,每次可以给这些数加1,需要多少次才能保证和与乘积不为0。

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
int a[110];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		memset(a,0,sizeof a);    //每次进行初始化,其实也可以不用
		int n,ans=0,sum=0;
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];        
			sum+=a[i];       //计算输入数组元素的和
			if(a[i]==0)    //如果遇到0,ans加1
				ans++;
		}
		sum+=ans;	  //更新sum的值,加上ans      ans代表把a[i]中的0都加1
		if(!sum) ans++;   //判断sum的值是否为0,如果为0,需要进行加1
		cout<<ans<<endl;
	}
	return 0;
}

I - Assigning to Classes

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 2nintegers 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.

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

这道题的意思就是你需要把学生分到两个班,这两个班的学生的中位数之差最小,其实就是把这个数组的元素的中间两个值进行相减。
这道题的坑在于给你的n只是一半的数量,需要2n个学生

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;      
int a[maxn];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{	
		int n;
		cin>>n;
		for(int i=0;i<2*n;i++)
			cin>>a[i];
		sort(a,a+2*n);     //对数组进行排序
		cout<<a[n]-a[n-1]<<endl;
	}
	return 0;
}

J - Display The Number

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 7segments 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 10^5.

Input

2
3
4

Output

7
11

这道题的意思就是给你一定的木棍,你需要用给你的这些木棍摆出一个最大的数,例如给你给3,能摆出7,这个7是最大值,0123456789对应的木棒根数为6255456376,比较之后,应当尽可能选择1或者7.

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int a[maxn];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{	
		int n;
		cin>>n;
		if(n%2==0)      //如果是偶数的花,尽量使用1
		{
			int k;
			k=n/2;
			for(int i=0;i<k;i++)
				cout<<1;
		}
		else     //如果是奇数,需要先-3,输出7,再将剩余的2拼成1
		{
			int k;
			k=(n-3)/2;
			cout<<7;
			for(int i=0;i<k;i++)
				cout<<1;
		}
		cout<<"\n";
	}
	return 0;
}

K - Yet Another Meme Problem

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

3
1 11
4 2
191 31415926

Output

1
0
1337

a⋅b+a+b=conc(a,b)
==>a·b+a=a*10w(上式两侧同减b,w为b的位数)
==>b+1=10w(上式两侧同除a)
由上式可以看出,b为10w-1,例如9,99,999,9999……
a无限制条件。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值