1044. Lucky Tickets. Easy!

本文探讨了公共运输中所谓的幸运车票问题,即车票号码前半部分数字之和等于后半部分数字之和的车票。通过动态规划算法解决不同位数下幸运车票的数量计算问题,并附带介绍了全排列的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1044. Lucky Tickets. Easy!
Time limit: 2.0 second
Memory limit: 64 MB
Background
The public transport administration of Ekaterinburg is anxious about the fact that passengers don’t like to pay for passage doing their best to avoid the fee. All the measures that had been taken (hard currency premiums for all of the chiefs, increase in conductors’ salaries, reduction of number of buses) were in vain. An advisor especially invited from the Ural State University says that personally he doesn’t buy tickets because he rarely comes across the lucky ones (a ticket is lucky if the sum of the first three digits in its number equals to the sum of the last three ones). So, the way out is found — of course, tickets must be numbered in sequence, but the number of digits on a ticket may be changed. Say, if there were only two digits, there would have been ten lucky tickets (with numbers 00, 11, …, 99). Maybe under the circumstances the ratio of the lucky tickets to the common ones is greater? And what if we take four digits? A huge work has brought the long-awaited result: in this case there will be 670 lucky tickets. But what to do if there are six or more digits?
Problem
So you are to save public transport of our city. Write a program that determines a number of lucky tickets for the given number of digits. By the way, there can’t be more than nine digits on one ticket.
Input
contains a positive even integer not greater than 9.
Output
should contain a number of tickets such that the sum of the first half of digits is equal to the sum of the second half of digits.
Sample
input
4
output
670


#include <iostream>

using namespace std;

int main(){

    int A[5][45];

    int total = 0;

    A[0][0]=1;

    int n ;

    cin>>n;

    if(n<=1 || n%2 != 0){

        cout<<0<<endl;

        return 0;

    }

    n = n/2;

    int i = 0;

    int j = 0;

    int k = 0;

    for(i=0;i<n;i++){

        for(j=0;j<=i*9;j++){

            for(k=0;k<10;k++){

                A[i+1][j+k] = A[i+1][j+k] + A[i][j];

            }

        }

    }

    for(i = 0 ; i<=n*9 ; i++){

        total = total + A[n][i]*A[n][i];

    }

    cout<<total<<endl;

}




这道题开始思路错了,认为需要全排列,然后发现3=1+1+1 ,3=2+0+1 , 3=0+0+0,因此会变得复杂度很高。但是趁机复习了一下全排列。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> totalstr;


void Permutation(char * A , int begin , int end){
    if(begin == end){
        vector<string>::iterator result = find(totalstr.begin(),totalstr.end(),A);
        if(result == totalstr.end()){
            totalstr.insert(totalstr.end(), A);//去重
        }
    }else{
        for(int i = begin ; i <= end ; i++){
            char temp = A[i];
            A[i] = A[begin];
            A[begin] = temp;
            Permutation(A,begin+1,end);
            temp = A[i];
            A[i] = A[begin];
            A[begin] = temp;
        }
    }
}


int main(){
    string A;
    cin>>A;
    int end = A.size()-1;
    char * p=(char*)A.data();
    Permutation(p, 0, end);
    for(int i = 0 ; i <= totalstr.size() ; i++){
        cout<<totalstr[i]<<endl;
    }
}
然后想到了throw  http://blog.csdn.net/zzjxiaozi/article/details/6649999/ 这篇主要介绍catch(...)
try
{
//程序中抛出异常
throw value;
}
catch(valuetype v)
{
//例外处理程序段
}
throw抛出值,catch接受,当然,throw必须在“try语句块”中才有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值