当前位置: 代码迷 >> 综合 >> 杭电ACM基础题(1323、1391、1390、1330、1334、1335)
  详细解决方案

杭电ACM基础题(1323、1391、1390、1330、1334、1335)

热度:11   发布时间:2023-12-29 06:16:31.0

文章目录

    • 1323、Perfection[一个正整数的因数之和是否等于其自身]
    • 1391、Number Steps[给定横纵坐标,找规律,求其表示的值]
    • 1390、Binary Numbers[将一个十进制数表示为二进制数,计算1在二进制中的位置]
    • 1330、Deck[计算(1/2+1/4+...1/n)的值]
    • 1334、Perfect Cubes[寻找使得a^3^=b^3^+c^3^+d^3^成立的等式]
    • 1335、Basically Speaking[将一个任意进制转化为任意进制]

1323、Perfection[一个正整数的因数之和是否等于其自身]

From the article Number Theory in the 1994 Microsoft Encarta: “If a, b, c are integers such that a = bc, a is called a multiple of b or of c, and b or c is called a divisor or factor of a. If c is not 1/-1, b is called a proper divisor of a. Even integers, which include 0, are multiples of 2, for example, -4, 0, 2, 10; an odd integer is an integer that is not even, for example, -5, 1, 3, 9. A perfect number is a positive integer that is equal to the sum of all its positive, proper divisors; for example, 6, which equals 1 + 2 + 3, and 28, which equals 1 + 2 + 4 + 7 + 14, are perfect numbers. A positive number that is not perfect is imperfect and is deficient or abundant according to whether the sum of its positive, proper divisors is smaller or larger than the number itself. Thus, 9, with proper divisors 1, 3, is deficient; 12, with proper divisors 1, 2, 3, 4, 6, is abundant.”
Given a number, determine if it is perfect, abundant, or deficient.
Input
A list of N positive integers (none greater than 60,000), with 1 < N < 100. A 0 will mark the end of the list.
Output
The first line of output should read PERFECTION OUTPUT. The next N lines of output should list for each input integer whether it is perfect, deficient, or abundant, as shown in the example below. Format counts: the echoed integers should be right justified within the first 5 spaces of the output line, followed by two blank spaces, followed by the description of the integer. The final line of output should read END OF OUTPUT.
Sample Input

15 28 6 56 60000 22 496 0

Sample Output

PERFECTION OUTPUT15  DEFICIENT28  PERFECT6  PERFECT56  ABUNDANT
60000  ABUNDANT22  DEFICIENT496  PERFECT
END OF OUTPUT

Code
1、判断一个正整数的因数之和是否等于其自身,要初始化,不然会WA

//判断一个正整数的因数之和是否等于其自身
//右对齐,5个空格,setiosflags(ios::right) setw(5)
#include<iostream>
#include<iomanip>
using namespace std;//求一个正整数的因数之和 
int factor(int n){
    int sum=0,m=n;for(int i=1;i<n;i++){
    if(n%i==0){
    sum=sum+i;}}if(sum==m){
    return 1;//perfect}else if(sum<m){
    return 2;//deficient}else{
    return 3;//aboundant}
}int main() {
    int num[100],len=0;for(int i=0;i<100;i++){
    cin>>num[i];if(num[i]==0){
    break;}len++;}for(int i=0;i<len;i++){
    int res=factor(num[i]);cout<<setiosflags(ios::right);if(i==0){
    cout<<"PERFECTION OUTPUT"<<endl;}if(res==1){
    cout<<setw(5)<<num[i]<<" PERFECT"<<endl;}else if(res==2){
    cout<<setw(5)<<num[i]<<" DEFICIENT"<<endl;}else{
    cout<<setw(5)<<num[i]<<" ABUNDANT"<<endl;}if(i==len-1){
    cout<<"END OF OUTPUT"<<endl;}}return 0; 
}

1391、Number Steps[给定横纵坐标,找规律,求其表示的值]

Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,… as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3, 1) respectively and this pattern has continued.
在这里插入图片描述
You are to write a program that reads the coordinates of a point (x, y), and writes the number (if any) that has been written at that point. (x, y) coordinates in the input are in the range 0…5000.
Input
The first line of the input is N, the number of test cases for this problem. In each of the N following lines, there is x, and y representing the coordinates (x, y) of a point.
Output
For each point in the input, write the number written at that point or write No Number if there is none.
Sample Input

3
4 2
6 6
3 4

Sample Output

6
12
No Number

Code:
给出坐标(x,y),计算其代表什么,比较简单–找规律

