C++筆試程式設計題
阿新 • • 發佈:2018-12-13
1.輸入一個有符號整數,輸出該整數的反轉值。
#include<iostream>
using namespace std;
int main()
{
int input;
cin >> input;
int output = 0;
while(input!=0)
{
output=output*10+input%10;
input/=10;
}
cout<<output;
}
2.給定整數n,取若干個1到n的整數可求和等於整數m,程式設計求出所有組合的個數。比如當n=6,m=8時,有四種組合:[2,6], [3,5], [1,2,5], [1,3,4]。限定n和m小於120
#include <bits/stdc++.h> using namespace std; int dp[122][122]; int main(int argc, char *argv[]) { int n,m; scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp)); //初始化dp dp[0][0] = 1; for(int k=1;k<=m;++k) dp[0][k] = 0; for(int k=1;k<=n;++k) dp[k][0] = 1; //dp[i][j]表示:從1,2,...,i-1,i取若干個整數求和等於整數j的組合的個數 for(int j=1;j<=m;++j) for(int i=1;i<=n;++i) if((j-i)>=0) dp[i][j] = dp[i-1][j] + dp[i-1][j-i]; else dp[i][j] = dp[i-1][j]; cout << dp[n][m] << endl; return 0; }