显示标签为“simple-c”的博文。显示所有博文
显示标签为“simple-c”的博文。显示所有博文

期中二

     n个开关排成一排,从1n按顺序依次编号。有n个人也从1n依次编号。第1个人(1号)将开关全部打开。第2个人(2号)将凡是22的倍数的开关关闭。第3个人(3号)将凡是33的倍数的开关做相反处理(该开关如为打开的,将它关闭;如为关闭的,将它打开)。以后的人都和3号一样,将凡是与自己编号相同的开关和是自己编号倍数的开关做相反处理。编程求解:当第n个人操作之后,哪几个开关是打开的?(注意n为正整数,可以从键盘输入)

 

#include <IOSTREAM.H>

int main(int argc, char* argv[])
{
    int a[100];
    int i , j , n;
    cout << "输入整数n:\n" ;
    cin >> n;
    for (i=0 ; i <= n ;i++)
    {
        a[i] = 1;
    }
    for (i=2 ; i <= n; i++)
    {
        for (j=1 ; i*j <= n; j++)
        {
            if (a[i*j]==0)
            {
                a[i*j]=1;
            }
            else
            {
                a[i*j]=0;
            }
        }
    }
    cout << "打开的开关是:\n";
    for (i=1 ; i<=n ; i++)
    {
        if (a[i]==1)  cout <<"第"<<i<<"个\n" ;
    }
    return 0;
}

删除多余字符

1、编写程序:从键盘输入一个字符串s,将s字符串中所有和前面重复多余的字符删除,其余字符保留。输出处理后的字符串。例如:

输入:abadcbad

输出:abdc

 

#include <IOSTREAM.H>

int main(int argc, char* argv[])
{
    char a[50];
    int i, j ;
    cout << "输入一个字符串:\n";
    cin >> a;
    for (i=0 ; a[i]!= ''; i++)
    {
        for (j=0; j<i ; j++)
        {
            if (a[i]==a[j])
            {
                a[i]='0';
            }
        }
    }
    for (i=0 ;a[i]!= '' ;i++)
    {
        if (a[i]=='0')  continue;
        cout << a[i];
    }
    cout<< '\n';
    return 0;
}

编程

请编程将:从午夜000秒起到中午12点(记为从00:00:0012:00:00)时钟的时针、分针、秒针3针重合于同一位置的时刻计算出来。为实际编程方便,当3针中两两之间的夹角小于0.1°时即认为重合了。

// 作业十时钟.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
#include<math.h>
void main()
{   
    int x=3;
    printf("秒针,分针,时针两两之间夹角不多于%d度\n",x);

    for(int i=0;i<=12*60*60;i++)
    {
        int s=i%60;
        int m=(i%3600)%60;
        int h=i/3600;

        int a=i%60*6;                  //a为秒针走过的角度
        double b=i%3600*0.1;              //b为分针
        int c=i%(12*60*60)/120;                   //c为时针
        if((fabs(a-b)<=x)&&(fabs(a-c)<=x)&&(fabs(b-c)<=x))
            printf("%d:%d:%d\n",h,m,s);
    }
}

五户共井问题

5户共井问题。有A、B、C、D、E5家人共用一口井,已知井深不超过10m。A、B、C、D、E的绳长各不相同,从井口放下绳索正好到达水面时:

a) 需要A家的绳2条接上B家的绳1条;
b) 需要B家的绳3条接上C家的绳1条;
c) 需要C家的绳4条接上D家的绳1条;
d) 需要D家的绳5条接上E家的绳1条;
e) 需要E家的绳6条接上A家的绳1条;
问井深和各家绳长。



#include
#include


void main ()
{
float depth;
float a,b,c,d,e;
bool flag = 1;
int num=10;
do
{
for (depth = num ;depth > 0;depth--)
{
for (a = 0; a < num ;a++)
{
for (b = 0; b < num;b++)
{
if(2*a+b!=depth) continue;
for (c = 0 ;c <num ;c++)
{
if(3*b+c != depth) continue;
for (d = 0; d<num ;d++)
{
if(4*c + d !=depth) continue;
for (e = 0; e< num ;e++)
{
if (5*d +e !=depth) continue;
if (6*e+a==depth)
{
cout <<"井深和A,B,C,D,E绳长分别是:\n";
cout << depth/num*10<<endl <<a/num*10<<endl <<b/num*10<<endl<<c/num*10<<endl<<d/num*10<<endl<<e/num*10<<endl;
flag = 0;
break;
}

}
}
}
}
}
}
if (flag)
{
num = num*10;
}
}
while(flag);

}

见下

2. n个开关排成一排,从1到n按顺序依次编号。有n个人也从1到n依次编号。第1个人(1号)将开关全部打开。第2个人(2号)将凡是2和2的倍数的开关关闭。第3个人(3号)将凡是3和3的倍数的开关做相反处理(该开关如为打开的,将它关闭;如为关闭的,将它打开)。以后的人都和3号一样,将凡是与自己编号相同的开关和是自己编号倍数的开关做相反处理。编程求解:当第n个人操作之后,哪几个开关是打开的?(注意n为正整数,可以从键盘输入)


#include
int main(int argc, char* argv[])
{
int a[100];
int i , j , n;
cin >> n;
for (i=0 ; i <= n ;i++)
{
a[i] = 1;
}
for (i=2 ; i <= n; i++)
{
for (j=1 ; i*j <= n; j++)
{
if (a[i*j]==0)
{
a[i*j]=1;
}
else
{
a[i*j]=0;
}
}
}
for (i=1 ; i<=n ; i++)
{
if (a[i]==1) cout <<"第"<<i<<"个\n" ;
}
return 0;
}

删除多余字符串

编写程序:从键盘输入一个字符串s,将s字符串中所有和前面重复多余的字符删除,其余字符保留。输出处理后的字符串。例如:
输入:abadcbad

#include

int main(int argc, char* argv[])
{
char a[50];
int i, j ;
cin >> a;
for (i=0 ; a[i]!= ''; i++)
{
for (j=0; j<i ; j++)
{
if (a[i]==a[j])
{
a[i]='0';
}
}
}
for (i=0 ;a[i]!= '' ;i++)
{
if (a[i]=='0') continue;
cout << a[i];
}
cout<< '\n';
return 0;
}

输出:abdc

Quote Of The Day