【PAT甲】A1001/A1005/A1045

22 篇文章 0 订阅
/*
*时间:2018年4月29日20:30:52-2018年4月29日20:49:21
*题目:1001.A+B Format
*分数:20
*编译器:g++
*题目描述:
Calculate a + b and output the sum in standard format -- that is, the digits must be separated into 
groups of three by commas (unless there are less than four digits).

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a,
 b <= 1000000. The numbers are separated by a space.

Output

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input

-1000000 9

Sample Output

-999,991
*/
//思路:将sum分开成每个位比较好处理,一开始如何从个位开始每三位输出一个逗号卡住了,应为我们都是从高位开始输出到低位的
//后来想了下,从总体的来看,位数在3的倍数的数后面跟一个逗号就可以了
#include <iostream>
using namespace std;
#include <vector>
int main()
{
	long a;
	long b;
	cin>>a>>b;
	long sum = a + b;
	if (sum < 0)//sum小于0的情况,都用正数来处理比较方便
	{
		cout<<"-";
		sum = -sum;
	}
	vector<int> v;
	if (sum == 0) cout<<"0";//sum为0的情况
	while (sum)//把sum分成每个位
	{
		v.push_back(sum % 10);
		sum = sum / 10;
	}
	for(int i=v.size()-1; i>=0; i--)
	{
		cout<<v[i];
		if(i%3 == 0 && i != 0) cout<<",";//位数在3的倍数的后面加一个逗号
	}
	return 0;
}





/*
*时间:2018年4月29日20:54:07-2018年4月29日21:08:55
*题目:1005.Spell It Right 
*分数:20
*编译器:g++
*题目描述:
Given a non-negative integer N, your task is to compute the sum of all the digits of N, 
and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space
 between two consecutive words, but no extra space at the end of a line.
Sample Input:

12345

Sample Output:

one five
*/
//思路:建一个数组,数字对应相应的英文,把和分成每个位,从高到低输出每个位对应的英文就ok
#include <iostream>
using namespace std;
#include <string>
#include <vector>
string digChar[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main()
{
	string num;//用字符类型来处理输入的数字
	cin>>num;
	int sum = 0;
	for (int i=0; i<num.size(); i++)//将每个位加起来
		sum += num[i] - '0';
	vector<int> v;
	if (sum == 0) cout<<digChar[0];
	while(sum)//加起来之后的又分成每个int的位
	{
		v.push_back(sum % 10);
		sum /= 10;
	}
	for (int i=v.size()-1; i>=0; i--)//输出每个位对应的英文,注意一下最后不要空格
	{
		if(i == 0) cout<<digChar[v[0]];
		else cout<<digChar[v[i]]<<" ";
	}
	return 0;
}


/*
*时间:2018年4月29日21:10:40-2018年4月29日21:39:36
*题目:1035.Spell It Right 
*分数:20
*编译器:g++
*题目描述:
To prepare for PAT, the judge sometimes has to generate random passwords for the users. 
The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase),
 or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, 
 l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge,
 and to help the juge modify the confusing passwords.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N (<= 1000), followed by N lines of accounts. 
Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.

Output Specification:

For each test case, first print the number M of accounts that have been modified, then print in the following M lines 
the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts 
must be printed in the same order as they are read in. If no account is modified, print in one line
 "There are N accounts and no account is modified" where N is the total number of accounts. However, 
 if N is one, you must print "There is 1 account and no account is modified" instead.

Sample Input 1:

3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa

Sample Output 1:

2
Team000002 RLsp%dfa
Team000001 R@spodfa

Sample Input 2:

1
team110 abcdefg332

Sample Output 2:

There is 1 account and no account is modified

Sample Input 3:

2
team110 abcdefg222
team220 abcdefg333

Sample Output 3:

There are 2 accounts and no account is modified
*/
//思路:对每个输入的密码遍历,看看是否有1i0O这四个字符,有就按照规则改掉
//没有一个是需要改的话一定要注意单复数的are和accounts
#include <iostream>
using namespace std;
#include <vector>
#include <string>
struct user{
	string name;
	string pw;
};
int main()
{
	int N;
	cin>>N;
	user usr[N];
	vector<int> modifiedID;//改动过的编号
	for (int i=0; i<N; i++)
	{
		cin>>usr[i].name>>usr[i].pw;
		int modifiedNum = usr[i].pw.size();
		for (int j=0; j<usr[i].pw.size(); j++)//对每个pw进行遍历
		{
			//cout<<"modifiedNum: "<<modifiedNum<<endl;
			if (usr[i].pw[j] == '1') usr[i].pw[j] = '@';
			else if(usr[i].pw[j] == 'l') usr[i].pw[j] = 'L';
			else if(usr[i].pw[j] == '0') usr[i].pw[j] = '%';
			else if(usr[i].pw[j] == 'O') usr[i].pw[j] = 'o';
			else modifiedNum--;//如果减到0表示没有一个字符需要修改的
		}
		if (modifiedNum) modifiedID.push_back(i);//没有减到0,就把这个编号压入到改动过的编号
	}
	if (modifiedID.size() == 0 && N == 1) cout<<"There is "<<N<<" account and no account is modified"<<endl;//单数is和account
	else if (modifiedID.size() == 0 && N >1) cout<<"There are "<<N<<" accounts and no account is modified"<<endl;//注意单复数呀,are和accouts
	else {
		cout<<modifiedID.size()<<endl;
		for(int i=0; i<modifiedID.size(); i++)
		{
			cout<<usr[modifiedID[i]].name<<" "<<usr[modifiedID[i]].pw<<endl;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值