N諾OJ刷題C&C++(P1000-P1010)


P1000 - A+B 問題

n-nuo

【題目描述】

Time Limit: 1000 ms
Memory Limit: 256 mb
輸入A,B
輸出A+B
-1,000,000,000<=A,B<=1,000,000,000

【輸入描述】

輸入包含兩個整數A,B,用一個空格分隔。

【輸出描述】

輸出一個整數,表示A+B的值。

【輸入樣例】

5 8

【輸出樣例】

13

【代碼實現】

#include <stdio.h>
int main(){
	int a,b;
	scanf("%d %d",&a,&b);
	printf("%d\n",a+b);
	return 0;
}

P1001 - 01 序列

n-nuo

【題目描述】

Time Limit: 1000 ms
Memory Limit: 256 mb
對於長度爲6位的一個01串,每一位都可能是0或1,一共有64種可能。它的前幾個是:
000000
000001
000010
000011
000100
請按從小到大的順序輸出這64種01串。

【輸入描述】

【輸出描述】

輸出64行,每行一個01串。

【代碼實現】

  • Example 01:
#include <stdio.h>

int main(){
	int i,temp,j;
	int a[6];
	for(i=0;i<64;i++){
		temp = i;
		for(j=0;j<6;j++){
			a[j] = temp%2;
			temp /= 2;
		}
		for(j=5;j>=0;j--){
			printf("%d",a[j]);
		}
		printf("\n");
	}
	return 0;
} 
  • Example 02:
#include <stdio.h>
void func(int n);
int main(){
	int i;
	for(i=0;i<64;i++){
		func(i);
		printf("\n");
	}
	return 0;
} 

void func(int n){
	int j;
	int a[6];
	for(j=0;j<6;j++){
		a[j] = n%2;
		n /= 2;
	}
	for(j=5;j>=0;j--){
		printf("%d",a[j]);
	}
}
  • Example 03:棧+C++
#include<iostream>
#include<stack>
using namespace std;
int main()
{
	for (int num = 0; num < 64; num++) {
		stack<int> s;
		int tmp = num;
		for (int i = 1; i <= 6; i++) {
			s.push(tmp%2);
			tmp /= 2;
		}
		while (!s.empty()) {
			cout << s.top();
			s.pop();
		}
		cout << endl;
	}
	return 0;
}
  • Example 04:求和
#include<stdio.h>
int main(){
    int i,j;     
    for(i=0;i<64;i++){
    	int res = 0;
        int temp = i;
		int cnt = 0;   
		int a[6]={0};
        while(temp>0){
            a[cnt] = temp % 2;
            temp /= 2;
            cnt++;
        }
        for(j = 5;j>=0;j--){
           res = res * 10 +a[j];
        }
        printf("%06d\n",res);
    }
    return 0;
}
  • Example 05:
#include <bits/stdc++.h>
using namespace std;

int main(){

    int A[6]={0};
    int i,j;
    
    for(i=0;i<64;i++)
    {
        printf("%d%d%d%d%d%d\n",A[0],A[1],A[2],A[3],A[4],A[5]);
        A[5]++;
        for(j=5;j>0;j--)
        {
            if(A[j] == 2)
            {
                A[j]= 0;
                A[j-1]++;
            }
        }

    }
    
    return 0;
}
  • Example 06:遞歸
#include<bits/stdc++.h>
using namespace std;

void Badd(char *s,int i); 

int main(){
    char s[6]={'0','0','0','0','0','0'};
    for(int i=0;i<64;i++){
        cout<<s<<endl;
        Badd(s,5);      
    }
    return 0;
}

void Badd(char *s,int i){   //二進制加法器 
    if(i==-1)return;
    if(s[i]=='0')s[i]='1';
    else{
        s[i]='0';
        return Badd(s,i-1);
    }    
}

P1002 - 數字統計

n-nuo

【題目描述】

Time Limit: 1000 ms
Memory Limit: 256 mb
請統計某個給定範圍[L, R]的所有整數中,數字 2 出現的次數。
比如給定範圍[2, 22],數字 2 在數 2 中出現了 1 次,在數 12 中出現 1 次,在數 20 中出現 1次,
在數 21 中出現 1次,在數 22 中出現 2 次,所以數字 2 在該範圍內一共出現了 6次。

【輸入描述】

2個正整數 L 和 R,之間用一個空格隔開。
1≤L≤R≤100000

【輸出描述】

數字 2 出現的次數。

【代碼實現】

#include <stdio.h>

