跳一跳
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int a;
int num = 0;//记录连跳次数
int sum = 0;
while (cin >> a && a != 0) {
if (a == 1) {
sum += 1;
num = 0;
}
else if (a==2)
{
sum += 2*(++num);
}
}
cout << sum;
system("pause");
}
碰撞的小球
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
struct ball
{
int station;
int direction;
int origin;
};
bool cmpweizhi(ball a, ball b) {
return a.station < b.station;
}
bool cmpchushi(ball a, ball b) {
return a.origin < b.origin;
}
int main()
{
ball myball[100];
int n,L, t;
cin >> n >> L >> t;
for (int i = 0; i < n; i++) {
cin >> myball[i].station;
myball[i].direction = 1;
myball[i].origin = i;
}
//对初始的小球位置进行排序后小球才能动,否则计算的位置不正确(前后位置不对)
sort(myball, myball+n, cmpweizhi);
for (int i = 0; i < t; i++) {
for (int j = 0; j < n; j++) {
myball[j].station += myball[j].direction;
//小球动完之后是否撞墙,撞墙方向要变
if (myball[j].station == 0 || myball[j].station== L) {
myball[j].direction = -myball[j].direction;
}
}
//小球动完之后是否和球发生了碰撞,若发生碰撞方向要变
for (int j = 1; j < n; j++) {
if (myball[j].station == myball[j - 1].station) {
myball[j].direction = -myball[j].direction;
myball[j-1].direction = -myball[j-1].direction;
}
}
}
//题目要求按照输入顺序来进行输出
sort(myball, myball + n, cmpchushi);
for (int i = 0; i < n; i++) {
cout << myball[i].station << " ";
}
system("pause");
}
买菜
/*做题思路:先在纸上把例子画出来,找规律,发现有四种符合要求的情况
如果单独列出的话会比较麻烦,而且容易出错,我开始这么做的是部分正确,等号部分处理有问题
若利用以下规律会很简单
发现重合的时间段满足a<=d&&c<=b
然后时间间隔等于min(b,d)-max(a,c);
用暴力破解的方法,列举所有的可能,因为数据量小而且测试例子不一定按照对应的顺序输入
所有要遍历一遍来匹配相近的时间段
*/
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
struct Time {
int begin;
int end;
};
int main()
{
Time H[100];
Time W[100];
int n;
cin >> n;
int sum = 0;
int jianje = 0;
for (int i = 0; i < n; i++) {
cin >> H[i].begin;
cin >> H[i].end;
}
for(int i=0;i<n;i++){
cin >> W[i].begin;
cin >> W[i].end;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (H[i].begin <= W[j].end&&H[i].end >= W[j].begin) {
jianje = min(H[i].end, W[j].end) - max(H[i].begin, W[j].begin);
sum += jianje;
}
}
}
cout << sum;
system("pause");
}
卖菜
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
int main()
{
int n;
cin >> n;
int *firstDay = new int[n];
int *secondDay = new int[n];
for (int i = 0; i < n; i++) {
cin >> firstDay[i];
secondDay[i] = 0;
}
secondDay[0] = (firstDay[0] + firstDay[1]) / 2;
secondDay[n-1]= (firstDay[n-1] + firstDay[n-2]) / 2;
for (int i = 1; i < n-1; i++) {
secondDay[i] = (firstDay[i - 1] + firstDay[i] + firstDay[i + 1]) / 3;
}
for (int i = 0; i < n; i++) {
cout << secondDay[i] << " ";
}
system("pause");
}
小中大
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
int main()
{
int n;
cin >> n;
int *array = new int[n];
for (int i = 0; i < n; i++) {
cin >> array[i];
}
int a, c;
double b;
a = max(array[0], array[n - 1]);
c = min(array[0], array[n - 1]);
if (n % 2 == 0) {
b = (array[n / 2] + array[n / 2 - 1]) / 2;
}
else
{
b = array[n / 2];
}
cout << a << " ";
if ((b - (int)b) > 0) {
cout << setiosflags(ios::fixed) << setprecision(1) << b << " ";
}
else
{
cout << (int)b << " ";
}
cout << c;
system("pause");
}
二十四点
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
int jieguo() {
int jieguo = 0;
char str[7];
int a[7];
for (int i = 0; i < 7; i++) {
cin >> str[i];
if (i % 2 == 0) {
a[i] = (int)str[i] - 48;
}
}
for (int i = 1; i < 7; i += 2) {
if (str[i]=='x') {
jieguo = a[i - 1] * a[i + 1];
a[i - 1] = a[i + 1] = jieguo;
}
if(str[i]=='/')
{ jieguo = a[i - 1] / a[i + 1];
a[i - 1] = a[i + 1] = jieguo;
}
}
if (str[1] == '+' && (str[3] == 'x' || str[3] == '/') && (str[5] == 'x' || str[5] == '/')) {
jieguo = a[0] + a[6];
//cout << jieguo<<"a";
return jieguo;
}
else if (str[1] == '-' && (str[3] == 'x' || str[3] == '/') && (str[5] == 'x' || str[5] == '/')) {
jieguo = a[0] - a[6];
//cout << jieguo<<"b";
return jieguo;
}
else {
for (int i = 1; i < 7; i += 2) {
if (str[i] == '+') {
jieguo = a[i - 1] + a[i + 1];
a[i - 1] = a[i + 1] = jieguo;
}
if (str[i] == '-')
{
jieguo = a[i - 1] - a[i + 1];
a[i - 1] = a[i + 1] = jieguo;
}
}
}
return jieguo;
}
int main()
{
int n;
cin >> n;
string *a = new string[n];
int j = 0;
for (int i = 0; i < n; i++) {
j = jieguo();
if (j == 24) {
a[i] = "Yes";
}
else
{
a[i] = "No";
}
}
for (int i = 0; i < n; i++) {
cout << a[i] << endl;
}
system("pause");
}
小明种苹果
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
struct shu {
int geshu;
int bianhao;
};
bool cmp(shu a, shu b) {
return a.geshu > b.geshu;
}
int main()
{
int N, M;
cin >> N >> M;
shu shua[100];
int a[100][100];
int sum = 0;
for (int i = 0; i < N; i++) {
//赋值之前一定要先初始化
//因为a[i][j]是待输入的不确定的所以直接相加是不对的
shua[i].geshu = 0;
for (int j = 0; j < M + 1; j++) {
cin >> a[i][j];
sum += a[i][j];
if (j != 0) {
shua[i].geshu += a[i][j];
}
}
shua[i].bianhao = i;
shua[i].geshu = -shua[i].geshu;
}
sort(shua, shua+N, cmp);
cout << sum << " " << shua[0].bianhao+1 << " " <<shua[0].geshu ;
system("pause");
}
小明种苹果(续)
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
int main()
{
int geshu=0;
int n;
cin >> n;
int cishu;
int a[100][100];
bool *b = new bool[n + 2];
int sum = 0;
int t = 0;
for (int i = 0; i < n; i++) {
cin >> cishu;
cin >> a[i][0];
t = a[i][0];
for (int j = 1; j < cishu; j++) {
cin >> a[i][j];
if (a[i][j] < 0)
t += a[i][j];
if (a[i][j] > 0) {
if (t > a[i][j])
{
b[i + 1] = true;
geshu++;
}
t = a[i][j];
}
}
sum += t;
}
b[0] = b[n];
b[n + 1] = b[1];
int zushu = 0;
if (n == 3) {
if (b[1] == true && b[2] == true && b[3] == true) {
zushu++;
}
}
else {
for (int i = 1; i <= n; i++) {
if (b[i] == true && b[i + 1] == true && b[i - 1] == true) {
zushu++;
}
}
}
cout << sum << " " << geshu << " " << zushu;
system("pause");
}
出现次数最多的数
#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
int main()
{
int b[1000] = { 0 };
int n;
cin >> n;
int shu;
for (int i = 0; i < n; i++) {
cin >> shu;
b[shu]++;
}
//输出次数最多且索引最小的
int max = 0;
int index = 0;
for (int i = 0; i < 1000; i++) {
if (b[i] > max&&b[i] != 0) {
max = b[i];
index = i;
}
}
cout << index;
system("pause");
}
//四舍五入保留一位小数cout << setiosflags(ios::fixed) << setprecision(1) << b << " ";