//给出坐标(x,y),计算其代表什么值
#include<iostream>
using namespace std;int main(){
    int n;cin>>n;int x,y;//横纵坐标(0,5000)for(int i=0;i<n;i++){
    cin>>x>>y;if((x+y)%2==0&&(x==y||x-y==2)){
    if(x%2==0&&y%2==0){
    cout<<x+y<<endl;}else{
    cout<<x+y-1<<endl;}}else{
    cout<<"No Number"<<endl;}}return 0; 
} 

1390、Binary Numbers[将一个十进制数表示为二进制数,计算1在二进制中的位置]

Given a positive integer n, find the positions of all 1’s in its binary representation. The position of the least significant bit is 0.

Example

The positions of 1’s in the binary representation of 13 are 0, 2, 3.

Task

Write a program which for each data set:

reads a positive integer n,

computes the positions of 1’s in the binary representation of n,

writes the result.
Input
The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.

Each data set consists of exactly one line containing exactly one integer n, 1 <= n <= 10^6.
Output
The output should consists of exactly d lines, one line for each data set.

Line i, 1 <= i <= d, should contain increasing sequence of integers separated by single spaces - the positions of 1’s in the binary representation of the i-th input number.
Sample Input

1
13

Sample Output

0 2 3

Code
将一个十进制数表示为二进制数,计算1在二进制中的位置

//将一个十进制数表示为二进制数,计算1在二进制中的位置
#include<iostream>
#include<vector>
using namespace std;int main(){
    int d;//d[1,10]int n;//[1,10^6]cin>>d;for(int i=0;i<d;i++){
    cin>>n;//将n转化为二进制int t; vector<int>binary;while(n!=0){
    t=n%2;binary.push_back(t);n=n/2;}bool flag=true;for(int k=0;k<binary.size();k++){
    //注意格式,最后一个值之后没有空格 if(binary[k]==1){
    if(flag==true){
    cout<<k;flag=false;    }else{
    cout<<" "<<k;}}}cout<<endl;}
} 

1330、Deck[计算(1/2+1/4+…1/n)的值]

A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table’s edge, and half the length of the card hangs over the edge of the table. If the card hung any further out, with its center of gravity off the table, it would fall off the table and flutter to the floor. The same reasoning applies if the card were placed on another card, rather than on a table.

Two playing cards can be arranged, carefully, with short edges parallel to table edges, to extend 3/4 of a card length beyond the edge of the table. The top card hangs half a card length past the edge of the bottom card. The bottom card hangs with only 1/4 of its length past the table’s edge. The center of gravity of the two cards combined lies just over the edge of the table.

Three playing cards can be arranged, with short edges parallel to table edges, and each card touching at most one other card, to extend 11/12 of a card length beyond the edge of the table. The top two cards extend 3/4 of a card length beyond the edge of the bottom card, and the bottom card extends only 1/6 over the table’s edge; the center of gravity of the three cards lines over the edges of the table.

If you keep stacking cards so that the edges are aligned and every card has at most one card above it and one below it, how far out can 4 cards extend over the table’s edge? Or 52 cards? Or 1000 cards? Or 99999?
Input
Input contains several nonnegative integers, one to a line. No integer exceeds 99999.
Output
The standard output will contain, on successful completion of the program, a heading:

Cards Overhang

(that’s two spaces between the words) and, following, a line for each input integer giving the length of the longest overhang achievable with the given number of cards, measured in cardlengths, and rounded to the nearest thousandth. The length must be expressed with at least one digit before the decimal point and exactly three digits after it. The number of cards is right-justified in column 5, and the decimal points for the lengths lie in column 12.
Sample Input

1
2
3
4
30

Sample Output

The line of digits is intended to guide you in proper output alignment, 
and is not part of the output that your solution should produce. 12345678901234567
# Cards Overhang1     0.5002     0.7503     0.9174     1.04230     1.997

Code
给定n 计算(1/2+1/4+…1/n)的值

//给定n 计算(1/2+1/4+...1/n)的值
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
    int n,count=0;while(cin>>n){
    double sum=0,a=2;count++;for(int i=0;i<n;i++){
    sum=sum+1/a;a+=2;}cout<<setiosflags(ios::fixed);if(count==1){
    cout<<"# Cards Overhang"<<endl;cout<<setw(5)<<n;cout<<setw(10)<<setiosflags(ios::right)<<setprecision(3)<<sum<<endl;}else{
    cout<<setw(5)<<n;cout<<setw(10)<<setiosflags(ios::right)<<setprecision(3)<<sum<<endl;}}
} 

1334、Perfect Cubes[寻找使得a3=b3+c3+d3成立的等式]

