刷紫书第三章习题(习题3-1到习题3-6)

习题3-1 Score UVA - 1585

There is an objective test result such as “OOXXOXXOOO”. An ‘O’ means a correct answer of a problem
and an ‘X’ means a wrong answer. The score of each problem of this test is calculated by itself and
its just previous consecutive ‘O’s only when the answer is correct. For example, the score of the 10th
problem is 3 that is obtained by itself and its two previous consecutive ‘O’s.
Therefore, the score of “OOXXOXXOOO” is 10 which is calculated by “1+2+0+0+1+0+0+1+2+3”.
You are to write a program calculating the scores of test results.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test
cases T is given in the first line of the input. Each test case starts with a line containing a string
composed by ‘O’ and ‘X’ and the length of the string is more than 0 and less than 80. There is no spaces
between ‘O’ and ‘X’.
Output
Your program is to write to standard output. Print exactly one line for each test case. The line is to
contain the score of the test case.
Sample Input
5
OOXXOXXOOO
OOXXOOXXOO
OXOXOXOXOXOXOX
OOOOOOOOOO
OOOOXOOOOXOOOOX
Sample Output
10
9
7
55
30

AC代码:
【坑我的点】
我把题目中的O看成了0,所以一直是Time limit exceeded

#include<cstdio>
#include<cstring>

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int sum=0;
        char s[100];
        scanf("%s",s);
        for(int i=0;i<strlen(s);)
        {
            if(s[i]=='X')i++;
            if(s[i]=='O')
            {
                int cnt=0;
                while(s[i]=='O'){i++;cnt++;sum+=cnt;}
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

例题3-2 Molar mass UVA - 1586

An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar
mass of an organic compound is the mass of one mole of the
organic compound. The molar mass of an organic compound
can be computed from the standard atomic weights of the
elements.
When an organic compound is given as a molecular formula, Dr. CHON wants to find its molar mass. A molecular
formula, such as C3H4O3, identifies each constituent element by
its chemical symbol and indicates the number of atoms of each
element found in each discrete molecule of that compound. If
a molecule contains more than one atom of a particular element, this quantity is indicated using a subscript after the chemical symbol.
In this problem, we assume that the molecular formula is represented by only four elements, ‘C’
(Carbon), ‘H’ (Hydrogen), ‘O’ (Oxygen), and ‘N’ (Nitrogen) without parentheses.
The following table shows that the standard atomic weights for ‘C’, ‘H’, ‘O’, and ‘N’.

这里写图片描述
For example, the molar mass of a molecular formula C6H5OH is 94.108 g/mol which is computed by
6 × (12.01 g/mol) + 6 × (1.008 g/mol) + 1 × (16.00 g/mol).
Given a molecular formula, write a program to compute the molar mass of the formula.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test
cases T is given in the first line of the input. Each test case is given in a single line, which contains
a molecular formula as a string. The chemical symbol is given by a capital letter and the length of
the string is greater than 0 and less than 80. The quantity number n which is represented after the
chemical symbol would be omitted when the number is 1 (2 ≤ n ≤ 99).
Output
Your program is to write to standard output. Print exactly one line for each test case. The line should
contain the molar mass of the given molecular formula.
Sample Input
4
C
C6H5OH
NH2CH2COOH
C12H22O11
Sample Output
12.010
94.108
75.070
342.296

AC代码:
【注意学习atoi,isdigit,isalpha函数】

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
       char s[100];
       scanf("%s",s);
       int c_num=0,n_num=0,h_num=0,o_num=0;
       for(int i=0;i<strlen(s);i++)
       {
           if(isalpha(s[i]))
           {
               if(s[i+1]!='\0' && isdigit(s[i+1]))
               {
                   char tmp[100];
                   int cnt=0;
                   for(int j=i+1;j<strlen(s);j++)
                   {
                        if(isdigit(s[j]))tmp[cnt++]=s[j];
                        else break;
                   }
                   tmp[cnt]='\0';
                   cnt=atoi(tmp);
                   switch(s[i])
                   {
                   case 'C':c_num+=cnt;break;
                   case 'N':n_num+=cnt;break;
                   case 'H':h_num+=cnt;break;
                   case 'O':o_num+=cnt;break;
                   }
               }
               else
               {
                   switch(s[i])
                   {
                   case 'C':c_num++;break;
                   case 'N':n_num++;break;
                   case 'H':h_num++;break;
                   case 'O':o_num++;break;
                   }
               }

           }
       }
       printf("%.3lf\n",c_num*12.01+h_num*1.008+o_num*16.00+n_num*14.01);
    }
    return 0;
}

