NOIP2008 火柴棒等式(字符和整形转换)

1799: 【穷举】火柴棒等式

时间限制: 1 Sec   内存限制: 128 MB
提交: 130   解决: 47
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A、B、C是用火柴棍拼出的整数(若该数非零,则最高位不能是0)。用火柴棍拼数字0-9的拼法如图所示:
 
注意:
1. 加号与等号各自需要两根火柴棍
2. 如果A≠B,则A+B=C与B+A=C视为不同的等式(A、B、C>=0)
3. n根火柴棍必须全部用上

输入

输入共一行,又一个整数n(n<=24)。

输出

输出共一行,表示能拼成的不同等式的数目。

样例输入

14

样例输出

2

提示

2个等式为0+1=1和1+0=1。


整数转字符串函数

itoa(int, string, 10);        //十进制整形转字符串

ltoa(int, string, 10);        //十进制长整型转字符串

ultoa(int, string, 10);       //十进制无符号长整型转字符串

gcvt();                             //浮点型转字符串(四舍五入)

ecvt();                              //双精度转字符串(不含小数点)

sprintf(string, "%d:%d:%d", a, b, c);          

itoa不是一个标准的C函数,它是Windows特有的,若写跨平台程序,用sprintf();

字符串转整形函数

int/long long/...  n = ...

atoi(string)                        //字符串转整型

atol(string)                        //字符串转长整型

strtod(string)                    //字符串转双精度浮点型,并报告不能被转换的剩余数字

strtol(string)                     //字符串转换为长整型,并报告不能被转换的剩余数字

strtoul(string)                   //字符串转无符号长整型,并报告不能被转换的剩余数字

sscanf(string, "%d", a);

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int a[] = {6,2,5,5,4,5,6,3,7,6};
int change(int n)
{
    char str[5];
    string b;
    itoa(n, str, 10);
    b = str;
    int sum = 0;
    for(int i=0; i<b.size(); i++)
    {
        sum += a[b[i]-'0'];
    }
    return sum;
}
int main()
{
    int n;
    while(cin >> n)
    {
        n -= 4;
        int tot=0;
        for(int i=0; i<=1000; i++)
        {
            for(int j=0; j<=1000; j++)
            {
                if(change(i)+change(j)+change(i+j) == n)
                {
                    //cout << i << " " << j << endl;
                    tot ++;
                }
            }
        }
        cout << tot << endl;
    }
}
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int n;
    int a[30] = {0};
    a[13] = 1;
    a[14] = 2;
    a[15] = 8;
    a[16] = 9;
    a[17] = 6;
    a[18] = 9;
    a[19] = 29;
    a[20] = 39;
    a[21] = 38;
    a[22] = 65;
    a[23] = 88;
    a[24] = 128;
    while(cin >> n)
    {
        cout << a[n] << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/a_thinking_reed_/article/details/79781174
今日推荐