int main(){
	int i,j,temp,count=0;
	int l,r;
	scanf("%d %d",&l,&r);//輸入l,r 
	for(i=l;i<=r;i++){
		temp = i;
		while(temp>0){
			if(temp%10==2){//取個位值是否和2相等,count++ 
				count++;
			}
			temp /= 10;  
		}
	}
	printf("%d\n",count);
	
	return 0;
}

P1003 - 翻轉數的和

n-nuo

【題目描述】

Time Limit: 1000 ms
Memory Limit: 256 mb
喜歡數學的Lucy經常向哥哥LF請教一些題目,哥哥總能很快就幫她解決。這不,Lucy又有難題要請教哥哥了Lucy的難題。
Lucy的難題是有關翻轉數的:

翻轉數就是把一個數的所有數字以相反順序寫出來,即原來的第一個數字成爲最後一個,而原來的最後一個則成爲第一個。我們把未翻轉的數稱爲翻轉數的原數。

例 如:6789的翻轉數是9876,而6789就是9876的原數。

你一定注意到了,一個翻轉數的原數可以有很多個,例如18的原數可以是818108100等等。因此我們假定:通過翻轉數求原數時,所求得的原數的個 位不爲零。即18的原數就是81。

Lucy現在給出2個翻轉數A、B。

這兩個翻轉數各自對應的原數是C、D,C 與D的和爲 S ,Lucy想知道 S是多少?

【輸入描述】

輸入爲多組測試數據,每行一組,包含兩個整數A、B,兩數中間以空格分隔,0<A<100 000 0000<B<100 000 000

【輸出描述】

輸出只有一個整數,即爲S。
注意:請不要輸出翻轉數前面無用的零。例:對於0056,只需輸出56

【代碼實現】

  • Example 01:C 語言實現
#include <stdio.h>
int reverse(int x);//函數聲明 

int main(){
	int a,b;
	while((scanf("%d %d",&a,&b))!=EOF){
		int s = reverse(a) + reverse(b); 
		printf("%d\n",s);
	} 
	return 0;
}

int reverse(int x){//實現翻轉函數 
	int result = 0;
	while(x){
		result = result * 10 + x % 10; //x%10表示取個位 
		x /= 10;//表示捨去個位 
	}
	return result;
} 
  • Example 02:
#include <stdio.h>
int main() {
    int a, b;
    while(~scanf("%d %d", &a, &b)) {
        int ans1 = 0, ans2 = 0;
        while(a) {
            ans1 = ans1 * 10 + a % 10;
            a /= 10;
        }
        while(b) {
            ans2 = ans2 * 10 + b % 10;
            b /= 10;
        }
        printf("%d\n", ans1 + ans2);
    }
    return 0;
}
  • Example 03:
#include<iostream>
using namespace std;
int reverse(int num)
{
	int result = 0;//翻轉結果 
	while (num > 0) {
		result = result * 10 + num % 10;
		num /= 10;
	}
	return result;
}
int main()
{
	int a, b;
	while (cin >> a >> b) {//EOF結束輸入
		int c = reverse(a);
		int d = reverse(b);
		cout << c + d << endl;
	}
	return 0;
}

P1005 - 博學樓的階梯

n-nuo

【題目描述】

Time Limit: 1000 ms
Memory Limit: 256 mb
衆所周知,北校區的博學樓是有11層樓,並且有三個電梯。假設電梯上升一層需要6秒,下降一層需要4秒,在每在一層停留需要3秒。電梯初始在1樓,現在給你電梯要去樓層順序,例如325代表電梯從1樓到達三樓,再從三樓到達2樓,再從2樓到達5樓。問通過這些操作,電梯需要花多少時間?例如325,從1樓到3樓需要2 * 6秒,然後停留3秒,再從3樓到2樓需要4秒,再停留3秒,再由2樓到5樓需要3 * 6秒,停留3秒。所以總共需要2 * 6 + 3 + 4 + 3 + 3 * 6 + 3 = 43。如果上次要停留的樓層與這次相同,則只需要再停留3秒即可。

【輸入描述】

輸入:輸入一個整數T(1 <= T <= 100),代表有T組樣例。
每組樣例有一個整數n(1 <= n <= 100),代表有n層樓是電梯需要去的。然後給出n個整數,給出的整數小於等於11,代表電梯到達樓層的順序。

【輸出描述】

輸出:對每組樣例,輸出一個整數,代表今天博學樓開放教室的總數。每個輸出結果後面均包含換行符。

【代碼實現】

  • Example 01:
