最近复习C++,拿起ACM从头练,在此做个答案记录,大家不要盲目复制哦,要有自己的思考哦
练习地址:http://acm.hdu.edu.cn/userloginex.php
//1.2.1 Text Reverse 变量绕来绕去的,注意不要粗心就是了
#include<string>
#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
int k;
cin >> k;
getchar(); // because '\n' also is a string type, so need a getchar here
string s;
while(k--){
getline(cin, s);
int start = 0, end;
int len = s.length();
for(int i=0; i <= len; i++){
if(s[i] == ' ' || i == len){
end = i-1;
for(int j = end; j >= start; j--){
cout<<s[j];
}
start = i + 1;
if(i != len){
cout<< " ";
}else{
cout<<endl;
}
}
}
}
return 0;
}
// 1.2.2 Financial Management 这里用了一个floor方法
#include<Math.h>
#include<iostream>
using namespace std;
int main()
{
int n = 12;
double a;
double sum = 0.0;
int i = n;
while(i--)
{
cin>>a;
sum += a;
}
int res = 0;
double mean = sum * 100 / n;
int floor_mean = floor(mean);
if(mean - floor_mean < 0.5){
res = floor_mean;
}else if(mean - floor_mean >= 0.5){
res = floor_mean + 1;
}
int front = res / 100;
int behind = res - front * 100;
cout<< "$" << front << "." << behind<<endl;
return 0;
}
//1.2.3 An Easy Task 关于闰年的,有点巧妙
#include<iostream>
using namespace std;
int main()
{
int n, year, after, k;
cin>>n;
while(n--){
cin>>year>>after;
// 找到第一个闰年
while(!((year%4==0 && year%100!=0) || year%400==0)){
year += 1;
}
k = 1;
while(k < after){
year += 4;
if((year%4==0 && year%100!=0) || year%400==0){
k++;
}
}
cout<<year<<endl;
}
return 0;
}
// 1.2.4 find your present (2) 异或功能不简单,我也是看别人文章才知道的
//关键在于这里其他的数字都是出现的次数都是偶数次,可以通过异或抵消掉
//我顺便还看了两个数不通过第三个数进行相互交换的异或方法,复习了一下异或
#include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n){
// cout<<"开始"<<endl;
if(n == 0){
// cout<<"停止"<<endl;
break;
}else{
int s = 0;
int k = n;
while(k--){
int i;
cin>>i;
s^=i;
}
cout<<s<<endl;
// cout<<"结束"<<endl;
}
}
return 0;
}
//1.2.5 The Seven Percent Solution
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
while(getline(cin, s)){
if(s[0] == '#'){
break;
}
int len = s.length();
for(int i = 0; i < len; i++){
switch(s[i]){
case ' ': cout<<"%20";break;
case '!': cout<<"%21";break;
case '$': cout<<"%24";break;
case '%': cout<<"%25";break;
case '(': cout<<"%28";break;
case ')': cout<<"%29";break;
case '*': cout<<"%2a";break;
default: cout<<s[i];
}
}
cout<<endl;
}
return 0;
}
//1.2.6 Quicksum 有意思
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
int len, quicksum;
while(getline(cin, s)){
if(s[0] == '#'){
break;
}
len = s.length();
quicksum = 0;
for(int i = 0; i < len; i++){
if(s[i] != ' '){
int k = s[i] - 64;
quicksum += k * (i+1);
}
}
cout<<quicksum<<endl;
}
return 0;
}
//1.2.7 decimal system 可以的
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n, r, sum, total;
string s;
while(cin>>n){
getchar();
if(n == 0){
break;
}
total = 0;
while(n--){
getline(cin, s);
int start = s.find('(');
int end = s.find(')');
if(end-start > 2){
r = 10;
}else{
r = s[start+1] - 48;
}
sum = 0;
for(int i = 0; i < start; i++){
sum = (s[i] - 48) + sum * r;
// cout<<sum<<endl;
}
total += sum;
}
cout<<total<<endl;
}
return 0;
}
//1.2.8 Vowel Counting
#include<string>
#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
int n, len, k;
cin >> n;
getchar(); // because '\n' also is a string type, so need a getchar here
string s;
while(n--){
getline(cin, s);
int len = s.length();
for(int i=0; i <= len; i++){
switch(s[i]){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':break;
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':s[i] = char(s[i] - 32); break;
default :
if(s[i] - 0 <= 90){
s[i] = char(s[i] + 32);
}
}
}
cout<<s<<endl;
}
return 0;
}