1005 Spell It Right (20分)
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 (≤10^100).
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
解题
用字符串类型保存数字,求各个位数的和,转化为字符类型,按顺序挨个输出即可;
#include<iostream>
#include<cstring>
using namespace std;
string Num;
void input()
{
cin>>Num;
//计算位数
int s=0;
for(int i=0;i<Num.size();i++){
s+=Num[i]-'0';
}
string R=to_string(s);
for(int i=0;i<R.size();i++)
{
switch(R[i]){
case '1':
cout<<"one";
break;
case'2':
cout<<"two";
break;
case'3':
cout<<"three";
break;
case'4':
cout<<"four";
break;
case'5':
cout<<"five";
break;
case'6':
cout<<"six";
break;
case'7':
cout<<"seven";
break;
case'8':
cout<<"eight";
break;
case'9':
cout<<"nine";
break;
case'0':
cout<<"zero";
break;
}
if(i!=R.size()-1) cout<<" ";
}
}
int main()
{
input();
}
不用switch短一点;
#include<iostream>
#include<cstring>
using namespace std;
string Word[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
string Num;
void input()
{
cin>>Num;
//计算位数
int s=0;
for(int i=0;i<Num.size();i++){
s+=Num[i]-'0';
}
string R=to_string(s);
for(int i=0;i<R.size();i++)
{
cout<<Word[R[i]-'0'];
if(i!=R.size()-1) cout<<" ";
}
}
int main()
{
input();
}
1006 Sign In and Sign Out (25分)
At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.
Input Specification:
Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:
ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.
Output Specification:
For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.
Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.
Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133
解题
熟悉string类型的输入输出;
#include<iostream>
#include<cstring>
using namespace std;
int M;
string opuser;
string cluser;
void input()
{
scanf("%d",&M);
string user;
int earliest=90909;
int latest=0;
int a,b,c;
int tmp1,tmp2;
int t;
for(int i=0;i<M;i++)
{
user.resize(100);
scanf("%s %d:%d:%d",&user[0],&a,&b,&c);
t=60*60*a+60*b+c;
if (t<earliest)
{
earliest=t;
opuser=user;
}
scanf("%d:%d:%d",&a,&b,&c);
t=60*60*a+60*b+c;
if (t>latest)
{
latest=t;
cluser=user;
}
}
}
int main()
{
input();
printf("%s %s",opuser.c_str(),cluser.c_str());
}
注意点
scanf输入string类型时需要先resize空间给string类型,然后输入地址为&user[0];
printf输出string类型需要输出user.c_str(),才能输出string类型;
因为printf和scanf为C语言代码,无法直接输出string类型;
或者用cin,cout;
可以用cin.get(),读入:分隔符;
这篇博客介绍了两道编程题目,1005 Spell It Right 要求计算非负整数的各位数字之和并用英文单词输出,而1006 Sign In and Sign Out 则需要找出每天解锁和锁定计算机房门的人员ID。对于1005题,可以使用字符串存储数字,逐位求和并转换为英文输出。1006题中,要处理签到和签退记录,确保正确处理时间顺序,找出首签到和末签退的人。
719

被折叠的 条评论
为什么被折叠?



