特殊的求和(函數和循環)

【問題描述】
編寫函數int fun(int a,int n)求Sn = a + aa + aaa + … + aa…a 的值(最后一個數中 a 的個數為 n ),其中 a 是一個1~9的數字,例如:2 + 22 + 222 + 2222 + 22222 (此時 a=2 n=5 ) 。參數由主函數傳入,最終結果傳回主函數再輸出。

【輸入形式】
一行,包括兩個整數,第一個為a,第2個為n(1 ≤ a, n ≤ 9),以空格分隔。

【輸出形式】 一行,Sn的值。

【樣例輸入】 2 5

【樣例輸出】 24690

#include <iostream>
using namespace std;int fun(int a,int n);int main()
{int a,n,sum;cin >> a >> n;sum = fun(a,n);cout << sum << endl;return 0;
}int fun(int a,int n)
{int sum = 0, x = a;//這個地方設一個x變量是很有必要的for(int i=1;i<=n;++i){sum += a;a = a*10 + x;//不能寫a+=a*10,這個算法會出問題}return sum;
}

轉載于:https://www.cnblogs.com/yuzilan/p/10626131.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278516.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278516.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278516.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ms project 入門_Microsoft Project 2010入門

ms project 入門Would you like to keep your projects on track and keep track of how time and resources are used? Let’s take a look at Microsoft Project 2010 and how it can help you stay on top of your projects. 您想保持項目進度并了解如何使用時間和資源嗎&…

mysql基本的增刪改查和條件語句

增 insert into 表名&#xff08;列名,列名。。。。。。&#xff09; values("test1",23),("test2",23),("test3",24); 這條命令可以一次增加一條數據&#xff0c;也可以同時增加多條數據 還可以從插入其他的表到數據到當前表 insert into 插入的…

后端model傳入前端JSP頁面中的值判斷后再取值

所遇到的問題后端model傳入前端JSP頁面中的值通過foreach循環內要滿足條件才能取值給Div中&#xff0c;我們知道jsp頁面中可以直接用EL表達式取值&#xff0c;格式就是${"model中傳來的數據"},但是我要把傳過來的數據判斷后再取值就遇到了問題&#xff0c;通過查百度…

黑莓os軟件下載_在PC上試用BlackBerry OS

黑莓os軟件下載There’s a wider selection of smart phones and mobile OS’s than ever before, but you can’t just go buy every phone available and try them all out. Here’s how you can test out the latest version of the BlackBerry OS for free on your PC. 智…

IO流之轉換流

一 轉換流 1 OutputStreamWriter類 是字符流通向字節流的橋梁&#xff1a; 可使用指定的字符編碼表&#xff0c;將要寫入流中的字符編碼成字節。它的作用的就是&#xff0c;將字符串按照指定的編碼表轉成字節&#xff0c;在使用字節流將這些字節寫出去。 public static void m…

Spring事務管理(三)-PlatformmTransactionManager解析和事務傳播方式原理

2019獨角獸企業重金招聘Python工程師標準>>> Spring在事務管理時&#xff0c;對事務的處理做了極致的抽象&#xff0c;即PlatformTransactionManager。對事務的操作&#xff0c;簡單地來說&#xff0c;只有三步操作&#xff1a;獲取事務&#xff0c;提交事務&#x…

div方框彎曲邊樣式_使用彎曲樣式編輯文本

div方框彎曲邊樣式Would you like a new Notepad replacement that incorporates the latest technologies while staying slim and fast? Text editors are usually bland, boring programs, but here’s a new one that makes your text come to life beautifully. 您是否想…

分布式鎖的幾種實現原理

分布式鎖主流有三種模式&#xff1a; 實現方式 功能要求 實現難度 學習成本 運維成本 MySQL 的方案借助表鎖/行鎖實現 滿足基本要求 不難 熟悉 小量OK、大量影響現有業務、1主多從架構&#xff0c;不方便擴容 通過 ZK 創建數據節點的方式實現 滿足要求 熟悉 ZK API 即可 需要學…

如何破解您忘記的Windows密碼

Here at How-To Geek, we’ve covered many different ways to reset your password for Windows—but what if you can’t reset your password? Or what if you’re using drive encryption that would wipe out your files if you changed the password? It’s time to cr…

