對於一個整數N,計算2的N次方並在螢幕顯示十進位制結果。
(512<=N<=1024)
#include <string> #include <iostream> using namespace std; string NPowerOf2(int n){ string res("1"); for( int i =0;i < n; ++i){ int carry = 0, multi= 0; for(int j = res.size( )-1; j >= 0; --j){ int tmpNum = res[j] - '0'; multi= (tmpNum<<1) + carry; carry = multi/10; res[j] = (multi%10) + '0'; } if( carry> 0) res.insert(res.begin( ), carry+'0'); } return res; } int main( ) { int n; while( cin>>n) cout<<NPowerOf2(n)<<endl; return 0; }
相關推薦
對於一個整數N,計算2的N次方並在螢幕顯示十進位制結果。
(512<=N<=1024) #include <string> #include <iostream> using namespace std; string NPowerOf2(int n){ string res("1"
對於一個m*n的整數矩陣,其中每一行和每一列的元素都按升序排列,設計一個高效的演算法判斷一個數值是否存在,並給出位置
package com.huanchuang.arvin.vo; public class Finder { private String findElement(int[][] matrix, int target) { int row = 0,
對於給定的一個字串,統計其中數字字元出現的次數。輸入資料有多行,第一行是一個整數n,表示測試例項的個數,後面跟著n行,每行包括一個由字母和數字組成的字串。
#include <iostream> using namespace std; int main() {int n,i,s;char x; cin>>n;
譚浩強 c程序設計 8.17用遞歸法將一個整數n轉換成字符串。例如,輸入486,應輸出字符串"486"。n的位數不確定,可以是任意位數的整數。
tco xsl bof hcl mku owb kit gym code 8.17用遞歸法將一個整數n轉換成字符串。例如,輸入486,應輸出字符串"486"。n的位數不確定,可以是任意位數的整數。 #include <stdio.h>char str1[20]
10.16輸入一個字符串,內有數字和非數字字符,如: a123x456 17960? 302tab5876 將其中連續的數字作為一個整數,依次存放到一數組num中。例如123放在num[0]中,456放在num[1]中……統計共有多少個整數,並輸出這些數。
tab lnp zip sm2 cuc ycm rds qt5 tft 10.16輸入一個字符串,內有數字和非數字字符,如: a123x456 17960? 302tab5876 將其中連續的數字作為一個整數,依次存放到一數組num中。例
求小於一個整數n的所有素數
** 主要知識點: 一個只能被自己和1整除的正整數就是素數,也叫質數,這裡有個規律,就是一個數如果不能被大於2且小於這個整數平方根的數整除,那麼這個數就是素數。 實現程式碼: import java.util.ArrayList; /* * 求小於一個自然數n的所有素數
【C語言】輸入一個整數N,求N以內的素數之和
【C語言】輸入一個整數N,求N以內的素數之和 /* ============================================================================ Name
大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項為0)。
題目描述 大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項為0)。 n<=39 /* 思路: 就是簡單的斐波那契數列,按照正常的思路求解即可 可以分為遞迴和非遞迴,這裡介紹非遞迴的方式 */ class Solution { pub
從鍵盤中輸入一個整數n,求1-n的和,以及偶數和、奇數和
n=int(input("從鍵盤中輸入一個數:")) sum1=0 sum2=0 sum3=0 i=1 while i<=n: sum1+=i if i%2==0: sum2+=i else:
劍指offer第32題JS演算法:輸入一個整數n,求從1到n這n個整數的十進位制表示中1出現的次數。例如輸入12,從1到12這些整數中包含1的數字有1,10,11和12,1一共出現了5次
題目:輸入一個整數n,求從1到n這n個整數的十進位制表示中1出現的次數。例如輸入12,從1到12這些整數中包含1的數字有1,10,11和12,1一共出現了5次 這是我某一次去朋友公司面試試水時出的面試題,結果給我五分鐘我寫了個for迴圈的方法,被狠狠鄙視/哭笑不得 結果回來後好奇就跟同事
《程式設計師程式碼面試指南》給定一個整數n,返回從1到n的數字中1出現的次數
題目: 給定一個整數n,返回從1到n的數字中1出現的次數。 例如: n=5,1~n為1,2,3,4,5。那麼1出現1次所以返回1。 n=11,1~n為1,2,3,4,5,6,7,8,9,10,11。那麼1出現的次數為1(1,10,11)返回4。 import j
網易面試之對於一個整數X,定義操作rev(X)為將X按數位翻轉過來,並且去除掉前導0
對於一個整數X,定義操作rev(X)為將X按數位翻轉過來,並且去除掉前導0。例如: 如果 X = 123,則rev(X) = 321; 如果 X = 100,則rev(X) = 1.現在給出整數x和y
用遞迴法將一個整數n轉換成字串。 例如,輸入483,應輸出字串“483”。n的位數不確定,可以是任意的整數。
/*3、用遞迴法將一個整數n轉換成字串。 例如,輸入483,應輸出字串“483”。n的位數不確定,可以是任意的整數。*/ #include <stdio.h> int main()
C語言 用遞迴法將一個整數n轉換成字串
#include<stdio.h> int main(void) { int n; int enter_n(); void print_n(int n); n=enter_n(); print_n(n); return 0; } int enter_n()
(用python來繪製一個空心菱形) 輸入一個整數n,輸出一個空心菱形,其中每個邊由n個'*'組成。
def kongxin(n): for i in range(n): print() for j in range(n - i - 1): print(' ', end='') for k in range(2 * i + 1): if k==0
整數劃分如,對於正整數n=6,可以分劃為:6 5+1
import java.util.Scanner; public class IntHuafen { /* 整數劃分 如,對於正整數n=6,可以分劃為: 6 5+1 4+2, 4+1+1 3+
輸入一個整數n,求小於這個整數的所有質數。
演算法:定義一個長度為n的boolean陣列,true表示是質數,false表示不是質數。初始化為true,之後從2開始迴圈。步驟: I、找到第一個值為true的下標i。 II、把所有的下標為i的倍數的值 置為false。 III、直到掃描完陣列中的所有數值
java實現斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項。 n
public class Solution_feibonaqi { public int Fibonacci(int n) { int result[] = { 0, 1 }; if (n < 2) { return result[n];
給定一個整數n,返回n!中的尾隨零數。
本題源自leetcode 172 =------------------------------------------------------------------------------ 思路:求0來自10。10來自2 x 5, 我們需要考慮所有5和2的產品。喜歡4
python求解給定一個整數N,求N!末尾有多少個0,求N!的二進位制中最低位1的位置
之前做的銀聯題目有一道題目就是1000的階乘尾部有多少個0,記得這樣的題目之前是做過的,但是當時就是時間太緊了沒想起來,今天又遇到這個題目,索性查檢視看是怎麼算的,然後程式計算一下,加深理解,對於這樣的題目網上也有很多的解法,核心的思想就是找規律看問題的本質是什麼的,