循环结构程序设计 基础题

1 求位数

#include 
int main() {
    int m,n;
    n=0;
    FILE *fin,*fout;
    fin=fopen("data.in","rb");
    fout=fopen("data.out","wb");
    fscanf(fin,"%d",&m);
    while(m>0){
        n++;
        m=m/10;
    }
    fprintf(fout,"%d",n);
    fclose(fin);
    fclose(fout);
    return 0;
}

2 水仙花数

重点掌握个十百位的求法
百:234/100=2;
十:234/10=23;23%10=3;
个:234%10=4
#include 
#include
int main() {
    FILE *fout;
//  fin=fopen("data.in","rb");
    fout=fopen("data.out","wb");
    int i,hun,ten,gw;
    double sum;
    for(i=101;i<999;i++){
        hun=i/100;
        ten=i/10%10;
        gw=i%10;
    sum=pow(hun,3)+pow(ten,3)+pow(gw,3);
    if(i==sum)fprintf(fout,"%d\n",i);
    }
//  fclose(fin);
    fclose(fout);
    return 0;
}
输出
153
370
371

3 韩信点兵_hanxin

不用想太多,遍历一遍就是了
注意有参数isanswer用于跳出程序循环
#include
int main() {
//  cout << "!!!Hello World!!!" << endl; 
// prints !!!Hello World!!!
    int i,a,b,c,isanswer;
    FILE *fin,*fout;
    fin=fopen("data.in","rb");
    fout=fopen("data.out","wb");
    fscanf(fin,"%d%d%d",&a,&b,&c);
    for(i=99;i>10;i--){
        if(i%3==a&&i%5==b&&i%7==c){
            isanswer=1;break;
        }
    }
    if(isanswer==1) 
    fprintf(fout,"%d\n",i);
    else fprintf(fout,"no answer");
    fclose(fin);
    fclose(fout);
    return 0;

4 倒三角形_traingle

#include
int main(){
    int i,j,n;
    scanf("%d",&n);
    for(i=0;i
输入输出:

循环结构程序设计 基础题_第1张图片

5 统计_stat

数组输入要加“&”(地址)符号
include
int main(){
    FILE *fin,*fout;
    fin=fopen("data.in","rb");
    fout=fopen("data.out","wb");
    int m,n,i,k=0;
    fscanf(fin,"%d",&n);
    int *a=new int[n];
    for(i=0;i

6 调和级数_harmony

#include 
int main() {
    FILE *fin,*fout;
    fin=fopen("data.in","rb");
    fout=fopen("data.out","wb");
    int n,i;
    double sum=0;
    fscanf(fin,"%d",&n);
    for(i=1;i<=n;i++){
        sum=sum+(double)1/i;
    }
    fprintf(fout,"%0.3lf\n",sum);
    fclose(fin);
    fclose(fout);
    return 0;
}
输入
3
输出
1.833

后面就不引用文件了hh(●´∀`●)

7 近似计算

重点在(double)k/i
#include 
//  pi/4=1-1/3+1/5-1/7+....
int main() {
    int i,k=1;
    double sum=0.0;
    for(i=1;i<1e6;){
        sum=sum+(double)k/i;
        k=-k;
        i=i+2;
    }
    printf("%0.6lf\n",sum);
    return 0;
}

8 子序列的和_sunsequence

1e6表示10^6;1e-6表示10^-6
刚开始把条件写反了,搞了很久很久很久….٩(๑`н´๑)۶
#include 
//  pi/4=1-1/3+1/5-1/7+....
int main() {
    int i,k=1;
    double sum=0.0;
    for(i=1;i<1e6;){
        sum=sum+(double)k/i;
        k=-k;
        i=i+2;
    }
    printf("%0.6lf\n",sum);
    return 0;
}

9 分数化小数_decimal

使用*表示不确定位数
#include 
int main() {
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    printf("%.*f\n",c,(double)a/b);
    return 0;
}

10 排列_permutation

题目说不用想太多,然后刚开始就没有想太多hhhh
不要尽相信题目,简直了hhhh

i 最小123,最大987/3=329。规定范围之后,循环遍历,当符合条件1+2+3+…+9=45;1*2*3….*9=362880,则输出。

#include
void permutation(int a,int &b,int &c){
    int hun,ten,gw;
    hun=a/100;
    ten=a/10%10;
    gw=a%10;
    b+=hun+ten+gw;
    c*=hun*ten*gw;
}
int main(){
    int b,c;
    int i,j,k;
    for(i=123;i<=329;i++){
        j=i*2;
        k=i*3;
        b=0;c=1;
        permutation(i,b,c);
        permutation(j,b,c);
        permutation(k,b,c);
        if(b==45&&c==362880){
        printf("%d %d %d\n",i,j,k);
        }
    }
    return 0;
}

你可能感兴趣的:(垒)