2034:
#include<iostream>
using namespace std;
int main()
{
int a[105];
int n=0,b;
while(cin>>b)
{
a[n]=b;
n++;
}
for(int i=n-1;i>=0;i--)
{
cout<<a[i]<<" ";
}
return 0;
}
2035:【例5.2】平移数据
#include<iostream>
using namespace std;
int main(){
int n,a[1005];
cin>>n;
for (int i = 1; i <= n; ++i) {
cin>>a[i];
}
for (int i = 1; i <= n; ++i) {
if(i==n) cout<<a[1]<<endl;
else cout<<a[i+1]<<" ";
}
return 0;
}
2036:【例5.3】开关门
#include<iostream>
using namespace std;
int main(){
int n;
bool a[1005]={0};
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j%i==0) a[j]=!a[j];
else continue;
}
}
for(int i=1;i<=n;i++){
if(a[i]) cout<<i<<" ";
}
return 0;
}
2037:【例5.4】约瑟夫问题
#include<iostream>
using namespace std;
int main()
{
int a[10000]={0},n,m,b=1,c;
cin>>n>>m;
c=n;
for(int i=1;i<=n;i++)
{
if(a[i]!=1)
{
if(b==m)
{
cout<<i<<" ";
a[i]=1;
b=0;
c--;
}
b++;
}
if(i==n)
i=0;
if(c==0)
break;
}
return 0;
}
2038:【例5.5】最大数位置
#include<iostream>
using namespace std;
int main()
{
int a[10000]={0},n,m,b=1,c;
cin>>n>>m;
c=n;
for(int i=1;i<=n;i++)
{
if(a[i]!=1)
{
if(b==m)
{
cout<<i<<" ";
a[i]=1;
b=0;
c--;
}
b++;
}
if(i==n)
i=0;
if(c==0)
break;
}
return 0;
}
2039:【例5.6】冒泡排序
#include<iostream>
using namespace std;
int main()
{
int n,a[25];
cin>>n;
for(int i=1;i<=n;++i)
cin >> a[i];
for(int i=1; i<=n-1;++i)
for(int j=1;j<=n-i;++j)
{
if(a[j]< a[j+1])
swap(a[j], a[j+1]);
}
for(int i = 1; i <= n; ++i)
cout << a[i] << endl;
return 0;
}
2040:【例5.7】筛选法找质数
#include <iostream>
using namespace std;
bool isprime(int n){
if(n==1) return 0;
for(int i=2;i*i<=n;i++){
if(n%i==0) return 0;
}
return 1;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
if(isprime(i)) cout<<i<<endl;
}
return 0;
}
1102:与指定数字相同的数的个数
#include<iostream>
using namespace std;
int main(){
int n,a[106],m;
cin>>n;
for (int i = 1; i <=n ; ++i) {
cin>>a[i];
}
cin>>m;
int p=0;
for (int i = 1; i <=n ; ++i) {
if(a[i]==m) p++;
}
cout<<p<<endl;
return 0;
}
1103:陶陶摘苹果
#include <iostream>
using namespace std;
int main(){
int m,p=0,a[15];
for (int i = 1; i <= 10; ++i) {
cin>>a[i];
}
cin>>m;
for(int i=1;i<=10;i++){
if(a[i]<=m+30) p++;
}
cout<<p<<endl;
return 0;
}
1104:计算书费
#include <iostream>
using namespace std;
int main(){
double a[11]={28.9,32.7,45.6,78,35,86.2,27.8,43,56,65},b[15],p=0;
for (int i = 0; i < 10; ++i) {
cin>>b[i];
p+=a[i]*b[i];
}
cout<<p<<endl;
return 0;
}
1105:数组逆序重存放
#include <iostream>
using namespace std;
int main(){
int n,a[105];
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=n;i>=1;i--){
cout<<a[i]<<" ";
}
return 0;
}
1106:年龄与疾病
#include <iostream>
#include <iomanip>
using namespace std;
double b[5];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int a;
cin>>a;
if(a>0&&a<=18) b[0]++;
else if(a>18&&a<=35) b[1]++;
else if(a>35&&a<=60) b[2]++;
else{
b[3]++;
}
}
for (int i = 0; i < 4; ++i) {
cout<<fixed<<setprecision(2)<<b[i]/n*100.0;
cout<<"%"<<endl;
}
return 0;
}
1107:校门外的树
#include <iostream>
using namespace std;
int main(){
int L,M,k1,k2,a[10001]={},s=0;
cin>>L>>M;
for(int i=1;i<=M;i++){
cin>>k1>>k2;
for(int j=k1;j<=k2;j++){
a[j]=1;
}
}
for(int i=0;i<=L;i++){
if(a[i]==0){
s++;
}
}
cout<<s;
return 0;
}
1108:向量点积计算
#include <iostream>
using namespace std;
int main(){
int n,a[1005],b[1005];
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
int p=0;
for(int i=1;i<=n;i++){
p+=a[i]*b[i];
}
cout<<p<<endl;
return 0;
}
1109:开关灯
#include <iostream>
using namespace std;
int main(){
int n,m,q=0,o=0;
bool a[5005]={false};
cin>>n>>m;
if(m==1){
for(int i=1;i<=n;i++){
cout<<i<<",";
}
}else{
for(int i=2;i<=m;i++){
for(int j=1;j<=n;j++){
if(j%i==0){
a[j]=!a[j];
}
}
}
for(int i=1;i<=n;i++){
if(!a[i]) q++;
}
for(int i=1;i<=n;i++){
if(!a[i]&&o<q-1){
cout<<i<<",";
o++;
}
else if(!a[i]&&o==q-1){
cout<<i<<endl;
o++;
}
}
}
return 0;
}