For hundreds of years Fermat’s Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believed to be correct, though it is still undergoing scrutiny.) It is possible, however, to find integers greater than 1 that satisfy the ``perfect cube’’ equation a^3 = b^3 + c^3 + d^3 (e.g. a quick calculation will show that the equation 12^3 = 6^3 + 8^3 + 10^3 is indeed true). This problem requires that you write a program to find all sets of numbers {a, b, c, d} which satisfy this equation for a <= 200.
Output
The output should be listed as shown below, one perfect cube per line, in non-decreasing order of a (i.e. the lines should be sorted by their a values). The values of b, c, and d should also be listed in non-decreasing order on the line itself. There do exist several values of a which can be produced from multiple distinct sets of b, c, and d triples. In these cases, the triples with the smaller b values should be listed first.

The first part of the output is shown here:

Cube = 6, Triple = (3,4,5)
Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 18, Triple = (9,12,15)
Cube = 19, Triple = (3,10,18)
Cube = 20, Triple = (7,14,17)
Cube = 24, Triple = (12,16,20)

Note: The programmer will need to be concerned with an efficient implementation. The official time limit for this problem is 2 minutes, and it is indeed possible to write a solution to this problem which executes in under 2 minutes on a 33 MHz 80386 machine. Due to the distributed nature of the contest in this region, judges have been instructed to make the official time limit at their site the greater of 2 minutes or twice the time taken by the judge’s solution on the machine being used to judge this problem.
Code1
1、pow(a,3),调用pow函数会超时,不要随便调用外部的格式

//寻找使得a^3=b^3+c^3+d^3成立的等式
#include<iostream>
#include<math.h>
using namespace std;
int main(){
    int a,b,c,d;for(int a=2;a<=200;a++){
    for(int b=2;b<a;b++){
    for(int c=b+1;c<a;c++){
    for(int d=c+1;d<a;d++){
    if(pow(a,3)==pow(b,3)+pow(c,3)+pow(d,3)){
    cout<<"Cube="<<a<<",Triple=("<<b<<","<<c<<","<<d<<")"<<endl;}}}}}return 0;
} 

Code2
1、不调用pow()函数不会超时

//寻找使得a^3=b^3+c^3+d^3成立的等式
#include<iostream>using namespace std;
int main(){
    int a,b,c,d;for(int a=2;a<=200;a++){
    for(int b=2;b<a;b++){
    for(int c=b+1;c<a;c++){
    for(int d=c+1;d<a;d++){
    if((a*a*a)==(b*b*b)+(c*c*c)+(d*d*d)){
    cout<<"Cube = "<<a<<", Triple = ("<<b<<","<<c<<","<<d<<")"<<endl;}}}}}return 0;
} 

1335、Basically Speaking[将一个任意进制转化为任意进制]

The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:
It will have a 7-digit display.

Its buttons will include the capital letters A through F in addition to the digits 0 through 9.

It will support bases 2 through 16.
Input
The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the end of file is reached.
Output
The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is to large to appear on the display, then print "ERROR’’ (without the quotes) right justified in the display.
Sample Input

1111000  2 10
1111000  2 16
2102101  3 10
2102101  3 1512312  4  21A 15  2
1234567 10 16ABCD 16 15

Sample Output

    1207817657CAERROR1100112D687D071

Code:
进制转化 如输入1111000 2 10 表示将2进制数1111000转化为10进制数;注意边界值

//进制转化 如输入1111000 2 10 表示将2进制数1111000转化为10进制数
#include<iostream>
#include<string.h>
#include<cmath>
#include<iomanip>
using namespace std;//将n进制转化为十进制
int convert_ten(char c[],int n){
    int len=strlen(c),result=0,m;for(int i=len-1,j=0;i>=0;i--,j++){
    switch(c[i]){
    case 'A':m=10;break;case 'B':m=11;break;case 'C':m=12;break;case 'D':m=13;break;case 'E':m=14;break;case 'F':m=15;break;default:m=c[i]-'0';}    result=result+m*pow(n,j);}return result;
} 
//将十进制转化为n进制,并输出 
int ten_convert(int a,int n){
    int num[7],i=0,result=0;while(a!=0){
    num[i]=a%n;i++;a=a/n;if(i>7){
    cout<<setiosflags(ios::right)<<setw(7)<<"ERROR"<<endl;return 1;}}    char c[8];int j=0;//将转化后的结果保存在c[]数组中 for(int k=i-1;k>=0;k--){
    switch(num[k]){
    case 10:c[j++]='A';break;case 11:c[j++]='B';break;case 12:c[j++]='C';break;case 13:c[j++]='D';break;case 14:c[j++]='E';break;case 15:c[j++]='F';break;default:c[j++]=num[k]+'0';//将数转化为字符 }}c[j]='\0';cout<<setw(7)<<c<<endl;return 0;
} int main(){
    char num[8];int base1,base2;//将num[] 中base1进制的值转化base2的值 while(cin>>num>>base1>>base2){
    int a=convert_ten(num,base1);//将base1进制转化为10进制 ten_convert(a,base2);//将10进制转化为base2进制 } return 0;
}