Fork me on GitHub

2020深信服软件测试岗编程题

2020深信服软件测试岗编程题

题目描述:

输入一个8位数字和指定剔除的位数,将剩余数字从大到小打印。若不符合要求则打印error如:输入95137462 2,则输出9764321
如:输入95137462 9, 则输出error
如:输入95137462123 1 则输出error

 

输入描述:

输入:八位有效数字,剔除指定第几位(空格间隔)

 

输出描述:

输出:剩余7个数字按从大到小排序输出

 

示例1

输入:95137462 2

输出:9764321

 

代码:

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

void swap(char *a, char *b)
{
    char temp;
    temp = *a;
    *a = *b;
    *b = temp;
 } 

int main()
{
    char str[9];
    int n;
    
    scanf("%s %d", str, &n);
    
// 判断输入是否正确
if(n < 1 || n > 8) { printf("error"); } if(strlen(str) != 8) { printf("error"); }
// 剔除掉指定的一位数字
char strtemp[8]; int i, j; for(i=0,j=0;i<8;i++) { if(i == n-1) { continue; } else { strtemp[j] = str[i]; j++; } } // 排序 for(i=0;i<7;i++) { for(j=0;j<7-i-1;j++) { if(strtemp[j] < strtemp[j+1]) { swap(&strtemp[j], &strtemp[j+1]); } } } // 输出 for(i=0;i<7;i++) { printf("%c", strtemp[i]); } return 0; }

 

posted @ 2020-10-16 20:03  小黑子杜  阅读(339)  评论(0编辑  收藏  举报