A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Tao_tao 中级黑马   /  2016-8-25 20:35  /  2855 人查看  /  24 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文

1. 找出多个字符串中的最大公共子字符串


2. 计算1~100中所有7的倍数的个数

#import <Foundation/Foundation.h>

int main(int argc,const char *argv[]){
    //定义变量接收7倍数的个数
    int count = 0;
    //循环1-100
    for(int i= 1;i<= 100;i++){
    //如果和7取余为0,这个数就是7的倍数
        if(i%7 == 0)
    //计数+1

            count++;
    }
    //打印个数
    NSLog(@"%d",count);
    return 0;
}

3. 输入6个字符串(仅含字母和数字),字符串排序并输出。

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

int main(int argc,const char *argv[]){
    //定义字符串数组
    char str[6][100];
    //提示用户输入
    printf("请输入6个字符串,由字母和英文组成,默认空格代表一个字符串结束\n");
    //接收
    for(int i = 0;i < 6; i++){
    scanf("%s",str[i]);
    }
    //循环对数组各行元素进行比较大小,并将较小者向后排序
    for(int i= 0; i< 6-1;i++){
        for(int j = i+1;j<6;j++){
        if(strcmp(str[j], str[i]) > 0){
            char temp[100];
            strcpy(temp, str[i]);
            strcpy(str[i], str[j]);
            strcpy(str[j], temp);
        }
        }
    }
    for (int i= 0; i< 6; i++) {
        printf("%s ",str[i]);
    }
    return 0;
}

4. 输入两数,打印其和、差、积、商、余数。

#include <stdio.h>

int main(int argc,const char *argv[]){
    int num1,num2;
    printf("请输入两个数用逗号间隔\n");
    scanf("%d,%d",&num1,&num2);
    int sum = num1 + num2;
    int jian = num1 - num2;
    int cheng = num1 * num2;
    float chu = num1 / (float)num2;
    int yu = num1 % num2;
    printf("和=%d,差 = %d,积 = %d,商 = %.2f,余数 = %d\n",sum,jian,cheng,chu,yu);
    return 0;
}

5. 输入字符串,统计A、B、C、D出现次数,由高到低输出字母和次数。

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

int main(int argc,const char* argv[]){
   //定义
    //字符数组
    char ch[100];
    int numa ,numb,numc,numd;
    numa = numb = numc =numd = 0;
    int counta,countb,countc,countd;
    counta = countb = countc = countd = -1;
    //提示输入
    printf("请输入字符串\n");
    //接收
    gets(ch);
    //遍历字符数组
    unsigned long int len = strlen(ch);
    for(int i = 0; i < len ;i++){
    //如果数组元素等于a,b,c,d中的一个则让其计数变量+1,并记录元素下标
        switch(ch[i]){
            case 'a':
                numa++;
                counta = i;
                break;
            case 'b':
                numb++;
                countb = i;
                break;
            case 'c':
                numc++;
                countc = i;
                break;
            case 'd':
                numd++;
                countd = i;
                break;
            default:
                break;

        }

    }
    //建立数组,将计数变量分别存入数组中
    int arr[4][2] = {};
    arr[0][0] = numa;
    arr[1][0] = numb;
    arr[2][0] = numc;
    arr[3][0] = numd;
    arr[0][1] = counta;
    arr[1][1] = countb;
    arr[2][1] = countc;
    arr[3][1] = countd;
    //按由高到低顺序排列
    for(int i = 0;i < 3;i++){
        for(int j = i+1;j< 4;j++){
            if(arr[i][0]<arr[j][0]){
                int temp = arr[i][0];
                arr[i][0] = arr[j][0];
                arr[j][0] = temp;
                int temp1 = arr[i][1];
                arr[i][1] = arr[j][1];
                arr[j][1] = temp1;
            }
        }
    }
    //遍历数组并输出其代表的字母
    for(int i = 0;i < 4;i++){
        printf("%c = %d\t",ch[arr[i][1]],arr[i][0]);
    }

    return 0;

}
//#include <stdio.h>
//#include <string.h>
//
//int main(int argc, const char * argv[]) {
//    //定义数组存储字符串
//    char a[100]={};
//    //提示输入字符串
//    printf("请输入一个字符串:\n");
//    //接受
//    //scanf("%s",&a);
//    gets(a);
//    //定义一个统计结构体数组
//   
//    struct tongji{
//        char zimu;
//        int cishu;
//    } ji[4]={{'A' ,0},{'B' ,0},{'C',0},{'D',0},};
//   
//   
//    int len = sizeof(a)/sizeof(char);
//    for (int i = 0 ; i<len; i++) {
//        switch (a[i]) {
//            case 'a':
//            case 'A':
//                ji[0].cishu++;
//                break;
//            case 'b':
//            case 'B':
//                ji[1].cishu++;
//                break;
//            case 'C':
//            case 'c':
//                ji[2].cishu++;
//                break;
//            case 'd':
//            case 'D':
//                ji[3].cishu++;
//                break;
//               
//            default:
//                break;
//        }
//    }
//   
//   
//    printf("a是%d次 b是%d次 c是%d次 d是%d次\n",ji[0].cishu,ji[1].cishu
//           ,ji[2].cishu,ji[3].cishu);
//   
//    //反向冒泡排序
//    for (int i=0; i<4; i++) {
//        for (int j = 0; j<4-1-i; j++) {
//            if (ji[j].cishu< ji[j+1].cishu) {
//               
//                struct tongji temp = ji[j];
//                ji[j] = ji[j+1];
//                ji[j+1] = temp;
//               
//            }
//        }
//    }
//   
//   
//    for (int i = 0 ; i<4; i++) {
//        printf("%c  %d      ",ji[i].zimu,ji[i].cishu);
//        
//        
//        
//    }
//   
//    return 0;
//}
//

