第一阶段 : 1000、1089—1096、1001
1000:A + B Problem
#include
using namespace std;
int main(void)
{
int a,b;
while(scanf("%d%d",&a,&b) != EOF)
cout << a + b << endl;
return 0;
}
1001
#include
using namespace std;
int main(void)
{
int a,b;
while(scanf("%d %d",&a,&b) == 2)
cout << a + b << endl;
return 0;
}
1089:Sum Problem
#include
using namespace std;
int main(void)
{
int n,sum;
while(scanf("%d",&n) == 1)
{
sum = n / 2.0 * (n+1);
cout << sum << endl << endl;
}
return 0;
}
1090:A+B for Input-Output Practice (II)
#include
using namespace std;
int main(void)
{
int n;
cin >> n;
int a,b;
while (n-- && scanf("%d%d",&a,&b) == 2)
cout << a + b << endl;
return 0;
}
1091:A+B for Input-Output Practice (III)
#include
using namespace std;
int main(void)
{
int a,b;
while(cin >> a >> b) {
if (a == 0 && b == 0)
return 0;
cout << a + b << endl;
}
return 0;
}
1092:A+B for Input-Output Practice (IV)
#include
using namespace std;
int main(void) {
int n,sum,t;
while(cin >> n && n != 0) {
sum = 0;
while (n--) {
cin >> t;
sum += t;
}
cout << sum << endl;
}
return 0;
}
1093
#include
using namespace std;
int main(void)
{
int n;
cin >> n;
while(n--)
{
int m,t,sum = 0;
cin >> m;
while (m-- && cin >> t)
sum += t;
cout << sum << endl;
}
return 0;
}
1094
#include
using namespace std;
int main(void)
{
int n,t,sum;
while(cin >> n){
sum = 0;
while(n-- && cin >> t){
sum += t;
//cout << sum << endl;
}
cout << sum << endl;
}
return 0;
}
1095
#include
using namespace std;
int main(void)
{
int a,b;
while(cin >> a >> b)
cout << a + b << endl << endl;
return 0;
}
1096
#include
using namespace std;
int main(void)
{
int n;
cin >> n;
while(n--)
{
int m,t,sum = 0;
cin >> m;
while (m-- && cin >> t)
sum += t;
cout << sum << endl;
if(n != 0)
cout << endl;
}
return 0;
}
第二阶段 :2000—2011、2039
2000:ASCII码排序
#include
#include
using namespace std;
const int n = 3;
int main(void)
{
char c[n];
while(cin >> c[0] >> c[1] >> c[2])
{
sort(c,c+n);
//打印输出
for(int i = 0;i < n;i++){
if(i < n - 1 )
cout << c[i] << " ";
else
cout << c[i] << endl;
}
}
return 0;
}
2001:计算两点间的距离
#include
#include
#include
using namespace std;
int main(void)
{
double x1,y1,x2,y2;
while(cin >> x1 >> y1 >> x2 >> y2)
printf("%.2f\n",sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)));
return 0;
}
2002:计算球体积
#include
#define PI 3.1415927
using namespace std;
int main(void)
{
double r;
while(cin >> r )
printf("%.3f\n",4/3.0*PI*r*r*r);
return 0;
}
2003:计算绝对值
#include
using namespace std;
int main(void)
{
double x;
while(cin >> x)
printf("%.2f\n",fabs(x));
}
2004:成绩分类
#include
using namespace std;
int main(void)
{
int score;
while(cin >> score)
{
if(score >= 90 && score <= 100)
cout << 'A' << endl;
else if(score >= 80 && score < 90)
cout << 'B' << endl;
else if(score >= 70 && score < 80)
cout << 'C' << endl;
else if(score >= 60 && score < 70)
cout << 'D' << endl;
else if(score >= 0 && score < 60)
cout << 'E' << endl;
else if(score < 0 || score > 100)
cout << "Score is error!" << endl;
}
}
2005:计算天数
#include
using namespace std;
int main(void)
{
int daysOfMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int y,m,d;
int days;
while(scanf("%d/%d/%d",&y,&m,&d) == 3)
{
days = 0;
if((y%400==0 || (y%4==0 && y%100!=0)) && m >=3)
daysOfMonth[2]++;
if(m == 1){
cout << d << endl;
continue;
}
for(int i = 1;i < m;i++)
days += daysOfMonth[i];
cout << days + d << endl;
daysOfMonth[2] = 28; //reset
}
}
2006:求奇数的乘积
#include
using namespace std;
int main(void)
{
int m,t,ret;
while(cin >> m)
{
ret = 1;
for(int i = 0;i < m;i++)
{
cin >> t;
if(t % 2 != 0)
ret *= t;
}
cout << ret << endl;
}
}
2007:
#include
using namespace std;
int main(void)
{
long long n,m,x,y;
while(cin >> n >> m)
{
x = y = 0;
// tips:
if(n > m)
{
int t = n;
n = m;
m = t;
}
for(int i = n;i <= m;i++)
{
if(i % 2 == 0)
x += i*i;
else
y += i*i*i;
}
cout << x << " " << y << endl;
}
}
2008
#include
using namespace std;
int main(void)
{
int n;double t;
int a,b,c;
while(cin >> n && n != 0){
a = b = c = 0;
while(n-- && cin >> t){
if(t < 0) a++;
if(t == 0) b++;
if(t > 0) c++;
}
cout << a << " " << b << " " << c << endl;
}
return 0;
}
2009:数值统计
#include
using namespace std;
int main(void)
{
double n,m,ret;
while(cin >> n >> m)
{
ret = n;
for(int i = 0;i < m - 1;++i)
{
n = sqrt(n);
ret += n;
}
printf("%.2f\n",ret);
}
return 0;
}
2010:多项式求和
#include
using namespace std;
int main(void)
{
double sum;
int m,n; cin >> m;
while(m-- && cin >> n)
{
sum = 0;
for(int i = 1;i <= n;i++)
{
if(i % 2 != 0)
sum += 1 * 1.0 / i;
else
sum -= 1 * 1.0 / i;
}
printf("%.2f\n",sum);
}
}
2011:水仙花数
#include
using namespace std;
int main(void)
{
int m,n;
while(cin >> m >> n)
{
int flag = 0;
for(int i = m;i <= n;i++)
{
int x = 0,t = i;
while(t > 0)
{
x += pow(t%10,3);
t /= 10;
}
if(x == i)
{
if(flag == 1)
cout << " ";
cout << i ;
flag = 1;
}
}
if(flag == 1) cout << endl;
if(flag == 0) cout << "no" << endl;
}
}
2039:三角形
#include
using namespace std;
int main(void)
{
int m;cin >> m;
//题目没有说是整数...--!
double a,b,c;
while(m--)
{
cin >> a >> b >> c;
if(a + b > c && a + c > b && b + c > a )
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
1970:
1062:Text Reverse
//使用了stl中的栈
#include
using namespace std;
int main(void)
{
int n;
cin >> n;
getchar();
char s[1050];
while(n--)
{
gets(s);
int len = strlen(s);
stack st;
for(int i = 0; i < len; i++)
{
if(s[i] != ' ')
st.push(s[i]);
if(s[i] == ' ' || i == len - 1 )
{
while(! st.empty())
{
cout << st.top();
st.pop();
}
if(s[i] == ' ')
cout << ' ';
}
}
cout << endl;
}
return 0;
}
1720:
void p1720()
{
int a,b;
while(scanf("%x %x",&a,&b) != EOF)
cout << a + b << endl;
}
2104:
void p2104()
{
int n,m,t;
while(cin >> n >> m)
{
if(n == -1 && m == -1) break;
while(m != n)
{
t = m;
m = n % m;
n = t;
}
if(n == 1)
cout << "YES" << endl;
else cout << "POOR Haha" << endl;
}
}
1064:
void p1064()
{
double income;
double sum = 0;
for(int i = 0;i < 12;i++)
{
cin >> income;
sum += income;
}
double average = sum / 12;
printf("$%.2f",average);
}
2734:
#include
using namespace std;
int main(void) {
char s[256];
int sum;
while (gets(s) && s[0] != '#') {
int len = strlen(s);
sum = 0;
for (int i = 0; i < len; i++) {
if (s[i] == ' ')
continue;
else
sum += (i + 1) * (s[i] - 'A' + 1);
}
cout << sum << endl;
}
}
2012
#include
using namespace std;
bool isPrime(int x)
{
if(x < 2) return false;
if(x % 2 == 0 && x != 2)
return false;
for(int i = 2;i*i < x;i++)
if(x % i == 0) return false;
return true;
}
int main(void)
{
int x,y;
while(cin >> x >> y)
{
int flag = 1;
if(x == 0 && y == 0)
return 0;
else
for(int i = x;i <= y;i++)
if(!isPrime(i*i+i+41))
flag = 0;
if(flag == 1) cout << "OK" << endl;
if(flag == 0) cout << "Sorry" << endl;
}
return 0;
}
2103
#include
using namespace std;
int main(void)
{
int n;
while(cin >> n)
{
int total = 1;
for(int i = n-1;i >= 1;i--)
total = 2*(total+1);
cout << total << endl;
}
return 0;
}
2104
#include
using namespace std;
int main(void)
{
int n;
while(cin >> n)
{
int a[n],sum = 0;double average;
for(int i = 0;i < n;i++)
{
cin >> a[i];
sum += a[i];
}
int *maxN = max_element(a,a+n);
int *minN = min_element(a,a+n);
sum -= (*maxN + *minN);
average = sum * 1.0 / (n-2);
printf("%.2f\n",average);
}
}
2015
#include
#include
using namespace std;
int main(void)
{
int n,m;
while(cin >> n >> m)
{
int sum = 0,average = 0,cnt = 0;
for(int i = 2;i <= 2*n;i+=2){
sum = sum + i;
if(i/2 % m == 0){
average = sum / m;
if(cnt != 0)
cout << " " << average;
else {
cnt++;
cout << average;
}
sum = 0;
}
}
if(n % m == 0) cout << endl;
else cout << " "<< sum / (n % m) << endl;
}
return 0;
}
2016
#include
using namespace std;
int main(void)
{
int n;
int a[100] = {0};
while(cin >> n && n != 0)
{
for(int i = 0; i < n;i++)
cin >> a[i];
int minN = 0;
//不用stl找最小值的下标
for(int i = 1;i < n;i++)
{
if(a[i] < a[minN])
minN = i;
}
int t = a[0];
a[0] = a[minN];
a[minN] = t;
//输出
for(int i = 0;i < n - 1;i++)
cout << a[i] << " ";
cout << a[n-1] << endl;
}
return 0;
}
2017
#include
#include
using namespace std;
int main(void)
{
int n;cin>>n;
getchar(); //读取前一个cin留在缓冲区的回车
string s;
while(n--)
{
string s;
getline(cin,s);
int len = s.length(),cnt = 0;
for(int i = 0;i < len;i++)
if(s[i] >= '0' && s[i] <= '9')
cnt++;
cout << cnt << endl;
}
return 0;
}
2018
#include
using namespace std;
int main(void)
{
int f[50]; int n;
while(cin >> n && n != 0)
{
f[1] = 1;f[2] = 2;f[3] = 3;
for(int i = 4;i <=n;i++)
{
f[i] = f[i-3] + f[i-1];
}
cout << f[n] << endl;
}
return 0;
}
2019
#include
using namespace std;
int main(void)
{
int n,m,x;
while(cin >> n >> m && n != 0 && m != 0)
{
//online
int t,cnt = 0;
for(int i = 0;i < n;i++)
{
cin >> t;
if(t < m )
cout << t << " ";
//one time
else if(t >= m )
{
if(cnt == 0) {
cnt ++;
cout << m << " " << t;
}
else
cout << " " << t;
}
}
//last loc
if(m > t)
cout << m << endl;
else
cout << endl;
}
return 0;
}
2020
#include
using namespace std;
const int MAX_N = 100;
int n, a[MAX_N];
void solve()
{
while(cin >> n && n != 0)
{
for(int i = 0;i < n;i++)
scanf("%d",&a[i]);
//bubble sort
for(int i = n - 1;i >= 0;i--){
for(int j = 0;j < i;j++){
if(fabs(a[j]) < fabs(a[j+1]))
swap(a[j],a[j+1]);
}
}
//输出
for(int i = 0;i < n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
}
2021
#include
#include
using namespace std;
const int money[6] = {100,50,10,5,2,1};
int n,t,x;
void solve()
{
while(cin >> n && n != 0)
{
x = 0;
for(int i = 0;i < n;i++){
cin >> t;
for(int j = 0;j < 6;j++)
{
x += t / money[j];
t %= money[j];
}
}
cout << x << endl;
}
}