紫书第三章习题

1. Score UVA - 1585题目链接: 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 s...
摘要由CSDN通过智能技术生成

 

 

 

1. Score   UVA - 1585

 题目链接: 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代码
#include<iostream>
using namespace std;
const int maxn=100;
int main()
{
	char p[maxn];
	int n,num,sum;
	cin>>n;
	while(n--){
		scanf("%s",p);
		num=0,sum=0;
		for(int i=0;p[i];++i)
			if(p[i]=='O') ++num,sum+=num;
			else if(p[i]=='X') num=0;
		cout<<sum<<endl;
	}
	return 0;
}

 

 

2.Molar mass      UVA - 1586

题目链接: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代码:

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=80+9;
double num(char p)//取原子质量 
{
	if(p=='C') return 12.01;
	if(p=='H') return 1.008;
	if(p=='O') return 16;
	if(p=='N') return 14.01;
}
int main()
{
	int n;
	cin>>n;
	while(n--){
		char pq[maxn],tmp[maxn];
		scanf("%s",pq);
		int pos=0;
		double sum=0,weight;
		while(pq[pos]){
			if(pq[pos]>='A'&&pq[pos]<='Z'){
				weight=num(pq[pos++]);
				int jk=1,cnt=0;
				while(pq[pos]>='0'&&pq[pos]<='9')
					tmp[cnt++]=pq[pos++];
				tmp[cnt]='\0';
				if(cnt>0) jk=atoi(tmp);//避免对空字符串操作 
				sum+=jk*weight;
			}
		}
		printf("%.3lf\n",sum);
	}
	return 0;
}

3.Digit Counting  UVA - 1225

题目链接: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

问题分析:查表,从n到n+1时先将n时的情况拷贝,再处理数n+1

AC代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int pq[10009][10];
int main()
{
	memset(pq,0,sizeof(pq));
	for(int i=1;i<=10000;++i){
		for(int j=0;j<10;++j) pq[i][j]=pq[i-1][j];//处理当前情况之前先将上一次的拷贝 
		int yu=i;
		while(yu>0){ //处理当前数i 
			++pq[i][yu%10];
			yu/=10;
		}
	}
	int num,n;
	cin>>num;
	while(num--){
		cin>>n;
		for(int i=0;i<10;++i)
			if(!i) cout<<pq[n][i];
			else cout<<" "<<pq[n][i];
		cout<<endl;
	}
	return 0;
} 

 

4.Periodic Strings   UVA - 455

问题链接:UVA-455

问题描述:

A character string is said to have period k if it can be formed by concatenating one or more repetit

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值