2007.
注意循环次数和m,n的大小,否则n-m可能是负数。每一次输出后结果要reset
#include
}
2008.
#include
using namespace std;
int main()
{
int n;
double x;
int count0=0, count1=0, count2=0;
while (cin >> n)
{
if (n == 0)
return -1;
while (n--)
{
cin >> x;
if (x < 0)
count1++;
else if (x == 0)
count0++;
else count2++;
}
cout << count1 << " " << count0 << " " << count2 << endl;
count1 = 0;
count2 = 0;
count0 = 0;
}
}
2009.
#include
#include
using namespace std;
int main()
{
int n,m;
double a[1000];
double sum=0.0;
while (cin >> n>>m)
{
for (int i = 1; i < m; i++)
{
a[0] = n;
a[i] = sqrt(a[i - 1]);
}
for (int j = 0; j < m; j++)
{
sum += a[j];
}
cout.precision(2);
cout << fixed << sum << endl;
sum = 0;
}
}
2010.
这道题一定要注意输出的格式,第一次的代码就是输出最后一个水仙花数后又打了一个空格符,导致presentation error,无奈加了一个数组输出,唉真是严格。
#include
#include
using namespace std;
int main()
{
int n,m;
while (cin >> m>>n)
{
int b, s, g;
int temp,j=0;
temp = m;
int a[1000] = { 0 };
for (int i = m; i <= n; i++)
{
b = temp/ 100;
g = temp % 10;
s = temp / 10 % 10;
if (temp == (b*b*b + g*g*g + s*s*s))
{
a[j] = temp;
j++;
}
temp++;
}
if (j == 0)
cout << "no" <
for (int z = 0; z < j - 1; z++)
{
cout << a[z] << " ";
}
cout << a[j - 1] << endl;
}
}
}
2012.
注意输出的大小写,第一遍老是wrong answer,排错排了一万年,发现写成sorry了,真是窒息,我好菜啊(灬ꈍ ꈍ灬)
#include
#include
using namespace std;
int main()
{
int x,y;
int r;
while (cin >> x >> y)
{
int confirm=0;
if (x == 0 && y == 0)
break;
for (int i = x; i <= y; i++)
{
r = i*i + i + 41;
for (int j = 2; j < r; j++)
{
if (r%j == 0) confirm=1;
}
}
if (confirm != 0)
{
cout << "Sorry"<
else
{
cout << "OK" << endl;
}
}
}
2013.
递归
#include
#include
using namespace std;
int peach(int day)
{
if (day == 1)
return 1;
return (peach(day-1)+1)*2;
}
int main()
{
int a;
while (cin>>a)
{
cout << peach(a)<< endl;
}
}
逆推:
#include#include using namespace std; int main() { int a; while (cin>>a) { int b = 1; for (int i = 1; i < a; i++){ b = (b+1)*2; } cout << b << endl; } }
2014.
#includeusing namespace std; int main() { int n; int a[1000]; int max, min; double sum=0.0; while (cin >> n) { int x; int i=0; sum = 0; while (n--) { cin >> x; a[i++] = x; } max = a[0]; min = a[0]; for (int j = 0; j <= i - 1; j++) { if (a[j] > max) { max = a[j]; } if (a[j] < min) { min = a[j]; } } for (int b = 0; b <= i - 1; b++) { sum += a[b]; } cout.precision(2); cout << fixed << (sum-max-min)/(i-2)<<endl; } }
2015.
#includeusing namespace std; int main() { int m, n; int a[100]; int b[100]; int count, i,j=0; int sum; while (cin >> n >> m) { a[0] = 2; for (i = 1; i < n; i++) a[i] = 2 + a[i - 1]; sum = 0; count = 0; j = 0; for (i = 0; i < n; i++) { count++; sum += a[i]; if (count == m) { b[j++] = sum / m; sum = 0; count = 0; } } if (n%m != 0) { b[j++] = sum / (n%m); } for (i = 0; i < j - 1; i++) { cout << b[i] << " "; } cout << b[j - 1] << endl; } }