6. 输入英文语句,单词首字符转换大写后输出。

#include <stdio.h>
int main(int argc,const char *argv[]){
    //定义字符数组
    char ch[100];
    //提示输入
    printf("请输入一句英文,默认输入空格代表一个单词结束\n");
    //接收
    gets(ch);
//    scanf("%s",ch);
    //定义一个变量用于控制转换大写
    int word = 0;
    //循环控制
    for(int i = 0;ch[i]!='\0';i++){

        //当字符等于空格时,下一个字母-32转换成大写
        if(ch[i] != ' '){
            if(word == 0){
                ch[i]=ch[i]-32;
                word = 1;
            }
        }else{
            word = 0;
        }
        printf("%c",ch[i]);
    }
//    puts(ch);

    return 0;
}

7. 中华人民共和国公民身份证号码组成规则是前4位是代表省份和地区(例如4201代表湖北省武汉市),最后一位代表性别(1或3代表男性)。编写一个程序,通过身份证号码判断某人是否是武汉人以及其性别。(Objective-C)

#import <Foundation/Foundation.h>

int main(int argc,const char *argv[]){

    @autoreleasepool{
       //定义NSString 对象接收身份证号
        NSString *identity = @"210604199901021333";
        //判断身份证号前4位和后1位
        if([identity hasPrefix:@"2105"]){
            NSLog(@"此人是辽宁本溪人");
        }else{
            NSLog(@"此人不是辽宁本溪人");
        }
        if([identity hasSuffix:@"1"]||[identity hasSuffix:@"3"]){
            NSLog(@"此人是男性");
        }else{
            NSLog(@"此人不是男性");
        }
    }
    return 0;
}

8. 编程求字符串“100”和“150”按十进制数值做差后的结果以字符串形式输出。(Objective-C)

#import <Foundation/Foundation.h>

int main(int argc,const char *argv[]){
    @autoreleasepool{
        NSString *str1 = @"100";
        NSString *str2 = @"150";
        int num1 = [str1 intValue];
        int num2 = [str2 intValue];
        int result = num1 - num2;
        NSLog(@"result = %d",result);
    }
    return 0;
}

9. 利用分类给NSString扩展3个方法(Objective-C)
1> 字符串反转(比如@”123”调用方法后返回@”321”)
2> 计算英文字母的个数(比如@”5435abc54abc3AHJ5”调用方法后返回的是9)
3> 去除字符串两端空格(比如@”  1235 45 ”调用方法后返回@”1235 45”)
10. 有一个Baby类,有Cry行为(方法,意为“哭”),Baby可以配一个保姆,但是作为保姆,必须遵守保姆协议:能够处理Baby类Cry的行为。请根据以上描述设计一套代理设计模式(Objective-C)

#import <Foundation/Foundation.h>

//写协议
@protocol lookBabyProtocol<NSObject>
-(void)feedBaby;//喂孩子
-(void)hongBaby;//哄孩子
@end
//创建保姆类
@interface baoMu:NSObject<lookBabyProtocol>

@end
@implementation baoMu

-(void)feedBaby{
    NSLog(@"保姆挤奶喂孩子");
}
-(void)hongBaby{
    NSLog(@"保姆哄孩子睡觉");
}

@end

//创建baby类
@interface Baby:NSObject
@property (nonatomic,strong)id<lookBabyProtocol> baoMu;//baby有一个保姆
-(void)eat;//饿了
-(void)sleep;//困了
@end
//实现baby类
@implementation Baby
-(void)eat{
    NSLog(@"哇哇哇!!!");
    [self.baoMu feedBaby];
}
-(void)sleep{
    NSLog(@"哇哇哇!!!");
    [self.baoMu hongBaby];
}
@end

int main(int argc,const char *argv[]){
    @autoreleasepool{
        Baby *by = [Baby new];
        baoMu *bm = [baoMu new];

    }
    //遍历数组输出100次的结果
    for(int i= 0;i< 6;i++){
        printf("%d出现%d次 \t",i+1,count[i]);
    }
    return 0;
}

24 个回复

正序浏览
学习学习!
回复 使用道具 举报

口味有点重了!!
回复 使用道具 举报
谢谢楼主,舍身取义啊
回复 使用道具 举报
很有价值
回复 使用道具 举报
太牛了 ,都记了
回复 使用道具 举报
厉害,黑马币真多
回复 使用道具 举报
学习学习!
回复 使用道具 举报
细听风语为梧桐 发表于 2016-8-29 23:06
采集的真不错,赞一个,我收藏一下

谢谢支持
回复 使用道具 举报
顶顶顶顶顶顶顶顶顶顶顶
回复 使用道具 举报
采集的真不错,赞一个,我收藏一下
回复 使用道具 举报

难道你有两只屌?
回复 使用道具 举报
yuanxianzhi 发表于 2016-8-29 20:40
黑马币 -18   黑马币还可以透支啊

就是这么屌
回复 使用道具 举报
梦想的小草 发表于 2016-8-29 21:06
为什么你黑马币是负数?

因为屌。。。
回复 使用道具 举报
为什么你黑马币是负数?
回复 使用道具 举报
为什么你黑马币是负数?
回复 使用道具 举报
黑马币 -18   黑马币还可以透支啊{:2_30:}
回复 使用道具 举报
这不是C么
回复 使用道具 举报
楼主,干的不错
回复 使用道具 举报
Tao_tao 发表于 2016-8-28 18:17
兄弟,认识JAVA么?

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

scanf("%s",str);

就这几句我就看出来是 C语言了 java导包是这样导的吗?
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马