sql語句練習50題(Mysql版)

表名和字段–1.學生表Student(s_id,s_name,s_birth,s_sex) –學生編號,學生姓名, 出生年月,學生性別–2.課程表Course(c_id,c_name,t_id) – –課程編號, 課程名稱, 教師編號–3.教師表Teacher(t_id,t_name) –教師編號,教師姓名–4.成績表Score(s_id,c_id,s_score) –學生編號…

OpenCV3 識別圖中表格-JAVA 實現

2019獨角獸企業重金招聘Python工程師標準>>> 關于 JAVA 學習 OpenCV 的內容&#xff0c;函數講解。內容我均整理在 GitHubd的OpenCV3-Study-JAVA OpenCV 3 識別圖中表格-Java 實現 1. 說明 網上大部分資料&#xff0c;都是針對 C的&#xff0c;python、java 的例子太…

內存泄露 體現在哪個數字上_Microsoft剛剛泄漏了一個新的開始菜單。 你喜歡哪個?...

內存泄露 體現在哪個數字上NTAuthority on TwitterTwitter上的NTAuthorityMicrosoft messed up today, releasing an internal build of Windows 10 to Windows Insiders. This build was never meant to see the light of day, but it features a new Start menu design—with…

簡述 Spring Cloud 是什么

很多同學都了解了Spring &#xff0c;了解了 Spring Boot, 但對于 Spring Cloud 是什么還是比較懵逼的。 本文帶你簡單的了解下&#xff0c;什么是Spring Cloud。 Spring Cloud 是什么 從字面理解&#xff0c;Spring Cloud 就是致力于分布式系統、云服務的框架。 Spring Cloud …

js DOM節點

元素節點 4種方式獲取 var oDiv document.getElementById("box");        //通過Id獲取元素var oDiv document.getElementsByClassName("div")[0];   //通過類名獲取元素  --》[ 0 ] 必須寫var oDiv document.getElementsByTagName("…

python web scraping

2019獨角獸企業重金招聘Python工程師標準>>> 最近在看《Web Scraping with Python》&#xff0c;借此來熟悉Python2.7如何開始編程。 發現書上主要使用的 http://example.webscraping.com/ 網站有部分變化&#xff0c;書中的代碼有點無法對照使用&#xff0c;因此稍…

傅里葉變換的物理意義

用三角函數表示周期函數 傅里葉的相關理論始于下面假設&#xff1a;對于周期為1的信號$f(t)$&#xff0c;可以由不同頻率的三角函數組成&#xff0c; $f(t) \frac{a_0}{2}\displaystyle{\sum^{\infty}_{k1}}(a_kcos(2\pi kt)b_ksin(2\pi kt))$ 組成的基礎波形為一個信號對&…

天貓年度總結

2019獨角獸企業重金招聘Python工程師標準>>> 魯大師天貓工作總結 時間&#xff1a;2017年10月22日-1月30日 1、對代理商進行6大區域的劃分管理&#xff0c;有專門的客服指導。 2、加班費申請和車費報銷制度。 3、簡化了特權訂金2階段改成1階段&#xff0c;極大的方便…

因特網使用期限_Internet死亡時使用PC的其他方式

因特網使用期限Nothing is more annoying than getting your Internet connection shut down, due to weather, or perhaps forgetting to pay your bill. Let’s take a look at some ways you can be productive and entertained without the Internet. 沒有什么比由于天氣原…

【基礎操作】線性基詳解

線性基是一個奇妙的集合&#xff08;我摘的原話&#xff09; 這里以非 $OI$ 的角度介紹了線性基 基礎部分 模板題 給你 $n$ 個數的集合&#xff0c;讓你選出任意多個不重復的數&#xff0c;使得它們的異或和最大。 線性基是什么 我們稱集合 $B$ 是集合 $S$ 的線性基&#xff0c…

節省大量教科書的三種潛在風險方法

Photo by Sultry 攝影&#xff1a; Sultry You can always save money on textbooks by buying online, going ebook, or renting what you need. But there are riskier ways to save a buck that just may yield even greater payoff, such as getting the international or …