hdu Acm steps Section 1

目录

a+b 1

a+b 2

a+b 3

a+b 4

a+b 5

a+b 6

a+b 7

a+b 8

总结:


a+b 1

题目

#include <iostream>
#include <string>
using namespace std;
int main() {
	while (1) {
		string str;
		getline(cin, str);
		int i = 0, a = 0, b = 0;
		if (str[0] == 0)break;
		for (; str[i] != ' '; i++) {
			a = a * 10 + str[i] - '0';
		}
		i++;
		for (; str[i] != 0; i++) {
			b = b * 10 + str[i] - '0';
		}
	//	cout << a << "    " << b << endl;
		cout << a + b << endl;

	}
	return 0;
}

为什么用getline():

        不清楚一共多少组输入,只能看最后是不是敲了回车然后判断

        getline()可以用string,cin.getline()只能输入到char数组里,感觉string处理起来比较方便

ps getline有头文件别忘了

a+b 2

题目

#include <iostream>
using namespace std;
int main() {
	long long int sum, a, b;
	cin >> sum;
	while (sum > 0) {
		//TODO
		cin >> a >> b;
		cout << a + b << endl;
		sum--;
	}
	return 0;
}

比1还简单

a+b 3

题目

#include <iostream>
using namespace std;
int main() {
	int a, b;
 
	while (1) {
		//TODO
		cin >> a >> b;
		if (a == 0 && b == 0)break;
		cout << a + b << endl;
	}
	return 0;
}

通过两个数是否均为0判断输入是否结束,也很简单

a+b 4

题目

#include <iostream>
using namespace std;
int main() {
	int sum, sumTrue = 0, temp;
	cin >> sum;
	while (sum != 0) {
		sumTrue = 0;
		for (int i = 0; i < sum; i++) {
			//TODO
			cin >> temp;
			sumTrue += temp;
		}
		cout << sumTrue << endl;
		cin >> sum;
	}
	return 0;
}

也好简单啊,sum指求和的数的个数,sumTrue指求出的和

a+b 5

题目

#include <iostream>
using namespace std;
int main() {
	int sum, count0, count1, temp;//sum 求和	count0行数	count1行中求和数字个数	temp输入的被球和数
	cin >> count0;
	while (count0 > 0) {
		sum = 0;
		cin >> count1;
		for (int i = 0; i < count1; i++) {
			//TODO
			cin >> temp;
			sum += temp;
		}
		cout << sum << endl;
		count0--;
	}
	return 0;
}

越来越简单?

a+b 6

题目

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str;
	int sum = 0, count; //sum:每行的求和 count:共有多少个被球和数
	getline(cin, str);
	while (str[0] != 0) {
		count = 0, sum = 0;
		int j = 0, temp = 0;
		while (str[j] != ' ') {
			count = count * 10 + str[j] - '0';
			j++;
		}
		j++;
		for (int i = 0; i < count; i++, j++) {
			temp = 0;
			while (str[j] != ' ' && str[j] != 0) {
				temp = temp * 10 + str[j] - '0';
				j++;
			}
			sum += temp;
		}
		cout << sum << endl;
		getline(cin, str);
	}
	return 0;
}

字符串判断有没有结束和多个数求和

a+b 7

题目

#include <iostream>
#include <string>
using namespace std;
int main() {
	while (1) {
		string str;
		getline(cin, str);
		int i = 0, a = 0, b = 0;
		if (str[0] == 0)break;
		for (; str[i] != ' '; i++) {
			a = a * 10 + str[i] - '0';
		}
		i++;
		for (; str[i] != 0; i++) {
			b = b * 10 + str[i] - '0';
		}
		//	cout << a << "    " << b << endl;
		cout << a + b << endl<<endl;

	}
	return 0;
}

a+b 8

题目

#include <iostream>
using namespace std;
int main() {
	int sum, count0, count1, temp;//sum 求和	count0行数	count1行中求和数字个数	temp输入的被球和数
	cin >> count0;
	bool first = true;
	while (count0 > 0) {
		if (!first)cout << endl;
		else first = false;
		sum = 0;
		cin >> count1;
		for (int i = 0; i < count1; i++) {
			//TODO
			cin >> temp;
			sum += temp;
		}
		cout << sum << endl ;
		count0--;
	}
	return 0;
}

注意关键词:between

总结:

老师布置的题目重复度可能很高,所以记得保存好每次作业的代码方便以后改改就交(bushi)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值