第六章-2-数组练习

今天的题目难度不是那么easy了,稍微难一点点,但是不难,结果我全不会了,还得查资料看书才能做出来,中午还要出门一趟,就先做这些吧。

/*
 * @Issue: 打印n阶螺旋方方阵,(n<10)
 * @Author: 一届书生
 * @LastEditTime : 2020-01-19 09:46:07
 */
#include<iostream>
using namespace std;

int main(){
     int n;
    // a, b, c, d分别为上、下、左、右边界,a、b为行号,c、d为列号 
   while( cin>>n){
    int a, b, c, d, i, value = 1, array[20][20];
    for (a = 0, b = n - 1, c = 0, d = n - 1; a <= b; a ++, b --, c ++, d --) {
        for (i = c; i <= d; i ++)       array[a][i] = value ++;
        for (i = a + 1; i < b; i ++)    array[i][d] = value ++;
        for (i = d; i > c; i --)        array[b][i] = value ++;
        for (i = b; i > a; i --)        array[i][c] = value ++;
    }
    for (i = 0; i < n; i ++) {
        for (int j = 0; j < n; j ++)
        printf("%5d",array[i][j]);
        cout<<endl;
    }

   }
    return 0;
}

  

/*
 * @Issue: 数组a包括十个整数,把a中所有的后项除以前项之商取整后放入数组b,按每行3个元素输出数组b
 * @Author: 一届书生
 * @LastEditTime : 2020-01-19 09:51:54
 */
#include<iostream>
using namespace std;

int main(){
    int a[20],b[20];
    for(int i=0;i<10;i++)cin>>a[i];
    for(int i=1;i<10;i++)b[i-1]=a[i]/a[i-1];
    for(int i=0,j=0;i<9;i++,j++){
        if(j>=3){
            cout<<endl;
            j=0;
        }
        cout<<b[i]<<" ";
    }
    return 0;
}

  

这个题还有bug没找到,把后边的大写字母一起加入的时候,就会出现找不到的现象,啊。

/*
 * @Issue: 从键盘输入字符b,用折半查找找出已经排序好的字符串a中的字符b,不存在输出**,存在输出其位置
 * @Author: 一届书生
 * @LastEditTime : 2020-01-19 10:31:17
 */
#include<iostream>
#include<string>
using namespace std;
    string a="abcdefghijklmnopqrstuvwxyz";//ABCDEFGHIJKLMNOPQRSTUVWXYZ
    char b;

int main(){
    while(cin>>b){
        int len=a.length();
        // for(int i=0;i<len;i++)cout<<a[i];
        int left=0,right=len-1;
        while(left<=right){
            int mid=(left+right)>>1;
            if(a[mid]==b){
                cout<<mid+1<<endl;
                break;
            }
            else if(b<a[mid])right=mid-1;
            else left=mid+1;
        }
        if(left>right)cout<<"**"<<endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/52dxer/p/12212696.html