#include <stdio.h>
int func(int a[],int n);//函數聲明 
int main(){
	int t,n,i;
	scanf("%d",&t);//有t組數據輸入
	while(t--){
		int result = 0;//耗時
		scanf("%d",&n);//記錄每組數據的個數
		int a[n];//定義數組,存放每組數據
		i = n;
		while(i--){
			scanf("%d",&a[n-1-i]);//記錄現在所在樓層 
		} 
		result = func(a,n);//計算耗時	
		printf("%d\n",result);//輸出result 
	}
	return 0;
}

int func(int a[],int n){
	int sum = 0,i;
	int last = 1;//定義初始樓層
	int now;//定義現在所在樓層
	for(i=0;i<n;i++){
		now = a[i];//現在所在樓層 
		if(now>last){
			sum += 3 + (now - last) * 6;//電梯往上 
		}else{
			sum += 3 + (last - now) * 4;//電梯往下 
		}
		last = now;//重新修改上一層樓層 
	}
	return sum;
}
  • Example 02:
#include <iostream>
using namespace std;

int main(){
	int m;  //接受數據的組數
	int n;  //每組數據的個數
	cin >> m;
	while(m--){
		int count = 0; //所耗時間總數
		int last = 1;  //上一次電梯所在樓層 
		int now;  //目前電梯所在樓層數
		cin >> n;
		count += 3*n;  //電梯一共要停留n次,每次3秒
		int values[n];
		int j=0;   //記錄數組元素已賦值個數
		/*---開始向數組賦值---*/
		 while (cin >> now && j<n ) {
        	values[j++] = now;
        	if (cin.get() == '\n')   //遇到回車,終止
           		break;
    	}
		
		/*---開始按樓層計算運動時間---*/
		for(int i=0;i<n;i++)
		{
			now=values[i];
			if(now > last)
				count += (now - last)*6;
			else
				count +=( last- now)*4;
			last = now;
		}
		cout << count << endl;
	}
	return 0;
}
  • Example 03:
#include<iostream>
using namespace std;
int main()
{
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		int ans = 3*n;//停留時間 
		int pre = 1, next;//之前停留的樓層,接下來要去的樓層 
		while (n--) {
			cin >> next;
			if (pre > next)
				ans += 4*(pre-next);//下降 
			else if (pre < next)
				ans += 6*(next-pre);//上升 
			//else 上次要停留的樓層與這次相同
			pre = next; 
		}
		cout << ans << endl; 
	}
	return 0;
}

P1006 - 字符串翻轉

n-nuo

  • C 語言版
/*給定一個字符串,反序輸出。*/

#include <stdio.h>
#include <string.h>
#define N 100

int main(){
	char s[N];
	gets(s);
	int i,len = strlen(s);
	for(i=len-1;i>=0;i--){
		printf("%c",s[i]);
	}

	return 0;
}
  • C++ 版
#include<iostream>
#include<stack>
using namespace std;
int main()
{
	string s;
	cin >> s;
	for (int i = s.length()-1; i >= 0; i--) {
		cout << s[i];
	}
	cout << endl;
	return 0;
}
  • 考慮算法版:棧的使用
#include <stdio.h>
#include <string.h>

int main()
{
    char s[100] = {'0'}, c;
    int top = 0;

    while (scanf("%c", &c) != EOF && c != '\n')
    {
        s[top] = c;
        top++;
    }

    while (top--)
    {
        printf("%c", s[top]);
    }

    return 0;
}
  • 考慮算法版:遞歸的使用
#include <stdio.h>

void PrintReverseString()
{
    char c;
    scanf("%c", &c);
    if ('\n' == c)
        return;
    else
    {
        PrintReverseString();
        printf("%c", c);
    }

}

int main()
{
    PrintReverseString();     
    return 0;
}

P1007 - 整除

n-nuo

  • C 語言版
/*輸出100到1000之間能被5和6同時整除的數,
輸出格式爲每10個數爲一行。相鄰兩個數之間用空格隔開(注意每一行末尾沒有空格)*/ 

#include <stdio.h>
int main(){
	int i,cnt=0;
	for(i=100;i<=1000;i++){
		if(i%5==0&&i%6==0){
			cnt++;
			if(cnt%10==0){
				printf("%d\n",i);
			}else{
				printf("%d ",i);
			}	
		}
	}
	return 0;
}
  • C++ 版
#include <iostream>
using namespace std;
int main()
{
	int count = 1;//計數,用於控制輸出格式 
	for (int num = 100; num <= 1000; num++) {
		if (num%5 == 0 && num%6 == 0) {
			if (count%10 == 0)
				cout << num << endl;
			else
				cout << num << " ";
			count++;
		}
	}
	return 0;
}

