首页 > 编程语言 > 详细

C语言 百炼成钢6

时间:2016-01-22 02:33:27      阅读:215      评论:0      收藏:0      [点我收藏+]
//题目17:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

//分析:将字符串存入字符数组,用for分别检索英文字母、空格、数字和其它字符
//char型可以转成Int类型,通过ASCII表就可以得出数字的范围时48~57;字母的范围是65~90;97~122;空格是32

void main(){
    char str[30] = "adfa-123 12 asdf‘sad13";
    int num = 0;
    int ch = 0;
    int nul = 0;
    int other = 0;
    int temp = 0;
    for (int i = 0; i < 30; i++)
    {
        if (str[i]==\0)
        {
            break;
        }
        else{
            temp = (int)str[i];
            if (temp>47 && temp<58)
            {
                num++;
            }
            else if ((temp>64 && temp<91) || (temp>96 && temp < 123)){
                ch++;
            }
            else if (temp==32)
            {
                nul++;
            }
            else{
                other++;
            }
        }
    }
    printf("\n数字的个数%d,字母的个数%d,空格的个数%d,其他字符的个数%d。",num,ch,nul,other);
    system("pause");
}

 

技术分享

 

//题目18:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时
//共有5个数相加),几个数相加有键盘控制。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

//分析:键盘输入数字,决定相加的个数
//

int getnum(int num){
    int a = 2;
    int res = 0;
    for (int i = num; i >-1; i--)
    {
        res += a*(int)(pow(10, i));
    }
    return res;
}


void main(){
    int num = 0;
    scanf("%d",&num);
    int s = 0;
    //方法1
    /*for (int i = 0; i <num; i++)
    {
        s += getnum(i);
    }*/
    //方法2
    int count = 0;
    int a = 2;
    int tn = 0;
    while (count < num){
        //每次实现加的那个数的值,a永远是200..0,tn是2222,两者相加 就变成正确的数
        tn = tn + a;
        s += tn;
        a = a * 10;
        count++;
    }
    printf("\n%d",s);
    system("pause");
}

 

技术分享

 

C语言 百炼成钢6

原文:http://www.cnblogs.com/zhanggaofeng/p/5149917.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!