#include <iostream>
#include <string>
#include <vector>
using namespace std;
eric30 發表在 痞客邦 留言(0) 人氣(439)
#include
using namespace std;
int main()
{
int n;
while(cin >> n)
{
bool* target = new bool[n];
int* numbers = new int[n];
for ( int i = 1; i <= n-1; i++ )
target[i] = true;
for(int i=0;i
{
cin >> numbers[i];
}
bool flag = false;
for(int i=0;i
{
long int diff = numbers[i] - numbers[i+1];
if(diff < 0) diff = -diff;
if(diff > n-1 || diff < 1) flag = true;
else target[diff - 1] = false;
}
if(flag) cout << "Not jolly" << endl;
else
{
for(int i=0;i
{
if(target[i])
{
cout << "Not jolly" << endl;
flag = true;
break;
}
}
if(!flag) cout << "Jolly" << endl;
}
delete [] target;
delete [] numbers;
}
return 0;
}
eric30 發表在 痞客邦 留言(0) 人氣(344)
#include
using namespace std;
void output(int& n1, int& n2)
{
int count = 0;
bool flag = false;
while(n1 || n2)
{
int sum = n1 % 10 + n2 % 10;
if(flag) ++sum;
flag = false;
if(sum > 9)
{
flag = true;
++count;
}
n1 /= 10;
n2 /= 10;
}
if(count == 0) cout << "No carry operation." << endl;
else if(count == 1) cout << "1 carry operation." << endl;
else cout << count << " carry operations." << endl;
}
int main()
{
int n1, n2;
while(cin >> n1 >> n2)
{
if(!n1 && !n2) break;
output(n1, n2);
}
return 0;
}
eric30 發表在 痞客邦 留言(0) 人氣(508)
#include
using namespace std;
unsigned int reverse(unsigned int number)
{
int rev_number = 0;
while(number)
{
rev_number *= 10;
rev_number += number % 10;
number /= 10;
}
return rev_number;
}
inline bool check_reverse(unsigned int number)
{
return number == reverse(number) ? true : false;
}
int main()
{
int n;
while(cin >> n)
{
while(n--)
{
unsigned int target; // 準備測試有無回文的數字
cin >> target;
for(int i=1;i<=1000;++i)
{
target += reverse(target);
if(check_reverse(target))
{
cout << i << " " << target << endl;
break;
}
}
}
}
return 0;
}
eric30 發表在 痞客邦 留言(0) 人氣(439)
#include
#include
using namespace std;
int main()
{
string str;
bool left = true;
while(getline(cin, str))
{
for(int i=0;i
{
if(str[i] == '"')
{
left ? cout << "``" : cout << "''";
left = !left;
}
else cout << str[i];
}
cout << endl;
}
return 0;
}
eric30 發表在 痞客邦 留言(0) 人氣(99)
#include
#include
#include
using namespace std;
bool check_inside(const double &x, const double &y, const int &a)
{
//查看有沒有在斜線區域的方式就是確認其與四個端點的距離是不是都在 a 之內
if(sqrt(x * x + y * y) > a) return false;
if(sqrt(x * x + (a-y) * (a-y)) > a) return false;
if(sqrt((a-x) * (a-x) + y * y) > a) return false;
if(sqrt((a-x) * (a-x) + (a-y) * (a-y)) > a) return false;
return true;
}
int main()
{
int N, a; // N : 下面要再讀幾筆資料, a : 正方型邊長
while(cin >> N >> a)
{
if(!N) break;
double x, y; // 點的 X 和 Y 值
int inside_count = 0;
for(int i=0;i
{
cin >> x >> y;
if(check_inside(x, y, a)) ++inside_count;
}
double ratio = (double)inside_count / (double)N;
cout << fixed << setprecision(5) << double(ratio * a * a * 100000) / 100000<< endl;
}
return 0;
}
eric30 發表在 痞客邦 留言(0) 人氣(99)
vector get_prime(const int& n)
{
//目標 : 把所有元素加進來,不是質數者刪除之
bool* is_prime = new bool[n];
for(int i=0;i
is_prime[i] = true;
for(int i=2;i
{
int j = 2;
while(i * j < n)
{
is_prime[i * j] = false;
++j;
}
}
//將所有 flag 是 true 的值集合成 vector, 此部份可自行改寫
vector primes;
for(int i=2;i
{
if(is_prime[i]) primes.push_back(i);
}
return primes;
}
eric30 發表在 痞客邦 留言(0) 人氣(82)
#include
using namespace std;
void Swap(int* a, int* b)
{
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
int main()
{
int a = 2, b = 6;
Swap(&a, &b);
cout << "a = " << a << ";b = " << b << endl;
return 0;
}
eric30 發表在 痞客邦 留言(0) 人氣(206)
#include
using namespace std;
//算出這個數是不是2的次方數
bool IsPowerOfTwo(int n)
{
bool flag = false;
if(n == 1) return false;
while(n!=0)
{
if(n & 0x01)
{
if(flag) return false;
flag = true;
}
n = n >> 1;
}
return true;
}
int main()
{
int n;
while(cin >> n)
{
if(n == 0) break;
if(IsPowerOfTwo(n)) cout << n << " 是2的次方數";
else cout << n << " 不是2的次方數";
}
return 0;
}
eric30 發表在 痞客邦 留言(0) 人氣(312)
#include
using namespace std;
unsigned long getNumbers(unsigned long x)
{
x = (x & 0x55555555UL) + ((x >> 1) & 0x55555555UL);
x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
x = (x & 0x0f0f0f0fUL) + ((x >> 4) & 0x0f0f0f0fUL);
x = (x & 0x00ff00ffUL) + ((x >> 8) & 0x00ff00ffUL);
x = (x & 0x0000ffffUL) + ((x >> 16) & 0x0000ffffUL);
return x;
}
int main()
{
unsigned long x;
while(cin>> x)
{
unsigned long total = 0;
total = getNumbers(x);
cout << "一共有 " << total << " 個 1\n";
}
return 0;
}
eric30 發表在 痞客邦 留言(0) 人氣(188)