天梯题目解答--入门菜鸟

http://wikioi.com/  天梯网址

很不错的在线编程网站,原来做过一点ACM,这个UI实在是比当时的ACM好太多,我个人比较纠结UI,还是很喜欢这个网站所以~~

下面整理一下做过的题,做完一些整理一次,不会太多集中在一篇也不会一篇就一道:)


一、入门菜鸟

1012. 题目描述 Description

输入n个数,n<=100,找到其中最小的数和最大的数

输入描述

第一行一个整数n

接下来一行n个整数,每个整数不超过231 -1

输出描述

最小和最大的数

::为了不要多占内存既然n<=100那么用unsigned int就行,开始判断一下n因为如果是0那么后面没有比较直接返回就好,代码如下

#include <iostream>
using namespace std;
int main(){
    unsigned int n;
    cin>>n;
    if(0==n)
        return 0;
    int m[2],t;
    for(int i=0;i<n;i++){
        if(0==i){
            cin>>t;
            m[0] = t;
            m[1] = t;
        }
        else{
            cin>>t;
            if(t<m[0])
                m[0] = t;
            if(t>m[1])
                m[1] = t;
        }
    }
    cout<<m[0]<<' '<<m[1]<<endl;        
}

1202. 题目描述 Description

求n个数的和

输入描述 Input Description

第一行一个整数n

接下来一行n个整数

输出描述 Output Description

所有数的和

这里的题刚开始还是很简单的不会涉及大整数,所以int就好,之后就是直接加,只需要一个临时用的t

#include <iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    if(0==n)
        return 0;
    int sum=0,t;
    for(int i=0;i<n;i++){
        cin>>t;
        sum+=t;
    }
    cout<<sum<<endl;
}

1203.题目描述 Description

 给出两个浮点数,请你判断这两个浮点数是否相等

输入描述 Input Description

 输入仅一行,包含两个浮点数

输出描述 Output Description

 输出仅一行,如果相等则输出yes,否则输出no。

提示: 我们一般认为两个浮点数相等,当且当他们之间的误差不超过1e-8。

既然是比较,直接相减会涉及到正负,这里特地写的宏,检验下自己在写宏的时候会不会粗心漏了括号(结果还真漏了一个T_T)

C++按说最好不要用宏,可以用常量或者内联函数代替的就代替了,这里练习一下:)

#include <iostream>
#define ABS(x) ((x)<0?(-(x)):(x))
using namespace std;
int main(){
    double a,b;
    cin>>a>>b;
    if(ABS(a-b)<=1e-8)
        cout<<"yes";
    else
        cout<<"no";
}

1204.题目描述 Description

给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置。

输入描述Input Description

仅一行包含两个字符串a和b

输出描述Output Description

仅一行一个整数

直接用string自己的函数了,比较简单

#include <iostream>
#include <string>
using namespace std;
int main(){
    string a,b;
    cin>>a>>b;
    int loc;
    loc = a.find(b,0);
	if(a.npos==loc)
        return 0;
    else
        cout<<(loc+1);
}

1205.题目描述 Description

给出一个英语句子,希望你把句子里的单词顺序都翻转过来

输入描述Input Description

输入包括一个英语句子。

输出描述Output Description

按单词的顺序把单词倒序输出

#include<iostream>
#include<vector>
using namespace std;
int main(){
    vector<string> ss;
    string t;
    while(cin>>t)
        ss.push_back(t);
    if(0==ss.size())
        return 0;
    vector<string>::iterator iter;
    for(iter=ss.end()-1;iter!=ss.begin();iter--)
        cout<<(*iter)<<" ";
    cout<<(*iter);
}

1206.题目描述 Description

保留两位小数输出一个浮点数。

输入描述 Input Description

一个浮点数。double范围内

输出描述 Output Description

保留两位小数输出

先贴我自己的代码

#include <iostream>
#include "stdio.h"
using namespace std;
int main(){
    double a;
    cin>>a;
    printf("%.2f",a);
}
但是感觉题目不应该是这么简单吧,后来看同学有人用的流控制,感觉出这个题如果是考这个倒是有些难度,但是菜鸟不应该到这种吧。。。

#include<iomanip>

cout<<fixed<<setprecision(2)<<s<<endl;

2235.题目描述 Description

.输入机票原价(3到4位的正整数,单位:元),再输入机票打折率(小数点后最多一位数字)。编程计算打折后机票的实际价格(单位:元。计算结果要将个位数四舍五入到十位数“元”)。输入只有一行两个数(两数间用一个空格分隔),第一个为整数,表示机票原价,第二个整数或实数(如是实数,小数点后最多1位数字)表示打折率。

输入样例1:

888  7

输出样例1:

620

其实就是四舍五入然后去掉个位的数,可以这样想,int没有小数点后面,让这个数先除以10,化成int那么就保证个位以上都是保留,然后这个数乘以10那么个位一定是0,四舍五入到10位,那么加的应该是50,然后就很简单了

#include <iostream>
using namespace std;
int main(){
    unsigned int op;
    float d;
    cin>>op>>d;
    cout<<(int((op*d+50)/100)*10);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值