PTA天梯赛习题集

忘记是哪些题目了,正在刷题的小伙伴应该能对上。
用于学习参考,要有进步呀~

#include <iostream>

using namespace std;

int main()
{
    char log;
    int total;
    cin >> total;
    cin.get();
    cin >> log;
    if(total == 1 )
    {
        cout << log << endl;
        cout << "0";
        return 0;
    }
    int n = 1; //撒漏最外层个数
    int needed = 0;
    while(needed <= total)
    {
        needed = (1+n)*(n/2+1)-1;
        n += 2;
    }
    n -= 4;
    //上半漏斗
    for(int i=n; i>0; i=i-2)
    {
        int b = (n-i)/2;
        if(b!=0)
            for(int j=b; j>0; j--)
                cout << " ";
        for(int k=i; k>0; k--)
            cout << log;
        cout << endl;
    }
    //下半漏洞
    for(int i=3; i<=n; i=i+2)
    {
        int b = (n-i)/2;
        if(b!=0)
            for(int j=b; j>0; j--)
                cout << " ";
        for(int k=i; k>0; k--)
            cout << log;
        cout << endl;
    }
    cout << total-(1+n)*(n/2+1)+1;
    return 0;
}

/**
n层数,循环当个数>N的前一个层数
(1+n)(n/2+1)-1 = 个数
**/
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String num = in.next();
		Map<String,Integer> map = new HashMap<String,Integer>();
		for(int i=0; i<num.length(); i++) {
			String now = String.valueOf(num.charAt(i));
			if(map.putIfAbsent(now, Integer.valueOf(1)) != null) {
				map.replace(now, (map.get(now))+1);
			}
		}
		
		for(String key:map.keySet()) {
			System.out.println(key+":"+map.get(key));
		}
	}

}
#include <iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;
    int f = (t-32)*5/9; //截断
    cout << "Celsius = " << f;
    return 0;
}
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		int times = 0;
		times = in.nextInt();
		in.nextLine(); //把后面的回车吞了
		Map<String, String> map = new HashMap<String, String>();
		String tmp = null;
		String[] tmps = null;
		for(int i=0; i<times; i++) {
			tmp = in.nextLine();
			tmps = tmp.split(" ");
			//System.out.println(Arrays.toString(tmps));
			map.put(tmps[1], tmps[0]+" "+tmps[2]);
		}
		
		times = in.nextInt();
		int setNo = 0;
		for(int i=0; i<times; i++) {
			setNo = in.nextInt();
			System.out.println(map.get(String.valueOf(setNo)));
		}
		
		in.close();
	}

}
但是超时了两个。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main {

	static BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in = new StreamTokenizer(re);
	
	public static int nextInt() {
		try {
			in.nextToken();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (int) in.nval;
	}
	

	public static void main(String[] args) throws IOException {
		int times = 0;
		times = nextInt();
		//re.readLine(); // 把后面的回车吞了
        //in.nextToken();
		Map<String, String> map = new HashMap<String, String>();
		String tmp;
		String[] tmps = null;
		for (int i = 0; i < times; i++) {
			tmp = re.readLine();
			tmps = tmp.split(" ");
			//System.out.println(tmp);
            //System.out.println(Arrays.toString(tmps));
            //System.out.println("____________________");
			map.put(tmps[1], tmps[0] + " " + tmps[2]);
		}

		times = nextInt();
		PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
		int setNo = 0;
		for (int i = 0; i < times; i++) {
			setNo = nextInt();
			pr.println(map.get(String.valueOf(setNo)));
		}
		pr.flush();
	}

}

连续因子那道题坑挺多的,题目也不好懂

package tianti;

import java.util.Scanner;

public class L1007 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String str = in.next();
		in.close();
		StringBuffer ans = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			switch (str.charAt(i)) {
			case '-':
				ans.append("fu ");
				break;
			case '0':
				ans.append("ling ");
				break;
			case '1':
				ans.append("yi ");
				break;
			case '2':
				ans.append("er ");
				break;
			case '3':
				ans.append("san ");
				break;
			case '4':
				ans.append("si ");
				break;
			case '5':
				ans.append("wu ");
				break;
			case '6':
				ans.append("liu ");
				break;
			case '7':
				ans.append("qi ");
				break;
			case '8':
				ans.append("ba ");
				break;
			case '9':
				ans.append("jiu ");
				break;
			}
		}
		
		System.out.println(ans.substring(0,ans.length()-1));

	}

}

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string M = "10X98765432", s;
    int times, t = 0, sum = 0;
    int weight[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    cin >> times;
    bool flag = true;
    while(times--)
    {
        cin >> s;
        if (s.find('X') == string::npos || s.find('X') == 17)
        {
            sum = 0;
            for (int i = 0; i<17; i++)
            {
                sum += (s[i] - '0') * weight[i];
            }
            if (s[17] == M[sum % 11])
                t++;
            else
            {
                flag = false;
                cout << s << endl;
            }

        }
        else
        {
            flag = false;
            cout << s << endl;
        }
    }
    if(flag)
        cout << "All passed";
    return 0;
}

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

long gcd(long x, long y)
{
    if(x==0 || y==0)
    {
        return 0;
    }
    return y%x==0 ? x : gcd(y%x, x);
}

int main()
{
    int times;
    cin >> times;
    long bigNum, son, mom, tmpSon, tmpMom, a;
    cin >> son;
    cin.get();
    cin >> mom;
    //目的处理分母,最后把整数取出来
    while(--times)
    {
        cin >> tmpSon;
        cin.get();
        cin >> tmpMom;
        a = gcd(tmpMom, mom);
        son = son*(tmpMom/a) + tmpSon*(mom/a);//写在坟墓前面否则分母都变了
        mom *= tmpMom/a;
    }
    bigNum = son/mom;
    son -= mom*bigNum;
    if(son != 0)
    {
        a = gcd(son, mom);
        son /= a;
        mom /= a;
    }
    if(bigNum != 0 && son != 0)
    {
        cout << bigNum << " " << son << "/" << mom;
    }
    else if(bigNum == 0 && son != 0)
    {
        cout << son << "/" << mom;
    }
    else if(bigNum != 0 && son == 0)
    {
        cout << bigNum;
    }
    else
    {
        cout << 0;
    }
    return 0;
}

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

int main()
{
    string a,b;
    getline(cin, a);
    getline(cin, b);
    for(int i=0; i<a.size(); i++)
    {
        if(b.find(a[i]) != -1)
        {
            continue;
        }
        else
        {
            cout << a[i];
        }
    }
    return 0;
}

有一个案例过不了,不知道为什么

#include<iostream>
using namespace std;

static int maxd, mid, mind;

int main()
{
    cin >> mind >> mid >> maxd;
    if(mind >= mid)
    {
        int tmp = mind;
        mind = mid;
        mid = tmp;
        if(mind >= maxd)
        {
            tmp = mind;
            mind = maxd;
            maxd = tmp;
        }
    }
    cout << mind << "->";
    if(mid >= maxd)
    {
        int tmp = mid;
        mid = maxd;
        maxd = tmp;
    }
    cout << mid << "->" << maxd;
    return 0;
}

过了的

#include<bits/stdc++.h>
using namespace std;

int a[3];
int main()
{
	cin>>a[1]>>a[2]>>a[0];
	sort(a, a+4);
	cout<<a[1]<<"->"<<a[2]<<"->"<<a[3];
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值