请编写函数fun,函数的功能是:删去一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。例如,一维数组中的数据是:2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10。删除后,数组中的内容应该是:2 3 4 5 6 7 8 9 10。注意:部分源程序在文件PROGl.C中。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

题目

请编写函数fun,函数的功能是:删去一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。

例如,一维数组中的数据是:2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10。

删除后,数组中的内容应该是:2 3 4 5 6 7 8 9 10。

注意:部分源程序在文件PROGl.C中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。


相似考题
更多“ 请编写函数fun,函数的功能是:删去一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。例如,一维数组中的数据是:2 2 2 3 4 4 5 6 6 6 6 7”相关问题
  • 第1题:

    请编写函数fun,函数的功能是:移动一维数组中的内容:若数组中有n个整数,要求把下标从0到p(含p,p小于等于n-1)的数组元素平移到数组的最后。

    例如,一维数组中的原始内容为:1,2,3,4,5,6,7,8,9,10:p的值为3。移动后,一维数组中的内容应为:5, 6, 7, 8, 9, 10, 1, 2, 3,4。

    注意:部分源程序在文件PROGl.C中。

    请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。


    正确答案:
    解析:该程序功能是移动一维数组中的内容。解题思路首先将字符串拷贝到一个临时的数组,然后将字符串进行前移操作,最后将由于前移覆盖掉的字符,由临时数组中拷贝到指定字符串。

  • 第2题:

    函数fun()的功能是:逆置数组元素中的值。形参n给出数组中数据的个数。

    例如:若a所指数组中的数据依次为:1、2、3、4、5、6、7、8、9,则逆置后依次为:9、8、7、6、5、4、3、2、1。

    注意:部分源程序给出如下。

    请勿改动main()函数和其他函数中的任何内容,仅在fun()函数的横线上填入所编写的若干表达式或语句。

    试题程序:


    正确答案:

    【1】n/2【2】i【3】a[n-i-1]
    【解析】fun()函数实现的功能是实现数组的逆置。具体步骤通过参数传递,得到数组指针(即数组名)和数组记录大小的变量n,通过这两个参数配置for循环,遍历数组的中间的元素位置,每次循环都将i位置和对应位置结点(i距离0位置等于其距离尾位置的结点)的元素交换,由此实现数组的逆置。

  • 第3题:

    【简答题】编程实现功能:删去一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。 例如,若一维数组中的数据是:


    手机中每个应用程序消耗了多少手机流量,那么这些功能Android中是如何实现的呢?按照惯例,先来谈谈原理级别的东西。Android系统中封装了一套流量数据API,这些API可以很好的管理Android系统流量使用情况。可以基于这些Android API来实现管理手机流量的功能。这些API很好的封装在了android.net包下的TrafficStats中,主要的方法有:
    TrafficStats.getMobileRxBytes();//2g/3g接收的流量
    TrafficStats.getMobileRxPackets();//2g/3g接收的包信息
    TrafficStats.getMobileTxBytes();//2g/3g上传的流量
    TrafficStats.getMobileTxPackets();//2g/3g上传的包信息
    TrafficStats.getTotalRxBytes();//手机总共接收的流量
    TrafficStats.getTotalTxBytes();//手机总共上传的流量
    TrafficStats.getUidRxBytes(uid); //得到某个应用程程序接收的流量
    TrafficStats.getUidTxBytes(uid); //得到某个应用程程序接收的流量
    基于这些方法实现一个Android手机流量管理的程序示例。
    Android编程实现简单流量管理功能:
    packagecn.itcast.mobilesafe.ui;
    importjava.util.List;
    importandroid.app.Activity;
    importandroid.content.Intent;
    importandroid.content.pm.PackageManager;
    importandroid.content.pm.ResolveInfo;
    importandroid.graphics.drawable.Drawable;
    importandroid.net.TrafficStats;
    importandroid.os.Bundle;
    importandroid.view.View;
    importandroid.view.ViewGroup;
    importandroid.widget.BaseAdapter;
    importandroid.widget.ImageView;
    importandroid.widget.ListView;
    importandroid.widget.TextView;
    importcn.itcast.mobilesafe.R;
    importcn.itcast.mobilesafe.util.TextForMater;
    public classTrafficManagerActivity extends Activity {
    private TextView _3gTotal;
    private TextView wifiTotal;
    private ListView content;
    private String mobileTraffic;
    private String wifiTraffic;
    private PackageManager pm;
    private TrafficAdapter adapter;
    @Override
    protected void onCreate(BundlesavedInstanceState) {
    super.onCreate(savedInstanceState);
    pm = getPackageManager();
    setContentView(R.layout.traffic_manager);
    _3gTotal = (TextView)this.findViewById(R.id._3gTotal);
    wifiTotal = (TextView)this.findViewById(R.id.wifiTotal);
    content = (ListView)this.findViewById(R.id.content);
    setTotalData();
    adapter = new TrafficAdapter();
    content.addHeaderView(View.inflate(this,R.layout.traffic_title, null));
    content.setAdapter(adapter);
    }
    private void setTotalData() {
    long mobileRx =TrafficStats.getMobileRxBytes();
    long mobileTx = TrafficStats.getMobileTxBytes();
    long totalRx =TrafficStats.getTotalRxBytes();
    long totalTx =TrafficStats.getTotalTxBytes();
    long wifiRx = totalRx - mobileRx;
    long wifiTx = totalTx - mobileTx;
    mobileTraffic =TextForMater.getDataSize(mobileRx + mobileTx);
    _3gTotal.setText(mobileTraffic);
    wifiTraffic =TextForMater.getDataSize(wifiTx + wifiRx);
    wifiTotal.setText(wifiTraffic);
    }
    private class TrafficAdapter extendsBaseAdapter{
    List<ResolveInfo> resolveInfos ;
    public TrafficAdapter() {
    super();
    Intent intent = new Intent();
    intent.setAction("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.LAUNCHER");
    resolveInfos =pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    }
    @Override
    public int getCount() {
    return resolveInfos.size();
    }
    @Override
    public Object getItem(int position) {
    return position;
    }
    @Override
    public long getItemId(int position) {
    return position;
    }
    @Override
    public View getView(int position, ViewconvertView, ViewGroup parent) {
    View view ;
    if(null == convertView){
    view =View.inflate(getApplicationContext(), R.layout.traffic_item, null);
    }else{
    view = convertView;
    }
    ViewHolder holder = new ViewHolder();
    holder.iv_traffic_icon = (ImageView)view.findViewById(R.id.iv_traffic_icon);
    holder.tv_traffic_name = (TextView)view.findViewById(R.id.tv_traffic_name);
    holder.tv_traffic_tx = (TextView)view.findViewById(R.id.tv_traffic_tx);
    holder.tv_traffic_rx = (TextView)view.findViewById(R.id.tv_traffic_rx);
    ResolveInfo info =resolveInfos.get(position);
    String appName =info.loadLabel(pm).toString();
    holder.tv_traffic_name.setText(appName);
    Drawable icon = info.loadIcon(pm);
    holder.iv_traffic_icon.setImageDrawable(icon);
    int uid = info.activityInfo.applicationInfo.uid;
    holder.tv_traffic_rx.setText(TextForMater.getDataSize(TrafficStats.getUidRxBytes(uid)));
    holder.tv_traffic_tx.setText(TextForMater.getDataSize(TrafficStats.getUidTxBytes(uid)));
    return view;
    }
    }
    static class ViewHolder{
    ImageView iv_traffic_icon;
    TextView tv_traffic_name;
    TextView tv_traffic_tx;
    TextView tv_traffic_rx;
    }
    }
    traffic_manager.xml
    <?xmlversion="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="2G/3G总流量" />
    <TextView
    android:id="@+id/_3gTotal"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
    </TableRow>
    <TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Wifi总流量" />
    <TextView
    android:id="@+id/wifiTotal"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
    </TableRow>
    </TableLayout>
    <SlidingDrawer
    android:id="@+id/ll_sd_traffic"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:content="@+id/content"
    android:handle="@+id/handle"
    android:orientation="vertical">
    <ImageView
    android:id="@id/handle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/notification" />
    <ListView
    android:id="@id/content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    </ListView>
    </SlidingDrawer>
    </LinearLayout>
    traffic_manager_item.xml
    <?xmlversion="1.0" encoding="utf-8"?>
    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <ImageView
    android:id="@+id/iv_traffic_icon"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />
    <TextView
    android:id="@+id/tv_traffic_name"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="名称" />
    <TextView
    android:id="@+id/tv_traffic_tx"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="上传" />
    <TextView
    android:id="@+id/tv_traffic_rx"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="下载" />
    </LinearLayout>
    traffic_title.xml
    <?xmlversion="1.0" encoding="utf-8"?>
    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="图标" />
    <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="名称" />
    <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="上传" />
    <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="下载" />
    </LinearLayout>

  • 第4题:

    函数fun()的功能是:逆置数组元素中的值,形参m给出数组中数据的个数。

    例如,若x所指数组中的数据依次为:9、8、7、6、5,则逆置后依次为:5、6、7、8、9。

    注意:部分源程序给出如下。

    请勿改动函数main()与其他函数中的任何内容,仅在fun()函数的横线上填写所需的若干表达式或语句。

    试题程序:


    正确答案:

    【1】m/2【2】i【3】a[m-1-i]
    【解析】该题中函数功能是逆置数组元素的值。此类题的解题思路,是在简单的两个数值交换的基础上,增加对数值定位的功能,也就是确定要交换的数值在数组中的位置。
    第一空:对数组中的元素进行逆置,根据上面的分析,整个数组中元素交换的循环次数,应该是只需要交换整个数组长度的一半的次数,所以,第一空应填:“m/2”。
    第二空:元素逆置,就是第一个和最后一个交换,依次类推。个数为m的数组中,与第i个元素相对应需要交换的元素是第m-1-i个元素,所以第二空填:“i”。
    第三空:根据两个元素交换的算法,第i个和第m-i-1个元素交换的过程,应填:“a[m-1-i]”。

  • 第5题:

    下列给定程序中,函数fun的功能是:把形参a所指数组中的奇数按原顺序依次存放到a [0][2] .....中,把偶数从数组中删除,奇数个数通过函数值返回。 例如:若a所指数组中的数据最初排列为:9、1、4,2、3、6、5、8、7,删除偶数后a所指数组中的数据为:9、1、 3、5、7,返回值为5。 请在下画线处填入正确的内容并将下画线删除,使程序得出正确的结果。 注意:部分源程序在文件BLANKl.C中。 不得增行或删行,也不得更改程序的结构!


    正确答案:
    【考点分析】
    本题考查:if语句条件表达式;自增/自减运算符;函数返回值。
    【解题思路】
    填空1:根据题目要求,需要进行奇偶数的判定,可以通过if条件语句来判断数组元素是否是奇数,如果元素不能被2整除,则为奇数,所以填人if(a[i]%2==1)。
    填空2:将为奇数的元素重新存放到数组的前面,同时下标增1。
    填空3:函数返回值需要返回数组中奇数的个数,因此返回变量j。
    【解题宝典】
    奇数和偶数的判定方法,通过if条件语句完成,对2求余运算,结果为0是偶数,结果为1是奇数。
    自增1、自减1运算符:自增l运算符记为”++”,其功能是使变量的值自增l。自减1运算符记为”一一”,其功能是使变量值自减1。自增1、自减1运算符均为单目运算,都具有右结合性。有以下几种形式: