[DP]343. Integer Break

探讨如何将正整数n拆分成至少两个正整数之和,并最大化这些整数的乘积。通过数学分析得出算法,使用C++实现。适用于范围2至58的整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: You may assume that n is not less than 2 and not larger than 58.


题目分析:

1、根据题意,我们需要将给出的N(1<N<59)拆分成至少两个数,使其和为N,且要尽量让其乘积最大;

2、首先根据例举一些简单的数的积最大拆分我们可以发现,除了2和3以外(因为至少要拆分为两个数),别的数要使其拆分后积最大,就要将其拆分为2和3组成的和(且尽可能有多的3)。因此算法就变得很简单了,只需要N/3统计能拆分为多少个3,再根据N%3来判断是否能拆成完全的2和3的组合(余数为0则全为3,余数为2则有一个2,余数为1则须减少一个3改为两个2);

代码:

#include<cmath>
class Solution {
public:
    int integerBreak(int n) {
        if(n == 2) return 1;
        if(n == 3) return 2;
        
        int p = n/3;
        if(n % 3 == 0)
            return pow(3, p);
        else if(n % 3 == 1)
            return pow(3, p - 1) * 4;
        else
            return pow(3, p) * 2;
    }
};

3、对上述算法进行理论上的分析:

     首先对于2和3来说,它们符合拆分为尽可能多的3,剩下的为2的算法过程和结论(不考虑必须拆分为至少两个数的限制)。

     对于大于3任意数N而言,假设它已经被拆分为3和2的和的形式,如果有一个更优解(和式包含不为2或3的数N0),那么N0本身也可以被表示为2和3的和式,也就是说这个最优解利用N0替换表示N的2和3的和式中的一部分,已达到更大的乘积,假设N0 = 3m + 2n,那么N0 > 3^m + 2^n即3m + 2n > 3^m + 2^n,显然式子在m, n > 1时不可能成立。所以推翻了又更优解的可能。 

实验四的界面是图书查询界面Ss.java类代码: import static com.example.myapplication.R.id.tt_hg; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceManager; public class Ss extends AppCompatActivity { private EditText editId; private EditText editName; private EditText editAuthor; private static final int item1 = Menu.FIRST; private static final int item2 = Menu.FIRST + 1; public void onResume(){ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String size = sp.getString("font_size","25"); EditText syqt = findViewById(R.id.edit_id); syqt.setTextSize(Integer.valueOf(size)); EditText cxqt = findViewById(R.id.edit_name); cxqt.setTextSize(Integer.valueOf(size)); EditText szqt = findViewById(R.id.edit_author); szqt.setTextSize(Integer.valueOf(size)); RadioButton dfqt = findViewById(tt_hg); dfqt.setTextSize(Integer.valueOf(size)); RadioButton dft = findViewById(R.id.tt_hu); dft.setTextSize(Integer.valueOf(size)); Button dfttd = findViewById(R.id.btn_ff); dfttd.setTextSize(Integer.valueOf(size)); Button dftqq = findViewById(R.id.btn_clar); dftqq.setTextSize(Integer.valueOf(size)); Button dftww = findViewById(R.id.btn_back); dftww.setTextSize(Integer.valueOf(size)); Button dftwwl = findViewById(R.id.ff_id1); dftwwl.setTextSize(Integer.valueOf(size)); Button dftwws = findViewById(R.id.ff_id2); dftwws.setTextSize(Integer.valueOf(size)); Button dftwwt = findViewById(R.id.ff_id3); dftwwt.setTextSize(Integer.valueOf(size)); updateUI(); super.onResume(); } private void updateUI() { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String fontSize = sp.getString("font_size", "15"); int size = Integer.parseInt(fontSize); String textColor = sp.getString("text_color", "#FF000000"); int color = Color.parseColor(textColor); applyStyleToEditText(editId, size, color); applyStyleToEditText(editName, size, color); applyStyleToEditText(editAuthor, size, color); } private void applyStyleToEditText(EditText btn, int size, int color) { btn.setTextSize(size); btn.setTextColor(color); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ss); // 初始化 EditText editId = findViewById(R.id.edit_id); editName = findViewById(R.id.edit_name); editAuthor = findViewById(R.id.edit_author);//通过ID绑定视图对象 RadioButton ttHg = findViewById(R.id.tt_hg); RadioButton ttHu = findViewById(R.id.tt_hu); Button btnFf = findViewById(R.id.btn_ff); LinearLayout ll = findViewById(R.id.table_layout); registerForContextMenu(ll); btnFf.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 在此处编写查询逻辑(示例:Toast提示) // Toast.makeText(Ss.this, "查询按钮被点击,执行查询操作", Toast.LENGTH_SHORT).show(); } }); // 1清空 - 属性事件响应(XML 按钮需添加 android:onClick="clearByAttribute") // 2清空 - 内部匿名类事件响应 findViewById(R.id.ff_id1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearFields(); } }); // 3清空 - 监听器接口实现类事件响应 findViewById(R.id.ff_id2).setOnClickListener(new MyClickListener()); // 4清空 - 外部监听器接口实现类事件响应 findViewById(R.id.ff_id3).setOnClickListener(new ExternalClickListener(this)); Button btnBack = findViewById(R.id.btn_back); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Ss.this, Sy.class); startActivity(intent); finish(); } }); } // 1清空对应的属性事件响应方法 public void clearByAttribute(View view) { clearFields(); }//当用户点击按钮就会被执行 // 清空文本框的公共方法 public void clearFields() { if (editId != null) editId.setText(""); if (editName != null) editName.setText(""); if (editAuthor != null) editAuthor.setText(""); } // 内部监听器接口实现类(3清空) private class MyClickListener implements View.OnClickListener { @Override public void onClick(View v) { clearFields(); } } // 外部监听器接口实现类(4清空) @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo info) { menu.add(0, 1, 0, "删除"); menu.add(0, 2, 0, "修改"); super.onCreateContextMenu(menu, view, info); } @Override public boolean onContextItemSelected(MenuItem item) { if (item.getItemId() == 1) { Toast.makeText(this, "您点击了删除", Toast.LENGTH_LONG).show(); } else if (item.getItemId() == 2) { Toast.makeText(this, "您点击了修改", Toast.LENGTH_LONG).show(); } return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, item1, 0, "关于"); menu.add(0, item2, 0, "退出"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case item1: Toast.makeText(Ss.this, "姓名:骆树涛。学号:2404224132", Toast.LENGTH_SHORT).show(); break; case item2: this.finish(); break; } return true; } } 图书查询界面的ss.XML布局文件代码: <?xml version="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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书编号" android:textSize="20dp"/> <EditText android:id="@+id/edit_id" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书名称" android:textSize="20dp"/> <EditText android:id="@+id/edit_name" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书作者" android:textSize="20dp"/> <EditText android:id="@+id/edit_author" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中文" android:textSize="20dp"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="外文" android:textSize="20dp"/> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" android:textSize="20dp"/> <Button android:id="@+id/btn_clar" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="清空" android:textSize="20dp" android:onClick="clearByAttribute"/> <Button android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:textSize="20dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/ff_id1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" android:textSize="20dp"/> <Button android:id="@+id/ff_id2" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="清空" android:textSize="20dp" /> <Button android:id="@+id/ff_id3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" android:textSize="20dp"/> </LinearLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="*"> <TableRow> <TextView android:text="图书编号" android:textSize="15dp"/> <TextView android:text="图书名称" android:textSize="15dp"/> <TextView android:text="图书作者" android:textSize="15dp"/> <TextView android:text="馆藏地点" android:textSize="15dp"/> </TableRow> <TableRow android:id="@+id/table_layout"> <TextView android:text="1001" android:textSize="15dp"/> <TextView android:text="Android" android:textSize="15dp"/> <TextView android:text="张三" android:textSize="15dp"/> <TextView android:text="5-423" android:textSize="15dp"/> </TableRow> <TableRow> <TextView android:text="查询结果1条" android:textSize="15dp" android:layout_span="4" android:layout_gravity="center"/> </TableRow> </TableLayout> </LinearLayout> 实验六的代码如下 新增Xz.JAVA类代码 import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceManager; public class Xz extends AppCompatActivity { private EditText editBookId; private EditText editBookName; private EditText editBookAuthor; private EditText editBookLocation; public void onResume(){ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String size = sp.getString("font_size","25"); EditText syq = findViewById(R.id.edit_book_id); syq.setTextSize(Integer.valueOf(size)); EditText cxq = findViewById(R.id.edit_book_name); cxq.setTextSize(Integer.valueOf(size)); EditText szq = findViewById(R.id.edit_book_author); szq.setTextSize(Integer.valueOf(size)); EditText dfq = findViewById(R.id.edit_book_location); dfq.setTextSize(Integer.valueOf(size)); updateUI(); super.onResume(); } private void updateUI() { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String fontSize = sp.getString("font_size", "15"); int size = Integer.parseInt(fontSize); String textColor = sp.getString("text_color", "#FF000000"); int color = Color.parseColor(textColor); applyStyleToEditText(editBookId, size, color); applyStyleToEditText(editBookName, size, color); applyStyleToEditText(editBookAuthor, size, color); applyStyleToEditText(editBookLocation, size, color); } private void applyStyleToEditText(EditText btn, int size, int color) { btn.setTextSize(size); btn.setTextColor(color); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xz); editBookId = findViewById(R.id.edit_book_id); editBookName = findViewById(R.id.edit_book_name); editBookAuthor = findViewById(R.id.edit_book_author); editBookLocation = findViewById(R.id.edit_book_location); Button btnAddBook = findViewById(R.id.btn_add_book); btnAddBook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String bookId = editBookId.getText().toString(); String bookName = editBookName.getText().toString(); String bookAuthor = editBookAuthor.getText().toString(); String bookLocation = editBookLocation.getText().toString(); Intent intent = new Intent(Xz.this, Xq.class); intent.putExtra("bookId", bookId); intent.putExtra("bookName", bookName); intent.putExtra("bookAuthor", bookAuthor); intent.putExtra("bookLocation", bookLocation); startActivity(intent); } }); } } 新增的xz.XML布局文件代码 <?xml version="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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书编号" android:textSize="20dp"/> <EditText android:id="@+id/edit_book_id" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书名称" android:textSize="20dp"/> <EditText android:id="@+id/edit_book_name" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书作者" android:textSize="20dp"/> <EditText android:id="@+id/edit_book_author" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="馆藏地点" android:textSize="20dp"/> <EditText android:id="@+id/edit_book_location" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/btn_add_book" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新增" android:textSize="20dp" android:layout_gravity="center"/> </LinearLayout> 详情Xq.JAVA类代码 import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceManager; public class Xq extends AppCompatActivity { private static final int ITEM_HOME = Menu.FIRST; public void onResume(){ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String size = sp.getString("font_size","25"); TextView syqw = findViewById(R.id.tv_book_id); syqw.setTextSize(Integer.valueOf(size)); TextView cxqw = findViewById(R.id.tv_book_name); cxqw.setTextSize(Integer.valueOf(size)); TextView szqw = findViewById(R.id.tv_book_author); szqw.setTextSize(Integer.valueOf(size)); TextView dfqw = findViewById(R.id.tv_book_location); dfqw.setTextSize(Integer.valueOf(size)); super.onResume(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xq); TextView tvBookId = findViewById(R.id.tv_book_id); TextView tvBookName = findViewById(R.id.tv_book_name); TextView tvBookAuthor = findViewById(R.id.tv_book_author); TextView tvBookLocation = findViewById(R.id.tv_book_location); Intent intent = getIntent(); String bookId = intent.getStringExtra("bookId"); String bookName = intent.getStringExtra("bookName"); String bookAuthor = intent.getStringExtra("bookAuthor"); String bookLocation = intent.getStringExtra("bookLocation"); tvBookId.setText("图书编号:" + bookId); tvBookName.setText("图书名称:" + bookName); tvBookAuthor.setText("图书作者:" + bookAuthor); tvBookLocation.setText("馆藏地点:" + bookLocation); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, ITEM_HOME, 0, "首页"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case ITEM_HOME: Intent intent = new Intent(Xq.this, Sy.class); startActivity(intent); finish(); return true; default: return super.onOptionsItemSelected(item); } } } 详情的xq.XML布局文件代码 <?xml version="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" android:padding="16dp"> <TextView android:id="@+id/tv_book_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> <TextView android:id="@+id/tv_book_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> <TextView android:id="@+id/tv_book_author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> <TextView android:id="@+id/tv_book_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> </LinearLayout> 首页Sy.JAVA类代码 import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceManager; public class Sy extends AppCompatActivity { private Button btnAdd; private Button btnSearch; private Button btnSetting; private static final int item1 = Menu.FIRST; private static final int item2 = Menu.FIRST + 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sy); btnAdd = findViewById(R.id.btn_add); btnSearch = findViewById(R.id.btn_search); btnSetting = findViewById(R.id.btn_setting); btnSetting.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Sy.this, SettingsActivity.class); startActivity(intent); } }); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Sy.this, Xz.class); startActivity(intent); } }); btnSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Sy.this, Ss.class); startActivity(intent); } }); } public void onResume(){ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String size = sp.getString("font_size","25"); Button sy = findViewById(R.id.btn_add); sy.setTextSize(Integer.valueOf(size)); Button cx = findViewById(R.id.btn_search); cx.setTextSize(Integer.valueOf(size)); Button sz = findViewById(R.id.btn_setting); sz.setTextSize(Integer.valueOf(size)); String bg = sp.getString("bg","c"); int pic_id = this.getResources().getIdentifier(bg,"drawable","com.example.myapplication"); LinearLayout bg_b = findViewById(R.id.bg_d); bg_b.setBackgroundResource(pic_id); updateUI(); super.onResume(); } private void updateUI() { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String fontSize = sp.getString("font_size", "15"); int size = Integer.parseInt(fontSize); String textColor = sp.getString("text_color", "#FF000000"); int color = Color.parseColor(textColor); applyStyleToButton(btnAdd, size, color); applyStyleToButton(btnSearch, size, color); applyStyleToButton(btnSetting, size, color); } private void applyStyleToButton(Button btn, int size, int color) { btn.setTextSize(size); btn.setTextColor(color); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, item1, 0, "关于"); menu.add(0, item2, 0, "退出"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case item1: Toast.makeText(Sy.this, "姓名:骆树涛。学号:2404224132", Toast.LENGTH_SHORT).show(); break; case item2: finishAffinity(); break; } return true; } } 首页的sy.XML布局文件代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/bg_d" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新增" android:textSize="15dp"/> <Button android:id="@+id/btn_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" android:textSize="15dp"/> <Button android:id="@+id/btn_setting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置" android:textSize="15dp"/> </LinearLayout> SettingsActivity.Java设置类代码: import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceFragmentCompat; public class SettingsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.se); // 加载容器布局 // 加载PreferenceFragment到Activity中 getSupportFragmentManager() .beginTransaction() .replace(android.R.id.content, new SettingsFragment()) .commit(); } // 静态内部类:管理设置项的Fragment public static class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { // 从XML文件加载设置项(需创建res/xml/settings.xml) setPreferencesFromResource(R.xml.settings, rootKey); } } } Se.XML布局文件代码: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/content" android:layout_width="match_parent" android:layout_height="match_parent"/> arry.XML的valuse代码: <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 颜色选项 --> <string-array name="color_entries"> <item>黑色</item> <item>红色</item> <item>黄色</item> <item>蓝色</item> </string-array> <string-array name="color_values"> <item>#FF000000</item> <!-- 黑色 --> <item>#FFFF0000</item> <!-- 红色 --> <item>#FFFFFF00</item> <!-- 黄色 --> <item>#FF0000FF</item> <!-- 蓝色 --> </string-array> <!-- 背景图片选项(需在drawable目录准备对应图片) --> <string-array name="bg"> <item>科技</item> <item>书籍</item> </string-array> <string-array name="bg_hhg"> <item>a</item> <item>b</item> <item>c</item> </string-array> </resources> sring.XML的xml代码: <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 字号设置 --> <EditTextPreference android:key="font_size" android:title="字号设置" android:summary="输入文字大小" android:defaultValue="15" android:inputType="number"/> <!-- 颜色选择 --> <ListPreference android:key="text_color" android:title="文字颜色" android:summary="选择文字显示颜色" android:defaultValue="black" android:entries="@array/color_entries" android:entryValues="@array/color_values"/> <!-- 背景图片选择 --> <ListPreference android:key="bg" android:title="首页背景" android:summary="选择首页背景图片" android:entries="@array/bg" android:entryValues="@array/bg_hhg"/> </PreferenceScreen> AndroidManifest.xml代码 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication" tools:targetApi="31"> <meta-data android:name="com.google.android.actions" android:resource="@layout/sy" /> <activity android:name=".Sy" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Xz" /> <activity android:name=".Xq" /> <activity android:name=".Ss" /> <activity android:name=".Cada" /> <!-- 新增:注册设置活动 --> <activity android:name=".SettingsActivity" android:label="设置" android:theme="@style/Theme.AppCompat.Light.DarkActionBar" /> <!-- <activity--> <!-- android:name=".SettingsActivity"--> <!-- android:exported="true"--> <!-- android:label="@string/app_name"--> <!-- android:theme="@style/Theme.MyApplication"/>--> <!-- <intent-filter>--> <!-- <action android:name="android.intent.action.MAIN" />--> <!-- <category android:name="android.intent.category.LAUNCHER" />--> <!-- </intent-filter>--> <!-- </activity>--> </application> </manifest> 内部存储(Internal Storage)的Context中提供了相应的函数对文件进行操作, 实验7:基于文件存储的数据读写 在实验6的基础上,实现基于Android系统文件存储的数据存储功能,具体要求如下: (1)增加一个励志标题添加功能。在首页增加“设置标题”按钮,点击后进入励志标题编辑界面,励志标题文字保存在内部存储的文件中,文件名以开发者自己的姓名拼音命名,例如“zhangsan.txt”。用户打开软件后,标题栏的标题文字从内部文件中读取并显示。 (2)修改图书新增功能。点击“新增”按钮后,用户输入的图书数据存储在外部文件中,文件名以开发者自己的姓名拼音命名,例如“zhangsan.txt”。数据新增完毕后,自动跳到图书查询页面。 (3) 修改图书查询页面。打开图书查询界面后,界面中的那条图书信息从外部文件中读取,并显示在相应的位置。 以上内容的全部代码
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值