习题3-3 Digit Counting UVA - 1225

Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence
of consecutive integers starting with 1 to N (1 < N < 10000). After that, he counts the number of
times each digit (0 to 9) appears in the sequence. For example, with N = 13, the sequence is:
12345678910111213
In this sequence, 0 appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and each
digit from 4 to 9 appears once. After playing for a while, Trung gets bored again. He now wants to
write a program to do this for him. Your task is to help him with writing this program.
Input
The input file consists of several data sets. The first line of the input file contains the number of data
sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.
For each test case, there is one single line containing the number N.
Output
For each test case, write sequentially in one line the number of digit 0, 1, … 9 separated by a space.
Sample Input
2
3
13
Sample Output
0 1 1 1 0 0 0 0 0 0
1 6 2 2 1 1 1 1 1 1

【被自己坑的一段代码】

for(int i=1;i<=n;i++)
        {
            while(i/10>0)
            {
                num[i%10]++;
                i/=10;//这个i变化后,和上面for里面的那个i是同一个,当i>9时候,造成了死循环
            }
            num[i]++;
        }

AC代码:

#include<cstdio>
#include<cstring>

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,num[15];
        memset(num,0,sizeof(num));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int ii=i;
            while(ii/10>0)
            {
                num[ii%10]++;
                ii/=10;
            }
            num[ii]++;
        }
        for(int i=0;i<10;i++)
        {
            if(i==0)printf("%d",num[i]);
            else printf(" %d",num[i]);
        }
        printf("\n");
    }
    return 0;
}

习题3-4 Periodic Strings UVA - 455

A character string is said to have period k if it can be formed by concatenating one or more repetitions
of another string of length k. For example, the string ”abcabcabcabc” has period 3, since it is formed
by 4 repetitions of the string ”abc”. It also has periods 6 (two repetitions of ”abcabc”) and 12 (one
repetition of ”abcabcabcabc”).
Write a program to read a character string and determine its smallest period.
Input
The first line oif the input file will contain a single integer N indicating how many test case that your
program will test followed by a blank line. Each test case will contain a single character string of up
to 80 non-blank characters. Two consecutive input will separated by a blank line.
Output
An integer denoting the smallest period of the input string for each input. Two consecutive output are
separated by a blank line.
Sample Input
1
HoHoHo
Sample Output
2

【感想】
本题把我坑了挺久。我的思路是枚举1到(len+1)/2这么多个周期,如果都不是,则周期必然是len。还有输出格式一定要注意,本题要求很严格。还有本题帮助理解了while(t–)中的t到底什么时候减1的,这一点有我的代码很容易体会到!另外,这道题是充分自己输入数据,根据输出情况尽心调试许久!!!

AC代码:

#include<cstdio>
#include<cstring>

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char s[100];
        scanf("%s",s);
        int len=strlen(s);
        bool flag=1;
        for(int i=1;i<=(len+1)/2;i++)//注意这里是(len+1)/2,不是len/2,否则当输入的只有一个字符时,什么都输不出来了
        {
            flag=1;
            for(int j=i;j<len;j+=i)
            {
                char s1[100],s2[100];
                strncpy(s1,s+j-i,i);
                s1[i]='\0';//这一步坑我了,刚开始写的s1[j]='\0';
                strncpy(s2,s+j,i);
                s2[i]='\0';//这一步坑我了,刚开始写的是s[j+i]='\0';
                if(strcmp(s1,s2)!=0)
                {
                    flag=0;
                    break;
                }
            }
            if(flag)
            {
                if(t==0) {printf("%d\n",i);break;}//输出格式要求很严格,被坑惨了
                else {printf("%d\n\n",i);break;}
            }

        }
        if(!flag)
        {
             if(t==0) {printf("%d\n",len);}
             else {printf("%d\n\n",len);}
        }
    }
    return 0;
}

习题3-5 Puzzle UVA - 227