P1008 - 0 和 1 的個數

n-nuo

  • C 語言版
/*給定一個int型整數,輸出這個整數的二進制的0和1的個數。
輸入樣例:15  輸出樣例:count0=28 count1=4*/ 

#include <stdio.h>
int main(){
	int n; 
	scanf("%d",&n);//輸入int整數
	int cnt = 0;//定義1的個數 
	while(n > 0){
		if(n % 2){
			cnt++;//記錄1的個數 
		}
		n /= 2;
	} 
	printf("count0=%d count1=%d\n",32-cnt,cnt);
	return 0;
}
  • C++ 版
#include <iostream>
using namespace std;
int main()
{
	int num;
	cin >> num;
	int count = 0;
	//int有32位,需要保證0和1的總數爲32,所以只需要記錄1的個數即可 
	while (num > 0) {
		if (num%2 == 1)//該二進制位爲1 
			count++;
		num /= 2;
	}
	cout << "count0=" << 32-count << " count1=" << count << endl;
	return 0;
}

P1009 - 隨機數

n-nuo

  • 符合題意版
/*從1—20之間隨機抽取5個數,輸出抽取出來的5個數之和
與剩餘未被抽取的15個數之和的總和。
輸入描述:無  輸出描述:輸出一個答案在單獨的一行*/ 

#include <stdio.h>
#include <math.h>
int main(){
	int i,a[5],sum1=0,sum=0;
	for(i=0;i<5;i++){
		sum1 += rand()%20+1;
	}
	for(i=1;i<=20;i++){
		sum += i;
	}
	sum = sum - sum1 + sum1;
	printf("%d",sum);
	return 0;
} 
  • C 語言版
#include <stdio.h>
int main(){
	int sum = 0;
	int i; 
	for(i = 1; i <= 20; i++){//遍歷 
		sum += i;
	}
	printf("%d\n",sum);
	return 0;
}
  • C++ 版
#include <iostream>
using namespace std;
int main()
{
	int ans = 0;
	for (int i = 1; i <= 20; i++)
		ans += i;
	cout << ans << endl;
	return 0;
}

P1010 - 排序

n-nuo

  • C 語言版:直接排序
/*輸入n個數進行排序,要求先按奇偶後按從小到大的順序排序。*/

#include <stdio.h>

void sort(int arr[], int n);

int main(){
	int n, i;
	int a[1000];
	scanf("%d", &n);
	for(i = 0; i < n; i++){
		scanf("%d", &a[i]);
	}	
	sort(a, n);//升序排序 
	for(i = 0; i < n; i++){//輸出奇數 
		if(a[i] % 2){
			printf("%d ",a[i]);
		}
	}
	for(i = 0; i < n; i++){//輸出偶數 
		if(a[i] % 2 == 0){
			printf("%d ",a[i]);
		}
	}
	printf("\n");
	return 0; 
} 

void sort(int arr[], int n){//升序排序 
	int i, j, k, tmp;
	for(i = 0; i < n - 1; i++){
		k = i;
		for(j = i + 1; j < n; j++){
			if(arr[j] < arr[k]){
				tmp = arr[k];
				arr[k] = arr[j];
				arr[j] = tmp;
			}
		}
	}
}
  • C 語言版:冒泡排序
/*輸入n個數進行排序,要求先按奇偶後按從小到大的順序排序。*/

#include <stdio.h>

void sort(int arr[], int n);

int main(){
	int n, i;
	int a[1000];
	scanf("%d", &n);
	for(i = 0; i < n; i++){
		scanf("%d", &a[i]);
	}	
	sort(a, n);//升序排序 
	for(i = 0; i < n; i++){//輸出奇數 
		if(a[i] % 2){
			printf("%d ",a[i]);
		}
	}
	for(i = 0; i < n; i++){//輸出偶數 
		if(a[i] % 2 == 0){
			printf("%d ",a[i]);
		}
	}
	printf("\n");
	return 0; 
} 

void sort(int arr[], int n){//升序排序 
	int i, j, tmp;
	for(i = 0; i < n - 1; i++){
		for(j = 0; j < n - 1 - i; j++){
			if(arr[j] > arr[j + 1]){
				tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}
	}
}
  • C++ 版
#include <bits/stdc++.h> //萬能頭文件 
using namespace std;

bool cmp(int a,int b){
    if(a%2==b%2){//若同奇同偶 
        return a<b;//直接從小到大排序 
    }else{
        return (a%2) > (b%2);//奇數在偶數前 
    } 
} 

int main(){
    int n;
    int a[1005] = {0};
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> a[i];
    } 
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++){
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章