输入有多组数据,每组数据包含一个正整数n (1≤n≤2147483647)和一个正整数r (2≤r≤16)。
其中n为十进制整数,r为目标进制。
对应每组输入,输出整数n转换成r进制之后,其中包含多少个“1”。
1 2<br/>123 16
1<br/>0
#include <iostream> using namespace std; void one(long long n, int r) { int temp = 0, ans = 0; while (n != 0) { temp = n % r; if (temp == 1) { ans++; } n = n / r; } cout << ans << endl; } int main() { long long n = 0; int r = 0; while (cin >> n >> r) { one(n, r); } return 0; }
import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); BigInteger n; int r;String str; while(in.hasNext()){ n = in.nextBigInteger(); r = in.nextInt(); int count = 0; str = n.toString(r); for(int i = 0; i< str.length(); i++){ if(str.charAt(i)=='1'){ count++; } } System.out.println(count); } } }
#include<stdio.h> int countOne(int num, int p) { int count=0; while(num) { if(num%p==1) count++; num/=p; } return count; } int main() { int n,p; while(scanf("%d %d",&n,&p)!=EOF) { printf("%d\n",countOne(n,p)); } }
#include<iostream> using namespace std; int isOne(int a,int b,int re); //求出1的个数的函数 int main(){ int a=0,b=0; int result=0; while(cin>>a>>b){ int re=0; result = isOne(a,b,0); cout<<result<<endl; } return 0; } //a是目标数,b是进制,re是1的数量 int isOne(int a,int b,int re){ if(a/b==0){ if(a%b==1){ re++; } return re; }else{ if(a%b==1){ re++; } isOne(a/b,b,re); //这里用了一下递归 } }
import java.util.Scanner; public class Main { public static String f(int n, int base) { if (n == 0) return ""; return f(n / base, base) + ((n % base) >= 10 ? "" : (n % base)); } public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); int base = in.nextInt(); int cnt = 0; String s = f(n, base); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '1') cnt++; } System.out.println(cnt); } } }
水题一道。。。
进制转换的题,模拟手动进制转换即可。
#include <iostream> using namespace std; int main(int argc, const char * argv[]) { int n = 0, r = 0; //scanf返回值为正确输入数据的变量个数,当一个变量都没有成功获取数据时,此时返回-1 while (scanf("%d %d", &n, &r) != - 1) { int count = 0; //只要n != 0,则说明还需要进位转换 while (n != 0) { if (n % r == 1) { //余数为1,即进制转换后这位出现了1 count += 1; } n /= r; } printf("%d\n", count); } return 0; } ———————————————— 版权声明:本文为CSDN博主「hestyle」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://hestyle.blog.csdn.net/article/details/104659929
#include <stdio.h> #include <stdlib.h> int exchange(int, int);//进制转换 int main() { int n, r; while(~scanf("%d %d", &n, &r)) { printf("%d\n", exchange(n, r)); } } int exchange(int n, int r) { int count = 0; while(n) { if(n % r == 1) count++; n = n/r; } return count; }
写了一个进制转换 然后统计输出。 #include <iostream> #include <vector> #include <math.h> using namespace std; int main() { int n, r; int i = 0; char ascii[] = { 'A','B','C','D','E','F' }; vector<int> out; while (cin >> n >> r) { i = 0; while (n) { i++; int tempR = pow(r, i); int temp = n - (n / tempR)*tempR; out.push_back(temp / (pow(r,i-1))); n = n - temp; //cout << temp / (pow(r, i - 1) )<< endl; } int count = 0; for (int j = 0; j < out.size(); j++) { if (out[j] == 1) { count++; } /* if (out[out.size() - j - 1] >= 10) { cout << ascii[out[out.size() - j - 1] - 10]; } else cout << out[out.size() - j - 1]; if (j == out.size() - 1) { cout << endl; } */ } out.clear(); cout << count << endl; } }
#include <stdio.h>int Switch(intn, intr, int*a);int main(){int n;int r;int i;int a[10000];while(scanf("%d %d", &n, &r)!=EOF){int count=0;int len = Switch(n, r, a);for(i=0;i<len;i++){if(a[i]==1){count++;}}printf("%d\n", count);}return 0;}int Switch(int n, int r, int *a){int i=0;while(n!=0){a[i] = n % r;i++;n /= r;}return i;}
#include<stdio.h>#include<iostream>using namespace std;charb[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};inta[32];intconv(intn,intr){intlen=0,i=0,sum=0;while(n){a[len]=n%r;n/=r;if(a[len++]==1)sum++;}returnsum;}intmain(){intn,r;while(cin>>n>>r){cout<<conv(n,r)<<endl;}return0;}