1、A - A+B for Input-Output Practice (I)
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in on## 标题e line, and with one line of output for each line in input.
Sample
input | output |
---|---|
1 5 | 6 |
10 20 | 30 |
分析
对输入的两个数求和并输出
理解
这里这道题刚开始想的过于简单,写成了一个数据的a+b计算,再仔细读题才发现是要用多组数据。具体原因见–》http://t.csdn.cn/3BVwD
代码
#include<stdio.h>
int main()
{
int a,b;
while(~scanf("%d %d",&a,&b)){ //多组输入
printf("%d",a+b);
}
}
多组输入
while(~scanf(“%d %d”,&a,&b)){ }
2、B - A+B for Input-Output Practice (II)
Input
Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample
input | output |
---|---|
2 | – |
1 5 | 6 |
10 20 | 30 |
分析
读入n组数据,输出求和结果。
理解
与上道题目相比,有了n的值,在循环的书写上会轻松。
代码
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a, c;
while (n--) { //循环
cin >> a >> c;
cout << a + c << endl;
}
return 0; } c++
#include<stdio.h>
int main() {
int a,b,i,n,s[200];
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d %d",&a,&b);
s[i]=a+b;
}
for(i=0;i<n;i++){
if(a!=0||b!=0)
printf("%d\n",s[i]);
}
}
c
总的来说,c++要比c语言更容易理解和书写。
3、D - A+B for Input-Output Practice (IV) > 多组数据求和
Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Sample
input | output |
---|---|
4 1 2 3 4 | 10 |
5 1 2 3 4 5 | 15 |
0 |
分析
持续读入数据,到0为止,输出每组数据的和。
代码
#include<iostream>
using namespace std;
int main() {
int n;
while (~scanf("%d", &n)) {
if (n == 0)break; //到0为止
int a, ad = 0;
while (n--) { //一直减到n=1停止
cin >> a;
ad += a;
}
cout << ad << endl;
}
return 0;
}
a=a+1 可简写 a+=1
4、aabb -> 7744问题
输出所有形如aabb的四位完全平方数(即前两位数字相等,后两位数字也相等,且为某数平方)。
分析
分支和循环结合在一起时可以枚举所有可能的aabb,然后判断它们是否是完全平方数。
代码
//输出形如aabb的完全平方数
#include<bits/stdc++.h>
int main()
{
//枚举a,b
for(int a=1;a<=9;a++){
for(int b=0;b<=9;b++){
int x=a*1100+b*11; *//构造aabb*
if check((x)){ *//利用函数解决*
printf("%d\n",x);
}
}
}
}
*//判断x是否为完全平方数*
bool check(int x)
{
double y = sqrt(x);
if(y-int(y)<1e-6){
return true;
}
return false;
}
如何判断函数为完全平方数
bool check(int x)
{
double y = sqrt(x);
if(y-int(y)<1e-6) { //两者差值无限小
return true;
}
return false;
}
理解
构造aabb不一定需要用枚举从1~9999之间所有的数字,可以用循环a x 1000+a x 100+b x 10+b x 1,
5、K - 人见人爱A+B
这个题目的A和B不是简单的整数,而是两个时间,A和B 都是由3个整数组成,分别表示时分秒,比如,假设A为34 45 56,就表示A所表示的时间是34小时 45分钟 56秒。
Input
输入数据有多行组成,首先是一个整数N,表示测试实例的个数,然后是N行数据,每行有6个整数AH,AM,AS,BH,BM,BS,分别表示时间A和B所对应的时分秒。题目保证所有的数据合法。
Output
对于每个测试实例,输出A+B,每个输出结果也是由时分秒3部分组成,同时也要满足时间的规则(即:分和秒的取值范围在0~59),每个输出占一行,并且所有的部分都可以用32位整数表示。
sample
input | output |
---|---|
2 | |
1 2 3 4 5 6 | 5 7 9 |
34 45 56 12 23 34 | 47 9 30 |
分析
多组时间单位输入并相加,注意进位方法
代码
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
int a, b, c;
int d, e, f;
cin >> a >> b >> c >> d >> e >> f;
//时间的进位,相加
c += f; //秒c+秒f
b += e + c / 60;//分b+分e+秒的进位
a += d + b / 60;//时a+时d+分的进位
c %= 60;//秒取模
b %= 60;//分取模
cout << a << " " << b << " " << c << endl;
}
return 0;
}
时间的进位关系
cin >> a >> b >> c >> d >> e >> f;
c += f; //秒c+秒f
b += e + c / 6 ; //分b+分e+秒的进位
a += d + b / 60; //时a+时d+分的进位
c %= 60;//秒取模
b %= 60;//分取模
6、M - 人见人爱A^B
求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”
Input
输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。
Output
对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。
Sample
input | output |
---|---|
2 3 | 8 |
12 6 | 984 |
6789 10000 | 1 |
0 |
分析
a的b次方也就是b个a相乘,可利用循环进行
代码
#include<iostream>
using namespace std;
int main() {
int a, b;
while (1) {//和 while(n--)效果一样
cin >> a >> b;
if (!a && !b)break;//表示如果b为真就执行下一条语句,b为假就不执行,直接略过。在C语言中非0(如:1,2,-1都是真)就是真,0就是假。
int d = 1;
//b个a相乘
for (int j = 0; j < b; j++) //b个a乘b-1次
d = (d * a) % 1000;//乘法操作,后三位之间的操作对后三位的结果有影响,其他的直接忽略,所有用%取后三位就好
cout << d << endl;
}
return 0;
}
取后三位数字
a=a%1000;
次方循环
for (int j = 0; j < b; j++) //b个a乘b-1次
7、3n+1问题。
猜想:
对于任意大于1的自然数,若n为奇数,则将n变为3n+1,否则变为n的一半。经过若干次这样的变换,一定会使n变为1。例如3→10→5→16→8→4→2→1。
输入n,输出变换的次数。n≤109。
样例输入:3
样例输出:7
分析
从3n+1问题可以看出,n也不是“递增”式的循环,且循环次数也不确定,这种情况非常适合用while循环来实现。
代码
#include<bits/stdc++.h>
typedef long long ll;
int mian()
{
ll n,a[1005];
while(~scanf("%lld",&n)){
a[0]=n;//固定n
int m=1;
while(n!=1){
if(n %2 ==1){//如果为奇数
n=3*n+1;
}
else{
n/=2;
}
a[m++]=n;//存入每次变化的数据
}
printf("%d\n%lld",m-1,a[0]);
for(int i=1;i<m;i++){
printf("->%lld",a[i]);//输出 例如3→10→5→16→8→4→2→1。
}
printf("\n");
}
}