PTA-练习3

目录

实验4-2-3 换硬币

实验4-2-4 输出三角形字符阵列

实验4-2-5 输出整数各位数字

实验4-2-6 梅森数

实验4-2-7 找完数

实验4-2-9 水仙花数

实验6-2 英文字母替换加密(大小写转换+后移1位)

实验6-5 简单计算器

实验6-10 统计单词的长度


实验4-2-3 换硬币

//第一眼:好难,没思路
//第二眼:先给五分分配最多,然后两分,然后一分
//写个三层循环,然后凑出这么多钱就可以了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){

    int money,count=0;
    scanf("%d",&money);

    for(int a=money/5;a>0;a--){
        for(int b=money/2;b>0;b--){
            for(int c=money;c>0;c--){
                if(a*5+b*2+c==money){
                    printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n",a,b,c,a+b+c);
                    count++;
                }
            }
        }
    }
    printf("count = %d",count);
    return 0;
}


实验4-2-4 输出三角形字符阵列

//方法二:一共有n行,每行n个字母,写个双重循环
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
    int n;
    scanf("%d",&n);
    int count=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n+1-i;j++){
            printf("%c ",'A'+count);
            count++;
        }
        printf("\n");
    }
    
    return 0;
}

//有时候可以用简便方法
/*
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
    int n;
    scanf("%d",&n);
    switch(n){
        case 1:
            printf("A \n");
            break;
        case 2:
            printf("A B \n");
            printf("C ");
            break;
        case 3:
            printf("A B C \n");
            printf("D E \n");
            printf("F \n");
            break;
            //然后一直写道七
    }
    
    return 0;
}
*/

实验4-2-5 输出整数各位数字

//从高位到低位输出,好麻烦啊
//低位输出可以直接n%10,把结果保存到一个数组里面然后逆序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
    long n;
    scanf("%ld",&n);
    int A[100],i=0;

    if(n==0){
        printf("0 ");
    }
    while(n!=0){
        A[i]=n%10;
        i++;
        n=n/10;
        
    }
    while(i>0){
        printf("%d ",A[i-1]);
        i--;
    }
    return 0;
}

实验4-2-6 梅森数

//1不是素数
//梅森数不就是(2^n)-1
//从高位到低位输出,好麻烦啊
//低位输出可以直接n%10,把结果保存到一个数组里面然后逆序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
//构造一个函数sushu,如果是素数返回1,不是则返回0
//直接看2~~n-1之间有没有可以整除的
int sushu(int n){
    for(int i=2;i<n;i++){
        if(n%i==0){
            return 0;
        }
    }
    return 1;
}
int main(){
    int n;
    scanf("%d",&n);
    if(n==0||n==1){
        printf("None");
    }
    for(int i=2;i<=n;i++){
        if(sushu(pow(2,i)-1)){//是一个素数
            printf("%.lf\n",pow(2,i)-1);//!!!pow输出的是double类型
        }
    }
    return 0;
}


实验4-2-7 找完数

//找完数,    n=所有因子之和
//构造一个函数判断这个数是不是完数
int wanshu(int n){
    int sum=0;
    for(int i=1;i<n;i++){
        if(n%i==0){//i是因子
            sum=sum+i;
        }
    }
    if(sum==n){
        return 1;
    }else{
        return 0;
    }
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
    int m,n;
    scanf("%d %d",&m,&n);
    int flag=0;//用于标记有几个完数
    for(int i=m;i<=n;i++){
        if(wanshu(i)){
            flag++;
            printf("%d = 1",i);
            for(int j=2;j<i;j++){
                if(i%j==0){
                    printf(" + %d",j); 
                }
            }
            printf("\n");
        }//of if
    }
    if(flag==0){
        printf("None");
    }
    
    return 0;
}


实验4-2-9 水仙花数


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
    int n;
    scanf("%d",&n);
    int start=pow(10,n-1);
    int end=pow(10,n);
    for(int i=start;i<end;i++){
        if(water(i,n)){
            printf("%d\n",i);
        }
    }
    return 0;
}
//先构造一个函数判断水仙花数,水仙花数是指一个N位正整数(N≥3),
//它的每个位上的数字的N次幂之和等于它本身。
int water(int n,int wei){
    int flag=n;
    int sum=0;
    while(n>0){
        int i=n%10;
        sum=sum+pow(i,wei);
        n=n/10;
    }
    if(sum==flag){
        return 1;
    }else{
        return 0;
    }
}

实验6-2 英文字母替换加密(大小写转换+后移1位)

这题需要记住ascll码对应的值,A--65,a--97(大写字母+32=小写字母)

//ascll码:A--65,a--97
//

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main(){

    char str[100];
    gets(str);//gets函数输入一个字符串直到\n换行为止

    int n=strlen(str);
    for(int i=0;i<n;i++){
        if(str[i]>='a'&&str[i]<='y'){
            str[i]=str[i]-32+1;
        }else if(str[i]>='A'&&str[i]<='Y'){
            str[i]=str[i]+32+1;
        }else if(str[i]=='Z'){
            str[i]='a';
        }else if(str[i]=='z'){
            str[i]='A';
        }
    }
    puts(str);

    return 0;
}

实验6-5 简单计算器

//输入字符,遇到数字记录,遇到符号则把它和后面一位i相加
//上面的思路错误
//正确思路:先输入一个数字,然后输入一个字符,
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main(){

    int num,sum;
    char c;
    scanf("%d",&num);
    scanf("%c",&c);
    sum=num;
    int flag=0;
    
    while(c!='='){
        switch(c){
            case '+':
                scanf("%d",&num);
                sum=sum+num;
                break;
            case '-':
                scanf("%d",&num);
                sum=sum-num;
                break;
            case '*':
                scanf("%d",&num);
                sum=sum*num;
                break;
            case '/':
                scanf("%d",&num);
                if(num==0){
                    flag=1;
                }else{
                    sum=sum/num;
                }
                break;
            default:
                flag=1;
                break;
        }// of switch
        scanf("%c",&c);
    }
    if(flag){
        printf("ERROR");
    }else
        printf("%d",sum);
    
    
    return 0;
}


实验6-10 统计单词的长度


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main(){

    char c=getchar();
    int i=0;
    int flag=0,kaitou=1;
    
    while(c!='\n'){
        if(c!=' '){
            i++;
            flag=1;
        }
        if(c==' '){
            if(flag==1){
                printf("%d ",i);
                flag=0;
            } 
            i=0;
        }
        c=getchar();
    }
    if(i){
        printf("%d ",i);
    }

    return 0;
}

 

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西柚小萌新

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值