新生训练01

题解连接

C - Choosing Symbol Pairs

题意:求一个字符串中有多少第i个和第j个字母或数字相同的对数,其中(i,j)与(j,i)不相同。
解法: 求这一个字母出现的次数,然后从串的头扫到尾,每个字符出现的次数相加就是ans;

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

int main()
{  	
	map<char,int> cishu;
	long long ans=0;
    char c[100010];
    cin>>c;
    int n=strlen(c);
    for(int i=0;i<n;i++)
        cishu[c[i]]++; 
    for(int i=0;i<n;i++){
    	ans+=cishu[c[i]];
	} 
    cout<<ans<<endl;
    return 0;
}

D - Sum

题意

给你两个数字,从第一个比数字里每一位中最大的数大的进制开始,到16进制,在当前的进制下进行加法,求出结果以后求长度的最大值。

 
解法一:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#define maxn 200
using namespace std;
int getmaxnum(int x)//找一个数字中最大数位
{
    int max=0;
    while(x)
    {
        int s=x%10;
        if(s>max) max=s;
        x/=10;
    }
    return max;
}
int change(int x,int base)//10进制转base进制
{
    int s=0,p=1;
    while(x)
    {
        s+=x%10*p;
        x/=10;
        p*=base;
    }
    return s;
}
int main()
{
    int a,b;
    cin>>a>>b;
    int base=max(getmaxnum(a),getmaxnum(b))+1;//求基数
    int sum=change(a,base)+change(b,base);//在base进制下两数相加,求十进制结果
    int j=0;
    while(sum)//求其位数
    {
        int s=sum%base;
        sum/=base;
        j++;
    }
    cout<<j<<endl;
    return 0;
}

方法二:
梁sir的解法:
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		int a = cin.nextInt();
		int b = cin.nextInt();
		int n = a, m = b;
	    int maxx = 0;
	    while(a!=0)
	    {
	        maxx = Math.max(maxx,a%10);
	        a/=10;
	    }
	    while(b!=0)
	    {
	        maxx = Math.max(maxx,b%10);
	        b/=10;
	    }
//	    System.out.println(maxx);
	    int ans = 0;
	    for(int i=maxx+1;i<=16;i++)
	    {
	    	String nn = Integer.toString(n);
	    	String mm = Integer.toString(m);
	        BigInteger s1 = new BigInteger(nn,i);
	        BigInteger s2 = new BigInteger(mm,i);
//	        System.out.println(s1 + " " + s2 + " "+s1.add(s2).toString(i));
	        ans = Math.max(ans, s1.add(s2).toString(i).length());
	    }
	    System.out.println(ans);
	}
}

G - You’re Given a String…

暴力破解,直接遍历所有的子串进行比较就可以了,这种方法要记住。

 
#include<iostream>
using namespace std; 

int main(){
        string s;
        while(cin >> s){
                int ls = s.length() ;
                int max1 = 0;   //一定要初始化为0
                for(int i = 0 ; i < ls ; i++){
                        for( int j = i + 1 ; j < ls ; j++){
                                int m = 0;
                                while(s[i + m] == s[j + m] ){
                                        m++;
                                }
                                max1 = max(m,max1);

                        }
                }
                cout << max1 << endl;
        }
        return 0;
}

H-Party

题意:现在party上有n个人参加,然后依次按照规律离开,第一次是有0个朋友的人离开,第二次是有1个朋友的人离开,第三次是有2个朋友的人离开,依次是3,4,5,6,。。n-1个朋友的一次离开,求最后party会剩下多少人

思路:找规律,当一个人或2个人的时候,将会剩下0个人,3个人以上,即n个人时,将会剩下(n-2)个人

upload successful

 
#include<iostream>
using namespace std;
int main(){
	int t;
	int a;
	cin>>t;
	while(t--){
		cin>>a;
		if(a==2||a==1)cout<<"0"<<endl;
		else cout<<a-2<<endl;
	}
}

J - Second Order Statistics

对一个整数序列(可以有负数)进行排序,找第二小的数。
而且 In other words it is the smallest element strictly greater than the minimum.

 
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int a[110];
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	set<int>v(a,a+n);
	//sort(v.begin(),v.end(),less<int>());  这样不对,set不能进行排序,会自动从小到大排序了 
	set<int>::iterator it=v.begin();
	v.erase(it);
	it=v.begin();
//	for(set<int>::iterator it=v.begin();it!=v.end();it++)
//	cout<<*it<<" ";
	if(v.size()==0)cout<<"NO"<<endl;
	else {
		printf("%d",*it);
	}			
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值