#ACMORE 北京大学POJ

文章介绍了如何编写一个程序来计算给定精度和范围的实数R的幂,以及将电话号码转换成易于记忆的标准形式,以防止重复。涉及C++编程技巧和处理大数运算的问题。
摘要由CSDN通过智能技术生成

1001:Exponentiation

题目描述

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

输入解释

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

输出解释

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

输入样例
95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12
输出样例
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
提示

If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer

C++

while(cin>>s>>n)

{

...

}

c

while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want

/*while(scanf(%s%d",s,&n)!=EOF) //this also work    */

{

...

}

该题目是Virtual Judge题目,来自 北京大学POJ


题目来源 East Central North America 1988


源链接: POJ-1001  

源代码:

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

const int MAX = 200;

int main()

{

    int n;    //指数

    string str;    //储存输入中 r 的字符串方便后续

    short r[MAX] = {0};    //从低位到高位储存 r 值的数组

    while(cin >> str >> n)

    {

        int i,j,k;

        if(!n)    // 0次方直接输出1

        {

            cout << 1 << endl;

            continue;

        }

        reverse(str.begin(),str.end());    //反转字符串,方便后续去除小数点前不必要的 '0' ,以及给以低位开头的数字数组赋值

        int rdot = str.find('.');    //数字数组中小数点的位置

        if(rdot != -1)    //去除 '.' 和不必要 '0' ,调整小数点位置

        {

            str.erase(rdot,1);

            for(i = 0;i < rdot && str[i] == '0';++i);

            str.erase(0,i);

            rdot -= i;

        }

        else

        {

            rdot = 0;

        }

        int rlen = str.length();

        for(i = 0;i < rlen;++i)    //给以低位开头的数字数组赋值

        {

            r[i+1] = str[i] - '0';    //令 r[0] = 0 方便记录和计算结果小数点的位置

        }

        short tmp[MAX],ans[MAX];    // tmp[] 用于储存乘法时的另一个因数

        memcpy(ans,r,sizeof(ans));

        int anslen = rlen;    //同时作为 tmplen

        for(i = 1;i < n;++i)    //前面已进行一次方的计算,从 i = 1 开始

        {

            memcpy(tmp,ans,sizeof(tmp));

            memset(ans,0,sizeof(ans));    //将上次运算 ans[] 中的乘积移动到 tmp[] 中作为因数

            for(j = 1;j <= rlen;++j)    //运算

            {

                if(r[j])

                {

                    for(k = 1;k <= anslen;++k)

                    {

                        /*if(tmp[k])

                        {*/

                        ans[j+k-1] += r[j] * tmp[k];

                        ans[j+k] += ans[j+k-1] / 10;

                        ans[j+k-1] %= 10;

                        /*}*/

                    }

                }

            }

            anslen += rlen;    //更新 ans(tmp) 的长度

            if(!ans[anslen])    //去除最高位多余的 0 ,避免诸如 10 * 10 = 0100 , 0.09 * 0.09 = 00.0081 的情况

            {

                --anslen;

            }

        }

        int ansdot = n * rdot;    //计算结果中小数点的位置

        if((ansdot == anslen - 1 && !ans[anslen]) || ansdot > anslen)    //达成题目中 0.…… 要以 .…… 形式输出 (调皮:同时解决 ".……" 形式的输入 \( ̄︶ ̄*\)) )

        {

            anslen = ansdot;

        }

        for(i = anslen;i;--i)

        {

            if(ansdot == i)

            {

                cout << '.';

            }

            cout << ans[i];

        }

        cout << endl;    //输出结果

    }

    return 0;

}

1002:487-3279

题目描述

Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:

A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.
 

输入解释

The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.

输出解释

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.

输入样例
12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
输出样例
310-1010 2
487-3279 4
888-4567 3

该题目是Virtual Judge题目,来自 北京大学POJ


题目来源 East Central North America 1999


源链接: POJ-1002  

源代码:

#include <iostream>

#include <math.h>

#include <stdio.h>

#include <algorithm>

#define QSTYPE int

typedef struct

{

    int tel;

    int num;

}TELPAGE,*TELBOOK;

using namespace std;

//

int n;//号码的数目

int tel[100000]; //以整数形式记录号码

TELPAGE tbk[100000];//带有重记录的数组

// 函数定义

int DeleteLine();

int quicksort(QSTYPE *v, int left, int right);

int WriteToTBK();

//

// 快速排序

//

int cmp ( const void *a , const void *b )

    {

        return *(int *)a - *(int *)b;

     }

// 记录到重复统计表tbk中,返回表个数

int WriteToTBK()

{

    int i,j=0;

    //记录第一个号码

    tbk[0].num=1;

    tbk[0].tel=tel[0];

    //记录剩下的号码

    for(i=1;i<n;i++)

    {

        if(tel[i]==tel[i-1]) //重复

        {

            tbk[j].num++;

         }

        else //不重复

        {

            j++;

            tbk[j].num++;

            tbk[j].tel=tel[i];

        }

    }

    return ++j;

}

// 主程序

int main()

{

    int i,j,l;

    int mui;

    int m;//不重复的号码个数

    char tmp[40];

    scanf("%d",&n);//读入个数

    //初始化

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

    {

        tbk[i].num =0;

        tbk[i].tel =0;

        tel[i]=0;

     }

    //读入数据

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

    {

        scanf("%s",tmp);

        j=-1;mui=1000000;

        while(tmp[++j]!='\0'&&j<40)

        {

            if(tmp[j]=='-') continue;

            //替换字符

            if(tmp[j]>=65)

            {

                if(tmp[j]>=82)--tmp[j];

                tmp[j]=(tmp[j]-65)/3+48+2;

            }

            tel[i]+=mui*(tmp[j]-48);

            mui/=10;

        }

    }

    qsort(tel,n,sizeof(int),cmp);

    m=WriteToTBK(); //写到包中

    //输出数据

    int s=0;//成功计数

    for(i=0;i<m;i++)

    {

        if(tbk[i].num<=1) continue;

        printf("%03d-%04d %d",tbk[i].tel/10000,tbk[i].tel%10000,tbk[i].num);

        if(i!=m-1) printf("\n");

        s++;

    }

    if(s==0) //无重复

        printf("No duplicates.");

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值