A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small
squares of equal size. A unique letter of the alphabet was printed on each small square. Since there
were only 24 squares within the frame, the frame also contained an empty position which was the same
size as a small square. A square could be moved into that empty position if it were immediately to the
right, to the left, above, or below the empty position. The object of the puzzle was to slide squares
into the empty position so that the frame displayed the letters in alphabetical order.
The illustration below represents a puzzle in its original configuration and in its configuration after
the following sequence of 6 moves:
1) The square above the empty position moves.
2) The square to the right of the empty position moves.
3) The square to the right of the empty position moves.
4) The square below the empty position moves.
5) The square below the empty position moves.
6) The square to the left of the empty position moves.
Write a program to display resulting frames given their initial configurations and sequences of moves.
Input
Input for your program consists of several puzzles. Each is described by its initial configuration and
the sequence of moves on the puzzle. The first 5 lines of each puzzle description are the starting
configuration. Subsequent lines give the sequence of moves.
The first line of the frame display corresponds to the top line of squares in the puzzle. The other
lines follow in order. The empty position in a frame is indicated by a blank. Each display line contains
exactly 5 characters, beginning with the character on the leftmost square (or a blank if the leftmost
square is actually the empty frame position). The display lines will correspond to a legitimate puzzle.
The sequence of moves is represented by a sequence of As, Bs, Rs, and Ls to denote which square
moves into the empty position. A denotes that the square above the empty position moves; B denotes
that the square below the empty position moves; L denotes that the square to the left of the empty
position moves; R denotes that the square to the right of the empty position moves. It is possible that
there is an illegal move, even when it is represented by one of the 4 move characters. If an illegal move
occurs, the puzzle is considered to have no final configuration. This sequence of moves may be spread
over several lines, but it always ends in the digit 0. The end of data is denoted by the character Z.
Output
Output for each puzzle begins with an appropriately labeled number (Puzzle #1, Puzzle #2, etc.). If
the puzzle has no final configuration, then a message to that effect should follow. Otherwise that final
configuration should be displayed.
Format each line for a final configuration so that there is a single blank character between two
adjacent letters. Treat the empty square the same as a letter. For example, if the blank is an interior
position, then it will appear as a sequence of 3 blanks — one to separate it from the square to the left,
one for the empty position itself, and one to separate it from the square to the right.
Separate output from different puzzle records by one blank line.
Note: The first record of the sample input corresponds to the puzzle illustrated above.
Sample Input
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
Sample Output
Puzzle #1:
T R G S J
X O K L I
M D V B N
W P A E
U Q H C F
Puzzle #2:
A B C D
F G H I E
K L M N J
P Q R S O
T U V W X
Puzzle #3:
This puzzle has no final configuration.

【坑我之】
首先要说的是一定不要用fflush这个东西,一用就是错,反正一用就错。。然后格式注意下就可以了。。还有坑我的是switch里面我竟然用if语句哎,犯糊涂了,想了好久都不明白,今天上完课回来一看就知道了。。
AC代码如下:

#include<cstdio>
#include<cstring>

int main()
{
    int kase=0;
    while(1)
    {
        char s[10][20],ss[1005];
        fgets(s[0],10,stdin);
        if(strlen(s[0])==2 && s[0][0]=='Z')break;
        for(int i=1;i<5;i++)
            fgets(s[i],10,stdin);
        int x,y;
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
                if(s[i][j]==' '){x=i,y=j;break;}
        char c;
        int cnt=0;
        while(1)
        {
            c=getchar();
            if(c!='0')ss[cnt++]=c;
            else break;
        }
        getchar();//这里不要用fflush否则迷知错误发生,不要用fflush这个函数
        ss[cnt]='\0';
        bool flag=0;
        for(int i=0;i<strlen(ss);i++)
        {
            int xx=x,yy=y;
            //下面这个if语句不要放到switch里面去,一开始总是放在switch里面,结果老是不知道咋错的!
            if(ss[i]!='A' && ss[i]!='B' && ss[i]!='L' && ss[i]!='R' && ss[i]!='\n'){flag=1;break;}
            switch(ss[i])
            {
                //刚开始我总是把下面的if语句放在此处,其实应该放在switch外面的啊啊啊
                //if(ss[i]!='A' && ss[i]!='B' && ss[i]!='L' && ss[i]!='R' && ss[i]!='\n'){flag=1;break;}
                case 'A':if(!(x-1>=0 && x-1<5)){flag=1;break;}s[x][y]=s[x-1][y];x--;break;
                case 'B':if(!(x+1>=0 && x+1<5)){flag=1;break;}s[x][y]=s[x+1][y];x++;break;
                case 'L':if(!(y-1>=0 && y-1<5)){flag=1;break;}s[x][y]=s[x][y-1];y--;break;
                case 'R':if(!(y+1>=0 && y+1<5)){flag=1;break;}s[x][y]=s[x][y+1];y++;break;
            }
        }
        if(!flag)s[x][y]=' ';
        if(kase!=0)printf("\n");
        printf("Puzzle #%d:\n",++kase);
        if(flag)
        {
            printf("This puzzle has no final configuration.\n");
        }
        else
        {
            for(int i=0;i<5;i++)
            {
                for(int j=0;j<5;j++)
                {
                    if(j==0)printf("%c",s[i][j]);
                    else printf(" %c",s[i][j]);
                }
                printf("\n");
            }
        }
    }
    return 0;
}

习题3-6 Crossword Answers UVA - 232

A crossword puzzle consists of a rectangular grid of black and
white squares and two lists of definitions (or descriptions).
One list of definitions is for “words” to be written left to
right across white squares in the rows and the other list is for
words to be written down white squares in the columns. (A
word is a sequence of alphabetic characters.)
To solve a crossword puzzle, one writes the words corresponding to the definitions on the white squares of the grid.
The definitions correspond to the rectangular grid by
means of sequential integers on “eligible” white squares.
White squares with black squares immediately to the left or
above them are “eligible.” White squares with no squares either immediately to the left or above are also “eligible.” No other squares are numbered. All of the
squares on the first row are numbered.
The numbering starts with 1 and continues consecutively across white squares of the first row, then
across the eligible white squares of the second row, then across the eligible white squares of the third
row and so on across all of the rest of the rows of the puzzle. The picture below illustrates a rectangular
crossword puzzle grid with appropriate numbering.
An “across” word for a definition is written on a sequence of white squares in a row starting on a
numbered square that does not follow another white square in the same row.
The sequence of white squares for that word goes across the row of the numbered square, ending
immediately before the next black square in the row or in the rightmost square of the row.
A “down” word for a definition is written on a sequence of white squares in a column starting on a
numbered square that does not follow another white square in the same column.
The sequence of white squares for that word goes down the column of the numbered square, ending
immediately before the next black square in the column or in the bottom square of the column.
Every white square in a correctly solved puzzle contains a letter.
You must write a program that takes several solved crossword puzzles as input and outputs the lists
of across and down words which constitute the solutions.
Input
Each puzzle solution in the input starts with a line containing two integers r and c (1 ≤ r ≤ 10 and
1 ≤ c ≤ 10), where r (the first number) is the number of rows in the puzzle and c (the second number)
is the number of columns.
The r rows of input which follow each contain c characters (excluding the end-of-line) which describe
the solution. Each of those c characters is an alphabetic character which is part of a word or the
character ‘*’, which indicates a black square.
The end of input is indicated by a line consisting of the single number ‘0’.
Output
Output for each puzzle consists of an identifier for the puzzle (puzzle #1:, puzzle #2:, etc.) and the
list of across words followed by the list of down words. Words in each list must be output one-per-line
in increasing order of the number of their corresponding definitions.
The heading for the list of across words is ‘Across’. The heading for the list of down words is ‘Down’.
In the case where the lists are empty (all squares in the grid are black), the ‘Across’ and ‘Down’
headings should still appear.
Separate output for successive input puzzles by a blank line.
Sample Input

Sample Output
这里写图片描述

【感悟】
本题提交一次就AC了,但是阅读题目和调试,甚至是改变算法,这浪费不少时间!自身英文水平确实不行啊!

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

typedef struct
{
    int n;
    char s[20];
}node;

node s1[100],s2[100];

bool cmp(node st,node en)
{
    return st.n<en.n;
}

int main()
{
    int r,c;
    int kase=0;
    while(scanf("%d",&r)==1 && r)
    {
        scanf("%d",&c);
        char s[20][20];
        int num[20][20];
        int _num=1;
        for(int i=0;i<r;i++)
        {
            scanf("%s",s[i]);
            for(int j=0;j<c;j++)
            {
                if(s[i][j]!='*' && (i==0 || j==0 || s[i][j-1]=='*' || s[i-1][j]=='*'))
                num[i][j]=_num++;
            }
        }
        int cnt1=0,cnt2=0;
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                if(s[i][j]!='*')
                {
                    int ii=i,jj=j;
                    int cnt=0;
                    char ss[20];
                    while(s[i][j]!='*' && j<c)
                    {
                        ss[cnt++]=s[i][j++];
                    }
                    ss[cnt]='\0';
                    s1[cnt1].n=num[ii][jj];
                    strcpy(s1[cnt1++].s,ss);
                }
            }
        }
        for(int i=0;i<c;i++)
        {
            for(int j=0;j<r;j++)
            {
                if(s[j][i]!='*')
                {
                    int ii=i,jj=j;
                    int cnt=0;
                    char ss[20];
                    while(s[j][i]!='*' && j<r)
                    {
                        ss[cnt++]=s[j++][i];
                    }
                    ss[cnt]='\0';
                    s2[cnt2].n=num[jj][ii];
                    strcpy(s2[cnt2++].s,ss);
                }
            }
        }
        sort(s1,s1+cnt1,cmp);
        sort(s2,s2+cnt2,cmp);
        if(kase!=0)printf("\n");
        printf("puzzle #%d:\n",++kase);
        printf("Across\n");
        for(int i=0;i<cnt1;i++)
            printf("%3d.%s\n",s1[i].n,s1[i].s);
        printf("Down\n");
        for(int i=0;i<cnt2;i++)
            printf("%3d.%s\n",s2[i].n,s2[i].s);
    }
    return 0;
}
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值