2022.7.9暑假个人训练1-C . Clean up the Powers that Be

C . Clean up the Powers that Be

题目描述

Dr. Orooji, a number theoretician on the side, has written an amazing program that prime fectorizes numbers. He is so ecstatic that his program works, that he has forgotten to properly format (clean up) the output of his program. Luckily, Dr. O has hired you as a student assistant, to do his grunt work, formatting the output of his ingenius program. Your goal is to take the output of Dr. O's program, and nicely format it for everyone else.

The Problem: 

Dr. O's output consists of several cases (data sets) which are theinput to your program. Each input case (to your program) contains several pairs of numbers, each pair being a base and an exponent. Although each of the bases are prime numbers already, Dr. O hasn't bothered to list them in numerical order. Furthermore, sometimes he's listed the same base several times - thus a 2323 might be listed as well as a 2525. Clearly these should be coalesced into one term, 2828. Finally, Dr. O's output does not put exponents in a "superscript" position. Your job will be to fix all three of these issues with each input case given.

输入描述

Input starts with a positive integer, nn, on the first line by itself indicating the total number of test cases in the input file (i.e., the number of data sets to be processed). 

The next nn lines contain the test cases, one per line. Each test case will contain several positive integers separated by spaces on a single line. The first positive integer on each line, kk(0<k<200<k<20), will represent the number of terms in the prime factorization for that case. This will be followed by k pairs of positive integers, all separated by spaces. The first integer of each pair will be a positive prime number less than 10,000 (representing a base). The second integer of each pair will be a positive integer less than 100,000, representing the exponent to which the corresponding prime number is raised in the prime factorization given.

输出描述

For each input case, first output thefollowing header line:

Prime Factorization #mm:

where mm(1≤m≤n1≤m≤n) represents the input case number (starting with 1).The rest of the output for each case will be on the following two lines. In particular, each base will be on the second line and each exponent will be on the first line. The bases will be in ascending numerical order. The corresponding exponents will start in the following column (after its base ends) on the first row. Each following base will start in the next column after the previous exponent ends.

Leave a blank line after the output for each data set. Follow the format illustrated in Sample Output. 

提示

 

样例输入:

3
3 2 5 101 17 3 13
4 5 2 5 19 3 5 11 1
5 2 10 3 11 2 12 3 13 2 14

样例输出:

Prime Factorization #1:
 5 13     17
2 3   101

Prime Factorization #2:
 5 21   1
3 5   11

Prime Factorization #3:
 36 24
2   3

题目大意:给出t组数据,每组数据第一个数是个数,后面有2*n个数,第一个数表示底数,第二个数为指数(最后输出时间需要合并)

思路:用map数组来记录底数和指数,用两个数组来记录底数和指数之间的空格数

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int ,int>mp;
int res1[100];//记录底数的空格first;
int res2[100];//记录指数的空格second;
int f[100];
int getws(int x)
{
    int ans = 0;
    while(x){
        x/=10;
        ans++;
    }
    return ans;
}
int main()
{
    int t;
    cin >> t;
    for(int ii = 1;ii <= t;ii++){
        int n;
        mp.clear();
        cin >> n;
        int a,b;
        for(int i = 0;i < n;i++){
            cin >> a >> b;
            mp[a] += b;
        }
        int cnt = 0;
        for(auto x : mp){
            res1[cnt] = getws(x.first);
            res2[cnt] = getws(x.second);
            f[cnt] = x.first;
            cnt++;
        }
        cout << "Prime Factorization #" << ii << ":" << endl;
        for(int i = 0;i < cnt;i++){
            for(int j = 0;j < res1[i] ;j++)
                cout << " ";
            cout << mp[f[i]];
        }
        cout << endl;
        for(int i = 0;i < cnt;i++){
            cout << f[i];
//            if(i == cnt-1)//第二行最后也需要输出空格和第一行的指数对齐,否则会出现格式错误
//                break;
            for(int j = 0;j < res2[i];j++)
                cout << " ";
        }
        cout << endl << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值